4

Current app is Expo for React Native which is ejected to the bare workflow. Using expo-in-app-purchases for IAP.

How can you tell if a subscription is active or not?

When I grab the purchase history via:

const { results } = InAppPurchases.connectAsync();

if you look at the results, a result returns the following fields:

  • purchaseTime
  • transactionReceipt
  • orderId
  • productId
  • acknowledged
  • originalPurchaseTime
  • originalOrderId
  • purchaseState

Now purchaseState is always an integer. I'm mostly seeing 3 (I think I've seen a 1 one time...) Not sure this actually tells me anything valuable as they are all 3s

Short of manually taking the most recent purchase and adding 30 days (this is a monthly subscription) then seeing if this date is in the past, I'm not sure how to find if current user has active subscription. Help!

Thanks in advance!

chairsandtable
  • 127
  • 2
  • 9
  • Did you ever find a solution for this? I have the same issue. It seems you may be able to decrypt the receipt and read its contents? – whusterj Dec 10 '20 at 19:26
  • @ chairsandtable How do you implement the "expo-in-app-purchases"?when i install and trying to build the solution build it self failed.like SKErrorPaymentCancelled etc.any solution? – Khushi Feb 24 '21 at 07:26

2 Answers2

5

Apple gives you the receipt as a signed and base64-encoded string. In order to see the contents of the receipt (including the 'expires at' date), you need to send this receipt to Apple's receipt verification endpoint along with your app-specific password.

More info here: https://developer.apple.com/documentation/storekit/in-app_purchase/validating_receipts_with_the_app_store

A function similar to this worked for me. This retrieves the receipt info as a JSON object and inside there is expires_at_ms, which can be compared to today's date to see if the subscription has expired.

async validateAppleReceipt(receipt) {
  const prodURL = 'https://buy.itunes.apple.com/verifyReceipt'
  const stagingURL = 'https://sandbox.itunes.apple.com/verifyReceipt'
  const appSecret = '1234...'

  const payload = {
    "receipt-data": receipt,
    "password": appSecret,
    "exclude-old-transactions": true,
  }

  // First, try to validate against production
  const prodRes = await axios.post(prodURL, payload)

  // If status is 21007, fall back to sandbox
  if (prodRes.data && prodRes.data.status === 21007) {
    const sandboxRes = await axios.post(stagingURL, payload)
    return sandboxRes.data.latest_receipt_info[0]
  }

  // Otherwise, return the prod data!
  return prodRes.data.latest_receipt_info[0]
}
whusterj
  • 3,359
  • 1
  • 17
  • 14
-2

Have you tried to use

const { responseCode, results } = await getPurchaseHistoryAsync(true);
  • This retrieves a list of purchases, but it's not clear how to identify whether an auto-renewing subscription purchase is still active. – whusterj Dec 10 '20 at 19:27
  • 1
    I figured out that you need a backend and connect to play store services(in android), you can simply do it by using firebase cloud functions – michele banfi Dec 15 '20 at 18:19
  • Also to note Expo's docs on this: "You should not call this method on launch because restoring purchases on iOS prompts for the user’s App Store credentials, which could interrupt the flow of your app." https://docs.expo.dev/versions/latest/sdk/in-app-purchases/#inapppurchasesgetpurchasehistoryasyncoptions – JCraine Aug 09 '22 at 04:51