Set up In-App Purchasing
Learn how to initialize the Unity IAP package, configure essential settings, and verify setup to start processing in‑app purchases across supported platforms.
Read time 4 minutesLast updated 5 hours ago
Before you use Unity In-App Purchasing (IAP), initialize the IAP package. If you want to use Unity Analytics or Unity Authentication in your project, you need to initialize Unity Gaming Services (UGS) first before initializing IAP. Refer to the Initialize Unity Gaming Services section below for a how-to.
Initialize Unity Gaming Services
CallUnityServices.InitializeAsync()TaskInitialize In-App Purchasing
Prerequisites
Complete the steps in Define your products for the products you want to offer for purchase.Initialization steps
Initialization consists of the following steps:- Getting a for your app store.
StoreController - Attaching event listeners to the .
StoreController - Connecting to your app store.
- Fetching products from your app store.
- Fetching purchases from your app store.
using System.Collections.Generic;using UnityEngine.Purchasing;public class MyIAPManager{ private StoreController m_StoreController; public MyIAPManager() { // Define products var catalogProvider = new CatalogProvider(); catalogProvider.AddProduct("100_gold_coins", ProductType.Consumable, new StoreSpecificIds() { {"100_gold_coins_google", GooglePlay.Name}, {"100_gold_coins_mac", MacAppStore.Name} }); // Get StoreController m_StoreController = UnityIAPServices.StoreController(); // Add event listeners m_StoreController.OnStoreDisconnected += OnStoreDisconnected; m_StoreController.OnProductsFetched += OnProductsFetched; m_StoreController.OnProductsFetchFailed += OnProductsFetchFailed; m_StoreController.OnPurchasesFetched += OnPurchasesFetched; m_StoreController.OnPurchasesFetchFailed += OnPurchasesFetchFailed; // Connect to store m_StoreController.Connect().ContinueWith(_ => { // Fetch products from store catalogProvider.FetchProducts( list => m_StoreController.FetchProducts(list) ); }); } /// <summary> /// Invoked when connection is lost to the current store, or on a Connect() failure. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnStoreDisconnected(StoreConnectionFailureDescription failure) { } /// <summary> /// Invoked with products that are successfully fetched. /// </summary> /// <param name="products">Products successfully returned from the app store.</param> private void OnProductsFetched(List<Product> products) { // Fetch purchases for successfully retrieved products m_StoreController.FetchPurchases(); } /// <summary> /// Invoked when an attempt to fetch products has failed or when a subset of products failed to be fetched. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnProductsFetchFailed(ProductFetchFailed failure) { } /// <summary> /// Invoked when previous purchases are fetched. /// </summary> /// <param name="orders">All active pending, completed, and deferred orders for previously fetched products.</param> private void OnPurchasesFetched(Orders orders) { } /// <summary> /// Invoked when an attempt to fetch previous purchases has failed. /// </summary> /// <param name="failure">Information regarding the failure.</param> private void OnPurchasesFetchFailed(PurchasesFetchFailureDescription failure) { } /// <summary> /// Invoked when a purchase needs to be processed and fulfilled. /// </summary> /// <param name="order">The order awaiting fulfillment.</param> private void OnPurchasePending(PendingOrder order) { }}
Get a StoreController
StoreControllerStoreControllerAttach event handlers to the StoreController
For your store to function correctly, attach handlers to these events:- OnStoreDisconnected
- OnProductsFetched
- OnProductsFetchFailed
- OnPurchasesFetched
- OnPurchasesFetchFailed
- OnPurchasePending
StoreControllerConnect to your app store
Call StoreController.Connect to connect to the app store. The returnedTaskOnStoreDisconnectedFetch products
To validate that your products are available for purchase, call StoreController.FetchProducts. Upon success, the
OnProductsFetchedOnProductsFetchFailedFetchProductsFetchProductsOnPurchasePendingFetchProductsGetProducts
GetProductsFetchProductsFetchProductsFetchProductsGetProductsFetch purchases
CallStoreController.FetchPurchasesPendingOrdersConfirmedOrdersDeferredOrdersFetchPurchasesOnPurchasesFetchedOrdersOnPurchasesFetchFailedOnPurchasePendingGetPurchases
GetPurchasesFetchPurchasesFetchPurchasesGetProductsFetchProductsFetchPurchasesGetPurchases