dooboolab-community / react-native-iap

In App Purchase module for React Native!
https://react-native-iap.dooboolab.com
MIT License
2.79k stars 638 forks source link

iOS Receipt Validation Returns Status 21002 #1968

Closed liyamahendra closed 1 year ago

liyamahendra commented 1 year ago

Please use the Discussion board if you want to get some help. Please use issues to report bugs.

Description

On app startup, I want to check if the auto-renewing subscription is still active or not. I'm using the below code:

const isSubscriptionActive = async () => {
        if (Platform.OS === 'ios') {
            const availablePurchases = await RNIap.getAvailablePurchases();
            const sortedAvailablePurchases = availablePurchases.sort(
                (a, b) => b.transactionDate - a.transactionDate
            );
            const latestAvailableReceipt = sortedAvailablePurchases[0].transactionReceipt;

            const isTestEnvironment = __DEV__;
            const decodedReceipt = await RNIap.validateReceiptIos(
                {
                    'receipt-data': btoa(latestAvailableReceipt),
                    password: 'MY_SHARED_SECRET',
                },
                isTestEnvironment
            );
            console.log("decodedReceipt: ", decodedReceipt);
            const { latest_receipt_info: latestReceiptInfo } = decodedReceipt;
            console.log("latestReceiptInfo: ", latestReceiptInfo);
            const isSubValid = !!latestReceiptInfo.find(receipt => {
                const expirationInMilliseconds = Number(receipt.expires_date_ms);
                const nowInMilliseconds = Date.now();
                return expirationInMilliseconds > nowInMilliseconds;
            });
            return isSubValid;
        }
    }

However, the decodedReceipt returns {"status": 21002}

Expected Behavior

Should return the Receipt Info

Screenshots

Environment:

To Reproduce Steps to reproduce the behavior:

  1. Make a call to RNIap.initConnection()
  2. Make a call to RNIap.getAvailablePurchases() as shown in isSubscriptionActive method
  3. You'll notice the following output in console:
    decodedReceipt:  {"status": 21002}
    latestReceiptInfo:  undefined

[Optional] Additional Context

I'm using TypeScript.

iamrohitagg commented 1 year ago

I am getting the same 21002 response on my real device and I am testing the build from TestFlight.

iamrohitagg commented 1 year ago

PS: I got this fixed. I was testing this for sandbox account and I forgot to pass isTest true.

validateReceiptIos({
          receiptBody : body,
          isTest: true,
        })