Working with Consumable IAPs

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

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: StoreId)

where StoreId 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.registerPurchasesChangedHandler { (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).")
    	}
  	}
	}
}

🚧

Android vs Apple

Note that on Android a successful consumable purchase that has not yet been consumed will have a NamiPurchaseState of PENDING.

On iOS the purchase state will be PURCHASED when it is ready to be processed and consumed.

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 iOS 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.registerPurchasesChangedHandler { (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.registerPurchasesChangedHandler { (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.dismiss(animated: true) {
    	self.showMessageWithCreditBalance();
    }
  }
}