RNIap.getPurchaseHistory() always returns an empty array on iOS

1.4k views Asked by At

The RNIap.getPurchaseHistory always return an empty array. Does not matter if I call on app start or after purchase was made. In componentDidMount I do the following:

async componentDidMount() {

  await RNIap.initConnection();

  this.purchaseUpdateSubscription = purchaseUpdatedListener(
    async purchase => {
      try {
        // send purchase to the server
        const savedPurchase = await savePurchase(purchase);

        // finish transaction if server returned truthly value
        if (savedPurchase) {
          await RNIap.finishTransaction(purchase, true);

          // always returns en empty array
          console.log('Purchase history', await RNIap.getPurchaseHistory())
        }
      } catch (e) {
        console.log('Error: ', e);
      }
    }
  });
}

buyProduct function:

buyProduct = async productId => {
  try {
    await RNIap.requestPurchase(productId, false);
  } catch (e) {
    console.warn('Error: ', e);
  }
};

When the purchase is made I receive the message:

You're all set.
Your purchase was successful
[Environment: Sandbox].

Maybe the problem can be in another place not related to the codebase?

2

There are 2 answers

0
Erik Dreifaldt On

Stumbled upon what I believe might be a solution for you:

Wrap RNIap.getPurchaseHistory() around initConnection() and an endConnection() call. Perhaps also give RNIap.finshTransaction more time to finish before requesting the purchase history to make sure the latest purchases are included.

// CODE 
await RNIap.initConnection();
   const purchase_history = await RNIap.getPurchaseHistory()
   console.log(purchase_history)
RNIap.endConnection()
0
Md. Robi Ullah On

The solution applies to products or subscriptions other than Auto-Renewable subscriptions or products.

According to Apple documentation, they only store auto-renewable subscriptions. So you have to manage to make available the subscription/product to other devices owned by this user.

Steps to manage this:

  1. Get user identity by calling getUserId using the https://www.npmjs.com/package/react-native-user-identity package.
  2. Save the subscription/product details with a reference to the id on your server

You can get the same id by calling getUserId to other devices signed in with the same apple id.