5

I am trying to restore and detect the end of auto renewal subscriptions on iOS using https://www.npmjs.com/package/react-native-iap/v/4.0.8 By calling getAvailablePurchases() I get a different number of purchases everytime I invoke this function (e.g 7, next time 23, then 4..). This makes no sense to me. I know the subscription renews any 5 minutes for monthly subscription. My idea is to get the latest purchase and just check the expired dates in the receipt from this one. (at the moment I am parsing over all of them, this cannot be best practice). Another thing is that I detect several active purchases with different transactionReceipts eventhough there is just one active.

RNIap.getAvailablePurchases().then((purchases) => {
            if(purchases != undefined && purchases.length > 0){
                alert(purchases.length + ' purchases');
                purchases.forEach((purchase) => {
                    const receiptBody = {
                        'receipt-data': purchase.transactionReceipt,
                        'password': appSubscriptionId
                    };
                    RNIap.validateReceiptIos(receiptBody, true).then((result) => { // false for production
                        let from, to, lastRenewal = '';
                        if(result.status == 0){ // 0 means receipt is correct..
                            result.latest_receipt_info.forEach((info) => { 
                                if(this.dateIsInFuture(info.expires_date)){
                                    to = moment.utc(info.expires_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    from = moment.utc(info.original_purchase_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    lastRenewal = moment.utc(info.purchase_date, "YYYY-MM-DD HH:mm:ss").toString();
                                    this.setState({ premium: true, from: from, to: to, lastRenewal: lastRenewal});
                                }else{
                                    alert('nicht aktiv');
                                }
                            });
                        }
                    }); 
                }); 
            }else{
                alert('No purchases found');
            }
        });
Alex Yo
  • 105
  • 1
  • 7

1 Answers1

0

This functions gives all available purchases, also receipt: purchases[0].transactionReceipt, line returns latest subscription. I think it solves your problem.

getAvailablePurchases = async () => {
    try {
      console.info(
        'Get available purchases (non-consumable or unconsumed consumable)',
      );
      const purchases = await RNIap.getAvailablePurchases();
      console.info('Available purchases :: ', purchases);
      if (purchases && purchases.length > 0) {
        this.setState({
          availableItemsMessage: `Got ${purchases.length} items.`,
          receipt: purchases[0].transactionReceipt,
        });

        console.log(this.state.availableItemsMessage);
      }
    } catch (err) {
      console.warn(err.code, err.message);
      Alert.alert(err.message);
    }
  };
hakki
  • 6,181
  • 6
  • 62
  • 106
  • Hello, how to handle if user purchase the new subscription before expire the current one, I want the new subscription wil start after current subscription completion – Chandni Dec 07 '21 at 05:28