Reputation: 555
I'm trying to add In App Purchases (or as Google likes to call it; "In App Billing") to my React Native App.
What I've done:
yarn add react-native-iap
(this library)react-native run-android
on a physical device, and through publishing it to an Alpha test from Google Play ConsoleNotes: The app is signed and billing permissions are enabled in the manifest file, and the version of react-native-iap
is 0.3.23.
The issue:
When running a debug build the console.log()
just prints that the product is undefined
, and the productInfo
does not display on screen when running the Alpha deployed version (so the product was also undefined there). The products
variable is just an empty array.
(The try
-statement seems to succeed since I see no errors printed from it.)
import InAppPurchase from "react-native-iap";
const itemSKUs = Platform.select({
android: [
"com.kimer.unlock_premium" // I've also tried just "unlock_premium"
]
})
...
constructor(props) {
super(props);
this.state = {
modalVisible: false,
premiumProduct: undefined
};
}
...
async componentDidMount() {
try {
await InAppPurchase.prepare();
const products = await InAppPurchase.getProducts(itemSKUs);
console.log("finished call to get products");
console.log(products[0]);
this.setState({premiumProduct: products[0]});
} catch (err) {
console.warn(err);
}
}
...
render() {
let productInfo;
if (this.state.premiumProduct !== undefined) {
productInfo = (
<Text>{this.state.premiumProduct}
{this.state.premiumProduct.localizedPrice}
{this.state.premiumProduct.currency} {this.state.premiumProduct.productID}
{this.state.premiumProduct.title}
{this.state.premiumProduct.description}</Text>
);
}
return (
<View>
...
{productInfo}
...
</View>
);
}
Upvotes: 0
Views: 2329
Reputation: 555
SOLVED: It is now working for me 😄! I tried a couple of things but I'm not sure what was key to getting it to work 🤔 This is anyways what I did:
import InAppPurchase from "react-native-iap;" to "import * as InAppPurchase from 'react-native-iap';"
Upvotes: 1