Upgrade to IAP version 5
Follow this guide to migrate from Unity In-App Purchasing version 4 (or earlier) to version 5.
Read time 4 minutesLast updated 15 hours ago
IAP version 5 (v5) is a major rework of the IAP package, and migration requires significant code changes.
Overview of changes
- Configuration and initialization flow
- Initialization has been separated into multiple calls to cover store connection, fetching products, and fetching purchases.
- The configuration builder has been removed.
- Products can be fetched using a list or a , which can be defined at any time.
CatalogProvider - Store Extensions and configurations have been replaced with Store Extended Services.
- Changes to event handling and callbacks
- has been replaced with optional event handlers.
IDetailedStoreListener
Replace UnityPurchasing.Initialize()
As of IAP v5, you can initialize the Unity IAP package with greater flexibility. Connect to the store, fetch products, and handle purchases independently and asynchronously. This approach helps you identify and resolve issues that might block a successful initialization.
UnityPurchasing.Initialize()
Follow these steps to replace
UnityPurchasing.Initialize()- Call and await
StoreController.Connect()- When this call completes, you may assume that 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,
OnProductsFetchedwill be 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,Orderswill be invoked.OnPurchasesFetchFailed
- Your
Code sample of new initialization process using StoreController
The following is an example of the new initialization process:
StoreControllerStoreController 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, e.g. check for entitlements from completed orders }
Replace IDetailedStoreListener
and IStoreListener
IAP v5 no longer requires an implementation of IDetailedStoreListenerIStoreListenerIDetailedStoreListenerIStoreListenerIDetailedStoreListenerStoreControllerProductServicePurchaseServiceStoreServiceStoreController
During migration, replace the
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 ConfigurationBuilderConfigurationBuilder
| IAP v5 replacement |
|---|---|
| Rather than calling |
| Functionality in |
Replace IStoreController
Replace IStoreControllerIStoreControllerStoreControllerStoreControllerIStoreListenerUnityIAPServices.StoreController()IStoreControllerStoreControllerIStoreController | StoreController | Notes |
|---|---|---|
| | |
| | |
| | |
| | |
Replace purchase flow
To initiate a purchase, callPurchase()PurchaseProduct()StoreControllerPurchaseServiceStoreController.ConfirmPurchase(PendingOrder)PurchaseService.ConfirmPurchase(PendingOrder)PendingOrderUnity IAP version 4 and earlier
- The method automatically handles all purchase events.
ProcessPurchase
Unity IAP version 5 and later
Purchase handling has the following callbacks:- : 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 |
|---|---|
| |