dooboolab-community / react-native-iap

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

getAvailablePurchases returns correct data only once on iOS #1987

Closed vvodicka closed 1 year ago

vvodicka commented 1 year ago

Description

Docs for getAvailablePurchases says, it shall return "all purchases made by the user (either non-consumable, or haven't been consumed yet)". If I use it to get not-yet-consumed consumables, it returns correct data only once. Then it returns empty array till I make another purchase. currentPurchase is still set correctly during this behaviour. Above description applies also to getPurchaseHistory

Example:

import {getAvailablePurchases, requestPurchase, useIAP} from 'react-native-iap'

const {currentPurchase} = useIAP()

//purchase created with this call some time ago (currentPurchase exists) :
await requestPurchase({
          obfuscatedAccountIdAndroid: me?.userId,
          obfuscatedProfileIdAndroid: me?.userId,
          sku: selectedProductId,
          andDangerouslyFinishTransactionAutomaticallyIOS: false,
          applicationUsername: me?.userId,
        })

const logPurchases = async () => {
      const ap = await getAvailablePurchases()
      console.log('after 1st', ap)
      const ap2 = await getAvailablePurchases()
      console.log('after 2nd', ap2)
  }

<Button
        title={'logPurchases'}
        onPress={() => {
          logPurchases()
        }}
      />

Actual Behavior

console log after logPurchases button press: after 1st [{"productId": "com...] after 2nd [ ]

Expected Behavior

console log after logPurchases button press: after 1st [{"productId": "com...] after 2nd [{"productId": "com...]

Environment:

To Reproduce Steps to reproduce the behavior:

See example

andresesfm commented 1 year ago

The reason for this is that the restore transactions are marked as finished as soon as getAvaiableItems gets called. It makes it convenient because other platforms (including Storekit2) handle it automatically. However, there are situations when you'd like to have the freedom to handle finishing the transaction manually. I have this on my list to fix as soon as 11.0.0 lands

andresesfm commented 1 year ago

Fixed here: https://github.com/dooboolab/react-native-iap/pull/2000

andresesfm commented 1 year ago

Released on 11.0.2 Please set automaticallyFinishRestoredTransactions to false when calling getAvailableItems. Like so: getAvailableItems({automaticallyFinishRestoredTransactions:false}) Keep in mind that when this is set to false, you'll have to call finishTransaction when you are done restoring the purchase

vvodicka commented 1 year ago

Actually it seems to have bug. I get getAvailableItems is not a recognized Objective-C method error screen when trying to call getAvailableItems (with or without that newly added parameter)

andresesfm commented 1 year ago

Fixed here: https://github.com/dooboolab/react-native-iap/pull/2007