Expo in app purchases for React Native — how to tell if subscription is current / active?

Viewed 2314

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!

3 Answers

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]
}

Alternatively, you might use a third-party SDK to handle in-app subscriptions and get server-side receipt validation. Here how the code to check current subscription status can look like with Qonversion.io SDK. Disclosure - I'm the co-founder of Qonversion.io

const permissions: Map<string, Permission> = await Qonversion.checkPermissions();
    
const premiumPermission = permissions.get('premium');
if (mainPermission != null) {
  switch (premiumPermission.renewState) {
    case RenewState.NON_RENEWABLE:
      // NON_RENEWABLE is the state of consumable/non-consumable IAPs that could unlock lifetime access
      break;
    case RenewState.WILL_RENEW:
      // WILL_RENEW is the state of an auto-renewable subscription 
      break;
    case RenewState.CANCELED:
      // The user has turned off auto-renewal for the subscription, but the subscription has not expired yet.
      // Prompt the user to resubscribe with a special offer.
      break;
    case RenewState.BILLING_ISSUE:
      // Grace period: permission is active, but there was some billing issue.
      // Prompt the user to update the payment method.
      break;
  }
}

Have you tried to use

const { responseCode, results } = await getPurchaseHistoryAsync(true);
Related