React-native-iap: What is the correct way of restoring Auto-Renewable Subscriptions?

Created on 17 Jun 2019  路  6Comments  路  Source: dooboolab/react-native-iap

Version of react-native-iap

2.4.1

Version of react-native

0.59.3

Platforms you faced the error (IOS or Android or both?)

IOS

Expected behavior

Restore available auto-renewable subscriptions

Actual behavior

Restore auto-renewable subscriptions in different devices.

Tested environment (Emulator? Real Device?)

iPhone 6s Plus

Steps to reproduce the behavior

call and priny the results of await RNIap.getAvailablePurchases() and await RNIap.getPurchaseHistory() methods orderly


After purchasing auto-renewable subscriptions I want to restore purchased subscriptions in different devices. As mentioned in Deprecated Readme (iOS Purchasing process right way)

Non-consumable products can be restored via getPurchaseHistory function

but in documentation getAvailablePurchases() is used to restore purchases. In my tests, the result of RNIap.getAvailablePurchases() and RNIap.getPurchaseHistory() has different items and sizes.

I am very confused which one is the restored items. Which method should be used? Which one is the latest valid restored subscription?

Thank you.

馃摫 iOS 馃檹 help wanted

Most helpful comment

Yes. I did my restoring process as @andrewzey solution.

const availablePurchases = await RNIap.getAvailablePurchases();
    const sortedAvailablePurchases = availablePurchases.sort(
      (a, b) => b.transactionDate - a.transactionDate
    );
    const latestAvailableReceipt = sortedAvailablePurchases[0].transactionReceipt;

Now it is working correctly in my tests.

Thank you

All 6 comments

image

Have you went through this?

Yes, when I call getAvailablePurchases() 9 elements returns but when I call getPurchaseHistory() 2 elements returns. But the bottom images shows that restoring should be done with getAvailablePurchases(). As you can see non-consumable products mentioned in either with getAvailablePurchases() and getPurchaseHistory(). Which one is correct?

Screen Shot 2019-06-17 at 18 07 06

Screen Shot 2019-06-17 at 18 11 40

Screen Shot 2019-06-17 at 18 12 53

Also I can not understand how these methods returns different items while calling the same action?

index.js

/**
 * Gets an invetory of purchases made by the user regardless of consumption status
 * @returns {Promise<Purchase[]>}
 */
export const getPurchaseHistory = () => Platform.select({
  ios: async() => {
    checkNativeiOSAvailable();
    return RNIapIos.getAvailableItems();
  },
  //...
})();

/**
 * Get all purchases made by the user (either non-consumable, or haven't been consumed yet)
 * @returns {Promise<Purchase[]>}
 */
export const getAvailablePurchases = () => Platform.select({
  ios: async() => {
    checkNativeiOSAvailable();
    return RNIapIos.getAvailableItems();
  },
  //...
})();

as you can see two methods returns RNIapIos.getAvailableItems();

RNIapIos.m

RCT_EXPORT_METHOD(getAvailableItems:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
    [self addPromiseForKey:@"availableItems" resolve:resolve reject:reject];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

As I know restoreCompletedTransactions restores the purchases. So these two methods restoring the purchases. Where am I wrong?

Did you follow #275?

Yes. I did my restoring process as @andrewzey solution.

const availablePurchases = await RNIap.getAvailablePurchases();
    const sortedAvailablePurchases = availablePurchases.sort(
      (a, b) => b.transactionDate - a.transactionDate
    );
    const latestAvailableReceipt = sortedAvailablePurchases[0].transactionReceipt;

Now it is working correctly in my tests.

Thank you

Thanks for coming back. I'll close this for now.

Was this page helpful?
0 / 5 - 0 ratings