javascriptandroidreact-nativein-app-purchasereact-native-iap

React Native In App Purchase Issues In Android For Consumable InApp Purchase


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.


Solution

  • 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 :

    Hope this will be helpful for everyone.