I have Integrated React-Native-iap Package on my React Native Application for Implementing the Consumable InApp purchases.
Link :
https://github.com/dooboolab-community/react-native-iap
Here is What I have done so far :
Step-1. Import the Library :
import {initConnection, getProducts, getAvailablePurchases, endConnection, purchaseUpdatedListener, requestPurchase, flushFailedPurchasesCachedAsPendingAndroid, finishTransaction, AndroidModule, acknowledgePurchaseAndroid, clearTransactionIOS} from 'react-native-iap';
Step-2. Initialize Connection :
componentDidMount() {
this.initilizeIAPConnection();
}
initilizeIAPConnection = async () => {
try {
const result = await initConnection();
const products = await getProducts(itemSkus);
if (Platform.OS == 'android') {
await flushFailedPurchasesCachedAsPendingAndroid();
} else {
// await clearTransactionIOS();
}
} catch (err) {
console.warn(err.code, err.message);
}
};
Step-3. Buy Product :
doBuyProduct = async () => {
try {
await requestPurchase({skus:["inapp_1_99"]})
.then(async (purchase) => {
this.setState({
isLoadingActive : false,
})
let purcString = JSON.stringify(purchase);
Alert.alert("Success Alert", purchase[0].purchaseToken, [{text: "Ok"}], { cancelable: true});
await finishTransaction({purchase: purchase, isConsumable: true, developerPayloadAndroid: ""});
})
.catch((e) => {
this.setState({
isLoadingActive : false
})
Alert.alert("Fail Alert", e.message, [{text: "Ok"}], { cancelable: true});
});
} catch (err) {
Alert.alert("Main Fail Alert", err.message, [{text: "Ok"}], { cancelable: true});
this.setState({
isLoadingActive : false
})
}
}
Everything works well on iOS Side.
On Android Once I Call The Buy Product Function it gives success message & successfully buy the InApp.
But After 5 minutes I am getting Order Cancellation Receipt Email like this :
Test: Your Google Play Order Cancellation Receipt from 07-Apr-2023
As I am already calling Finish Transaction Which covers the Acknowledgement part for this Consumable InApp purchase. But somehow its not working according to its required.
Can someone guide me For the same.
Thanks in Advance.
3
Answers
Finally after doing more research & trials I found the solution for it.
So here is the Final working solution :
Step-1. Import the Library :
Step-2. Initialize Connection :
Step-3. End Connection :
Step-4. Buy Product :
Important Note :
Hope this will be helpful for everyone.
First of all, you must not use both
async-await
andpromise
together. It will not work as expected. You should use either one of them.it should be something like that, you should handle it by yourself.
About the question,
Which versions are you using for React Native and React Native IAP?
Also, you did not mention the backend side for this flow. Are you validating the receipt properly on the backend-side?
Here is the official documentation for acknowledgment.
https://developer.android.com/google/play/billing/integrate#acknowledge
Also, please check this out: after 6 times renewed, subscriptions are canceled for testing purposes
https://stackoverflow.com/a/66316354/2247055
@Mayur Prajapati what is the difference between both codes?
code1-
code2-
only purchase[0] is it working only for these small changes