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

Unable to get the history of purchases #2026

Closed kayro2222 closed 1 year ago

kayro2222 commented 1 year ago

Please use the Discussion board if you want to get some help. Please use issues to report bugs.

I'm trying to get the user's purchases with getPurchaseHistory() and getAvailablePurchases() to verify if the user has active subscription to send him to the main navigator of the app or to the subscription offers. The functions are returning an undefined value or an empty array.

Expected behavior

I need to get the history of purchases to validate if the date is expired or not. Using the version 10.0.6, I had successfully get the history using the methods described above.

Screenshots

Environment:

To Reproduce Steps to reproduce the behavior:

I'm requesting the purchase using the examples provided from the lib:

const handleRequestSubscription = async () => {
    setLoading(true)
    if (selectedSubscription) {
      if (Platform.OS === 'ios') {
        try {
          await requestSubscription({
            sku: selectedSubscription?.productId,
          })
        } catch (error) {
          setLoading(false)
          if (error instanceof PurchaseError) {
            Alert.alert(
              'Error',
              'A error has occurred. Please check your payment configuration.',
            )
            console.log({ message: `[${error.code}]: ${error.message}`, error })
          } else {
            console.log({ message: 'handleBuySubscription', error })
          }
        }
      }
    }
  }

  useEffect(() => {
    const checkCurrentPurchase = async () => {
      try {
        if (currentPurchase?.productId) {
          await finishTransaction({
            purchase: currentPurchase,
            isConsumable: true,
          })

          setIsSubscribe(true)
        }
      } catch (error) {
        if (error instanceof PurchaseError) {
          Alert.alert(
            'Error',
            `message: [${error.code}]: ${error.message}, error`,
          )
        } else {
          Alert.alert('Error', `message: 'handleBuyProduct', ${error}`)
        }
      }
    }

    checkCurrentPurchase()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [currentPurchase, finishTransaction])

But when I reload the app, I want to verify if this subscription is active using:

export const PurchaseHistory: React.FC = () => {
  const navigation = useNavigation()
  const { getAvailablePurchases, getPurchaseHistory } = useIAP()
  const { setIsSubscribe } = useSubscriptionContext()
  const [loading, setLoading] = useState(false)

  const handleGetPurchaseHistory = async () => {
    console.log('getPurchaseHistory', await getPurchaseHistory())
    console.log('getAvailablePurchases', await getAvailablePurchases())
  }

  useEffect(() => {
    const checkSubscribeActive = async () => {
      handleGetPurchaseHistory()
    }
    checkSubscribeActive()
  }, [])

  return <></>
}

My logs:

image

[Optional] Additional Context

I'm using setup({ storekitMode: 'STOREKIT2_MODE' }) I do not save the purchase information at my server-side.

andresesfm commented 1 year ago

Since you are using Storekit 2. You can try await IapIosSk2.latestTransaction(sku)

kayro2222 commented 1 year ago

it worked @andresesfm! thanks for your time.

kayro2222 commented 1 year ago

hey @andresesfm, I'm receiving [Error: Can't find product for sku **my_sku**] using IapIosSk2.latestTransaction(skus.monthly) when I reload my app. At fast refresh, I'm able to get the subscriptions. I'm still able to purchase the subscription with

await requestSubscription({
   sku: selectedSubscription?.productId,
})

Other problem that I'm seeing, is that this function IapIosSk2.subscriptionStatus(skus.monthly) always returns expired [{"state": "expired"}]

kayro2222 commented 1 year ago

it seems that I need to use the context hook at the purchase history too. I can't let my own context handle this.