chirag04 / react-native-in-app-utils

A react-native wrapper for handling in-app payments
MIT License
892 stars 187 forks source link

loadProducts can not work as expected after upgrading iOS 14 #226

Open hiroyaiizuka opened 3 years ago

hiroyaiizuka commented 3 years ago

version

"react-native-in-app-utils": "^6.0.2",
"react-native": "0.61.5",

Overview

Before upgrade iOS 14, our code worked fine as follows.


 if (!await PurchaseService.canMakePayments) {
       ...
  }

await PurchaseService.loadProducts()

   ...

await PurchaseService.purchaseProduct(productId)

const canMakePayments = () => {
  return new Promise(resolve => {
    InAppUtils.canMakePayments(response => resolve(response))
  })
}

const loadProducts = () => {
  return new Promise((resolve, reject) => {
    InAppUtils.loadProducts(products, (error, response) => {
      if (error) {
        return reject(error)
      }
      return resolve()
    })
  })

const purchaseProduct = (productId: string) => {
  return new Promise((resolve, reject) => {
    InAppUtils.purchaseProduct(
      productId,
      (error, response: PurchaseProductResponse) => {
        if (error) {
          reject(error)
        }
        if (response && response.productIdentifier) {
          resolve(response)
        }
      },
    )
  })
}

But after upgraded iOS14, ios subscription pop up did not show up. It seems loadProducts() failed and purchaseProduct() did not call.

I checked this issue, i upgraded this library from 6.0.1 to 6.0.2

But above code leaded to uncontrollable timing of ios subscription popup.

After changed like below, it worked perfectly.

 if (!await PurchaseService.canMakePayments) {
       ...
  }

await PurchaseService.purchaseProduct(productId)

const purchaseProduct = (productId: string) => {
  return new Promise((resolve, reject) => {
    InAppUtils.loadProducts(products, (error, response) => {
      if (error) {
        reject(error)
      }

      return InAppUtils.purchaseProduct(productId, (error, response) => {
        // NOTE for v3.0: CurrentUser can cancel the payment which will be available as error object here.
        if (error) {
          reject(error)
        }
        if (response && response.productIdentifier) {
          resolve(response)
        }
      })
    })
  })
}

Call purchaseProduct method inside loadProducts callbacks.

It seems there are some related sentences in README, but i think sample code(purchaseProduct()) should be fixed.

スクリーンショット 2020-12-23 16 55 20
hqdai commented 3 years ago

I'm getting this issue too on simulator, device is working fine? Any new update on this :D

hiroyaiizuka commented 3 years ago

Yeah, i could finally work fine both in simulators and device with above changes.

hqdai commented 3 years ago

Yeah, i could finally work fine both in simulators and device with above changes.

Oh, Could you share more detail on your solution? When calling InAppUtils.loadProducts(productIds, (error, products) => { console.log( error); });

Error always show:

Screen Shot 2021-02-05 at 15 55 59