skip to Main Content

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

enter image description here

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


  1. Chosen as BEST ANSWER

    Finally after doing more research & trials I found the solution for it.

    So here is the Final working solution :

    Step-1. Import the Library :

    import {initConnection, getProducts, endConnection, requestPurchase, flushFailedPurchasesCachedAsPendingAndroid, finishTransaction} 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. End Connection :

     componentWillUnmount() {
                 endConnection();
            }
    

    Step-4. Buy Product :

    doBuyProduct = async () => {
        if (Platform.OS == 'android') {
            try {
                await requestPurchase({skus:["inapp_1_99"]})
                    .then(async (purchase) => {
                    this.setState({
                        isLoadingActive : false,
                    })
                    finishTransaction({purchase: purchase[0], isConsumable: true, developerPayloadAndroid: undefined});
                })
                .catch((e) => {
                    this.setState({
                        isLoadingActive : false
                    })
                });
            } catch (err) {
                this.setState({
                    isLoadingActive : false
                })
            }
        } else {
            try {
                await requestPurchase({sku, andDangerouslyFinishTransactionAutomaticallyIOS:false})
                    .then(async (purchase) => {
                    this.setState({
                        isLoadingActive : false,
                    })
                    finishTransaction({purchase:purchase, isConsumable: true});
                })
                .catch((e) => {
                    this.setState({
                        isLoadingActive : false
                    })
                });
            } catch (err) {
                this.setState({
                    isLoadingActive : false
                })
            }
        }
    }
    

    Important Note :

    • Make sure that you are upgraded to the latest Package version for the InApp purchase before doing above changes

    Hope this will be helpful for everyone.


  2. First of all, you must not use both async-await and promise together. It will not work as expected. You should use either one of them.

    const purchase = await requestPurchase({skus:["inapp_1_99"]})
    this.setState({
        isLoadingActive : false,
    })
    const purcString = JSON.stringify(purchase);
    Alert.alert("Success Alert", purchase[0].purchaseToken, [{text: "Ok"}], { cancelable: true});
    await finishTransaction({purchase: purchase, isConsumable: true, developerPayloadAndroid: ""});
    

    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

    Login or Signup to reply.
  3. @Mayur Prajapati what is the difference between both codes?
    code1-

     await finishTransaction({purchase: purchase, isConsumable: true, developerPayloadAndroid: ""});
    

    code2-

    finishTransaction({purchase: purchase[0], isConsumable: true, developerPayloadAndroid: undefined});
                })
    

    only purchase[0] is it working only for these small changes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search