Reputation: 89
^2.4.1
16.6.3
Android
Listing Items or Buying an Item
Products Array is empty
Real Device
I got two errors: E_UNKNOWN An unknown or unexpected error has occured. Please try again later. adding purchase listener is only provided in ios. This is my Item's id: ozann_ozan
Some codes:
<TouchableOpacity
onPress={() => this.buyItem('ozann_ozan')}
>
const itemSkus = Platform.select({
ios: [
'com.example.coins100'
],
android: [
'com.bettipspro'
]
});
componentWillMount() {
this.getAvailablePurchases();
this.getItems();
}
async componentDidMount() {
try {
const products = await RNIap.getProducts(itemSkus);
console.log(products);
this.setState({ products });
} catch(err) {
console.warn(err); // standardized err.code and err.message available
}
}
getItems = async() => {
const products = await RNIap.getProducts(itemSkus);
console.log(itemSkus);
try {
const products = await RNIap.getProducts(itemSkus);
// const products = await RNIap.getSubscriptions(itemSkus);
console.log('Products', products);
this.setState({ productList: products });
} catch (err) {
console.warn(err.code, err.message);
}
}
getAvailablePurchases = async() => {
console.log('eben');
try {
console.info('Get available purchases (non-consumable or unconsumed consumable)');
const purchases = await RNIap.getAvailablePurchases();
console.info('Available purchases :: ', purchases);
Alert.alert(purchases.length);
if (purchases && purchases.length > 0) {
Alert.alert('purchases.length');
this.setState({
availableItemsMessage: `Got ${purchases.length} items.`,
receipt: purchases[0].transactionReceipt,
});
}
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
}
}
buyItem = async(sku) => {
console.info('buyItem: ' + sku);
try {
const purchase: any = await RNIap.buyProduct(sku);
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
} catch (err) {
console.warn(err.code, err.message);
const subscription = RNIap.addAdditionalSuccessPurchaseListenerIOS(async(purchase) => {
this.setState({ receipt: purchase.transactionReceipt }, () => this.goToNext());
subscription.remove();
});
}
}
Upvotes: 3
Views: 5829
Reputation: 26
I had the same issue, and even though this is a bit old, I figured it would be useful to provide some support for those who have problems. As I just came across this problem myself and it was annoying as the documentation doesn't really mention this clearly.
On android Only - you need to open a connection to the billing service. This used to be RNIAP.prepare()
, but it is now RNIAP.initConnection()
. This does nothing on iOS but it is a required step for the RNIAP.getProducts(itemSkus)
to work.
Also, if you are trying to receive subscriptions, for some reason it is agnostic on iOS and you get them if you call RNIAP.getProducts(itemSkus)
but on android you need to specify them as subscriptions for it to work. You can do this with RNIAP.getSubscriptions(itemSkus)
.
I was fighting with this for a couple of hours on Android and iOS for different issues, so I hope this helps.
Cheers
Upvotes: 1