Working with Consumable IAPs

There are a few extra steps needed when working with a consumable purchase. We outline them here.

📘

Consumable Purchases on Android Coming Soon!

Consumable IAPs are only currently supported in our iOS SDK. If you'd like to be notified when these are available on Android, send us an email at [email protected].

When working with consumable purchases, there are a couple of extra steps you need to take in your app code.

  1. You need to let the SDK know that you have processed (or consumed) the consumable so that it is eligible to be purchased again.
  2. Check for purchases that have not been consumed when the app launches and make sure they are consumed.
  3. Optionally take an action on the successful purchase, such as incrementing a credit or coin count granted by purchasing the consumable IAP or displaying a message with the account balance after the transaction.

Consuming the IAP

The Nami platform tracks all purchases that have been made as do the stores themselves and in both places you'll be prevented from making that same purchase a second time.

However, in the case of a consumable IAP, you actually want to let your customers purchase this SKU multiple times.

In order to allow for this, you simply need to Consume the SKU. Once this is done, your customers will be able to purchase it again. This can be done with the following SDK call

NamiPurchaseManager.consumePurchasedSKU(skuID: purchaseProductIdentifier)
[NamiPurchaseManager consumePurchasedSKUWithSkuID: purchaseProductIdentifier];
NamiPurchaseManagerBridge.consumePurchasedSKU(purchaseProductIdentifier)
NamiPurchaseManager.consumePurchasedSKU(skuID);

where purchaseProductIdentifier is the SKU ID defined in the Control Center.

👍

Nami Best Practice

We recommend calling the consumePurchasedSKU method immediately after a successful purchase has happened. See the code sample below.

🚧

App with Consumable and Non-Consumable SKUs

If your app sells a mixture of consumable and non-consumable SKUs (such as subscriptions), be sure to check the SKU ID before you consume the SKU.

NamiPurchaseManager.register { (purchases, state, error) in
  if state == .purchased {
    if let purchase = purchases.first {
      // if you have non-consumable SKUs check the sku_id
      if purchase.skuID == "consumable_sku_id" {
        NamiPurchaseManager.consumePurchasedSKU(skuID: purchase.skuID)            
        print("Consumed purchase for \(purchase.skuID).")
      }
    }
  }
}

Check for Unconsumed Purchases

👍

Nami Best Practice

It is a good idea to check for any unconsumed purchases when your app starts.

If the app were killed during the purchase process or a purchase was made outside of the app, the purchase change listener would not properly consume the purchase and update the user's credit balance.

In order to check for purchases that have not been consumed, add a listener for purchase changes to the applicationDidFinishLaunching method in your app.

We'll modify the code above slightly to track the SKU ID of purchases that are started and go into a pending state as well as purchases that complete with a state of purchased.

As before, if you have non-consumable SKUs in your app, be sure to check the SKU ID to make sure this is a SKU you want to consume.

// Place at class level
var pendingSKUID : String? = .none

...
// Place inside applicationDidFinishLaunching
NamiPurchaseManager.register { (purchases, state, error) in
  if state == .pending {
    self.pendingSKUID = purchases.first?.skuID
  }
                              
  if state == .purchased {
    for purchase in purchases {
      // Make sure the purchase is one we want to consume, vs something like a subscription
      if purchase.skuID == "consumable_sku_id" && purchase.SKUID == self.pendingSKUID  {
        NamiPurchaseManager.consumePurchasedSKU(skuID: purchase.skuID)
        print("Consumed \(purchase.skuID).")
      }
    }
  }
}

Taking Additional Actions When Consuming a Purchase

Finally, we may want to take some additional actions after consuming a purchase, such as:

  • Adding credits to a balance
  • Granting access to an entitlement
  • Displaying a message that states the user's total credit balance

We can accomplish this by adding a little extra code to the last example. Here we see examples of both incrementing a credit count and displaying a message to the user after the purchase completes.

// Place at class level
var pendingSKUID : String? = .none

...
// Place inside applicationDidFinishLaunching
NamiPurchaseManager.register { (purchases, state, error) in
  if state == .pending {
    self.pendingSKUID = purchases.first?.skuID
  }
                              
  if state == .purchased {
    for purchase in purchases {
      // Make sure the purchase is one we want to consume, vs something like a subscription
      if purchase.skuID == "consumable_sku_id" && purchase.SKUID == self.pendingSKUID  {

        increaseCreditBalance(10);       
        NamiPurchaseManager.consumePurchasedSKU(skuID: purchase.skuID)
        print("Consumed \(purchase.skuID) and increased credit balance.")
      }
    }
                
    // Take further action after paywall is closed
    NamiPaywallManager.dismissNamiPaywallIfOpen(animated: true) {                  
      self.showMessageWithCreditBalance();
    }
  }
}

Did this page help you?