react-native-iap: getAvailablePurchases returning "Error: An unknown error occurred" for TestFlight build

Description

Everything was working fine with react native IAP v9.x.x with us. I updated it due to a crash issue on crashlytics regarding the thread safety. Now I am using the latest version, everything is working fine but I am not able to fetch the getAvailablePurchases in the TestFlight build. It’s working fine if I run it directly Run from Xcode.

Expected Behavior

It should return the available purchases

Screenshots

Environment:

  • react-native-iap: v12.7.4
  • react-native: 0.69.0
  • Platforms (iOS, Android, emulator, simulator, device): iOS

To Reproduce Steps to reproduce the behavior:

  1. Call getAvailablePurchases function in the TestFlight build.
  2. “Error: An unknown error occurred” returns.

[Optional] Additional Context


async getAvailablePurchases() {
    try {
      const availablePurchases = await RNIap.getAvailablePurchases()
      Utils.log('Store All Time Total Purchases ' + availablePurchases)
      const purchasesNeedsToCheck = []
      availablePurchases.forEach(element => {
        const txnMomentDate = moment(Number(element.transactionDate))
        const currentDateMoment = moment()
        const olderDays = Math.ceil(moment.duration(currentDateMoment.diff(txnMomentDate)).asDays())
        Utils.log('Transaction old days ' + olderDays)

        if (olderDays <= 35){ // #Warning: Only considering a month old txn. Need to change this if we have yearly subscription
          purchasesNeedsToCheck.push(element)
        }
      });

      Utils.log('Total Purchases needs to check ' + purchasesNeedsToCheck)

      var response = []
      for (const i in purchasesNeedsToCheck) {
        const purchase = purchasesNeedsToCheck[i];
        
        if (Utils.isSubsProduct(purchase.productId)) {
          Utils.log('Calling Validating purchase for productID : ' + purchase.productId)
          const result = await this.storeTransaction(purchase)
          if (result && result.productId) {
            response.push(result.productId)            
            SessionManager.instance.updatePurchasedData(purchase)            
            await SharedPreferences.storePurchasesProducts(result.productId);
            break        
          }
        }
      }

      Utils.log('Valid Purchases ' + JSON.stringify(response))
      return response
    } catch (error) {
      FirebaseManager.instance.recordError(IAP_ERROR_CODE, error);
      throw error
    }
  }

About this issue

Most upvoted comments

I fix this issue.

[Why] I use SK2 mode by default but isIosStorekit2 always return false

[How] setup mode before you call initConnection.

import { setup } from 'react-native-iap'
setup({ storekitMode: 'STOREKIT2_MODE' })

I have the same problem on real ios device in sandbox mode! calling getAvailablePurchases On IOS sometimes gives me the expected array of purchases which i use to find the latestPurchase to check the purchase state(still valid or not) but sometimes it throws an error “Error: An unknown error occurred” while i had not changed anything in the code ! any suggestions please ! PS: i am using the method getAvailablePurchases which is exported by the hook useIAP and then using the value availablePurchases “react-native-iap”: “12.7.3”