Upgrade from IAP version 4 to version 5
Follow this guide to migrate from Unity In-App Purchasing version 4 to version 5.
Read time 6 minutesLast updated 6 days ago
In-App Purchasing (IAP) version 5 (v5) introduces significant architectural improvements that give you better control over each step of your connection and purchase flow. While migration requires that you make significant updates to your existing IAP implementation, this guide provides step-by-step instructions and code samples to help you transition.
Overview of changes
The following table summarizes the changes in IAP v5 and what you need to update in your implementation. For more information on the benefits of upgrading, refer to the Why you should upgrade to Unity In-App Purchasing (IAP) v5.x support article.
Change | Required update |
|---|---|
| Initialization is split into separate async calls for store connection, product fetching, and purchase fetching. | Replace UnityPurchasing.Initialize() |
| Replace ConfigurationBuilder |
| Store Extensions are replaced with Store Extended Services. | Replace ConfigurationBuilder |
| Replace IDetailedStoreListener and IStoreListener |
| Replace IStoreController |
The | Replace purchase flow |
| Restore transactions |
Entitlement checks are now event-based, using | Replace entitlement checks |
Apple App Store receipt validation is deprecated. Google Play receipt validation now uses | Update receipt validation |
| Update Codeless IAP |
Replace UnityPurchasing.Initialize()
As of IAP v5, you can initialize the Unity IAP package with greater flexibility. You can connect to the store, fetch products, and handle purchases independently and asynchronously. This approach can help you identify and resolve issues that might block a successful initialization.
Follow these steps to replace the behavior of :
UnityPurchasing.Initialize()- Call and await :
StoreController.Connect()- When this call completes, IAP is connected to your current app store.
- Call :
FetchProducts()- You can add products to an instance of , similar to adding products to the
CatalogProvider. You can also pass a list ofConfigurationBuildertoProductDefinitionsorProductService.FetchProducts(). For more information, refer to Code sample of new initialization process.StoreController.FetchProducts() - Your event handler is called when the request has successfully completed. If no specified products could be fetched,
OnProductsFetchedis invoked.OnProductsFetchFailed
- You can add products to an instance of
- Call after your products have been successfully fetched:
FetchPurchases()- Your event handler is called when the request has successfully completed. The
OnPurchasesFetchedobject contains a filterable collection of all deferred, pending, and completed orders returned by the app store. On failure,Ordersis invoked.OnPurchasesFetchFailed
- Your
Code sample of new initialization process using StoreController
The following example shows how to initialize IAP v5:
StoreController m_StoreController; async void InitializeIAP() { m_StoreController = UnityIAPServices.StoreController(); m_StoreController.OnPurchasePending += OnPurchasePending; await m_StoreController.Connect(); m_StoreController.OnProductsFetched += OnProductsFetched; m_StoreController.OnPurchasesFetched += OnPurchasesFetched; var initialProductsToFetch = new List<ProductDefinition> { new(goldProductId, ProductType.Consumable), new(diamondProductId, ProductType.Consumable) }; m_StoreController.FetchProducts(initialProductsToFetch); }void OnProductsFetched(List<Product> products) { // Handle fetched products m_StoreController.FetchPurchases(); } void OnPurchasesFetched(Orders orders) { // Process purchases, for example, check for entitlements from completed orders }
Replace IDetailedStoreListener and IStoreListener
IAP v5 no longer requires an implementation of or for handling purchases or initialization. Replace functionality previously handled by by attaching event handlers to , or to the individual , , and services.
IDetailedStoreListenerIStoreListenerIDetailedStoreListenerStoreControllerProductServicePurchaseServiceStoreServiceFor an example of adding event handlers to , refer to Code sample of new initialization process.
StoreControllerTo migrate, replace each function with the following:
IDetailedStoreListener
| IAP v5 replacement |
|---|---|
| Add an event handler to |
| Continue execution after |
| Add event handlers to |
| Add an event handler to |
| Add an event handler to |
Replace ConfigurationBuilder
Replace the following functions with the following behaviours:
ConfigurationBuilder
| IAP v5 replacement |
|---|---|
| Rather than calling |
| You can find functionality in |
Replace IStoreController
Replace with . exposes IAP functionality similar to , but you can fetch by calling at any time.
IStoreControllerStoreControllerStoreControllerIStoreListenerStoreControllerUnityIAPServices.StoreController()Replace these methods from with the following methods from :
IStoreControllerStoreControllerIStoreController | StoreController | Notes |
|---|---|---|
| | |
| | |
| | |
| | |
Replace purchase flow
To initiate a purchase, call or on or . To confirm a pending purchase, call or with the you want to confirm. For more information about the purchase flow, refer to Purchases.
Purchase()PurchaseProduct()StoreControllerPurchaseServiceStoreController.ConfirmPurchase(PendingOrder)PurchaseService.ConfirmPurchase(PendingOrder)PendingOrderIn IAP v4 and earlier, the method automatically handled all purchase events. In IAP v5, purchase handling uses the following callbacks:
ProcessPurchase- : Called for new purchases.
OnPurchasePending - : Called for restored purchases.
OnPurchasesFetched
Use your existing logic within both of these new callbacks.
ProcessPurchaseRestore transactions
RestoreTransactionsStoreControllerPurchaseServiceConfirmed purchases are automatically restored when you call or through calling .
FetchPurchases()CheckEntitlement()Replace entitlement checks
To check a user's entitlement to a product, IAP relies on events. You can check the entitlements for all your fetched products by calling and handling the event. To check the entitlement for a single product, call and handle the event.
FetchPurchasesOnPurchasesFetchedCheckEntitlementOnCheckEntitlementFetch additional products
Call on either or with a list of additional products to trigger your event handler. Alternatively, you can call on a instance. For an example of fetching products via , refer to Code sample of new initialization process. For an example of calling on , refer to Create a catalog in the Editor.
FetchProductsStoreControllerProductServiceOnPurchasesFetchedFetchProductsCatalogProviderProductServiceFetchProductsCatalogProviderReceipt validation
Receipt validation for Apple App Store receipts has been deprecated. Receipt validation is supported only for Google Play Store. To fetch receipts, use from instead of .
Order.Info.ReceiptOrderProduct.ReceiptCodeless IAP specifics
Replace the following function with the following:
CodelessIAPStoreListener
| IAP v5 replacement |
|---|---|
| |