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 5 minutesLast updated a day 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 has 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 ofIDetailedStoreListenerIStoreListenerIDetailedStoreListenerStoreControllerProductServicePurchaseServiceStoreServiceStoreController
To migrate, replace each
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 followingConfigurationBuilder
| IAP v5 replacement |
|---|---|
| Rather than calling |
| You can find functionality in |
Replace IStoreController
ReplaceIStoreControllerStoreControllerStoreControllerIStoreListenerStoreControllerUnityIAPServices.StoreController()IStoreControllerStoreControllerIStoreController | StoreController | Notes |
|---|---|---|
| | |
| | |
| | |
| | |
Replace purchase flow
To initiate a purchase, callPurchase()PurchaseProduct()StoreControllerPurchaseServiceStoreController.ConfirmPurchase(PendingOrder)PurchaseService.ConfirmPurchase(PendingOrder)PendingOrderProcessPurchase- : Called for new purchases.
OnPurchasePending - : Called for restored purchases.
OnPurchasesFetched
ProcessPurchaseRestore transactions
RestoreTransactionsStoreControllerPurchaseServiceFetchPurchases()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 callingFetchPurchasesOnPurchasesFetchedCheckEntitlementOnCheckEntitlementFetch additional products
CallFetchProductsStoreControllerProductServiceOnPurchasesFetchedFetchProductsCatalogProviderProductServiceFetchProductsCatalogProviderReceipt validation
Receipt validation for Apple App Store receipts has been deprecated. Receipt validation is supported only for Google Play Store. To fetch receipts, useOrder.Info.ReceiptOrderProduct.ReceiptCodeless IAP specifics
Replace the followingCodelessIAPStoreListener
| IAP v5 replacement |
|---|---|
| |