Fulfill purchases through the SDK
Retrieve purchase information and determine purchase status for products bought by players.
Read time 2 minutesLast updated 11 hours ago
Unity IAP fetches purchase information from the store so your application can recognize and fulfill what players have bought. This ensures that your game can deliver content or entitlements to users based on their purchase history, even if they bought items outside your app or on another device.
A purchase is represented as an
OrderOrderRetrieve purchases
Purchases made by the user can be retrieved from the store. However, consumable products must be tracked by your application after they are consumed, because stores don't return consumables that have already been fulfilled. For non-consumable products and subscriptions, the store accurately returns these purchases when you callFetchPurchasesCheckEntitlementDetermine purchase status
You can determine the status of a purchase in two ways:- Use to determine if the
Orderis aOrder,PendingOrder,ConfirmedOrderorDeferredOrder.FailedOrder - Use to receive
CheckEntitlement, which returnsEntitlementStatus,EntitledButNotFinished,EntitledUntilConsumed,FullyEntitledorNotEntitled.Unknown
Purchase attributes
Attribute | Description |
|---|---|
| A unique identifier for the purchase. |
| The purchased product. |
| The quantity of the product purchased. |
| Receipt data for validating the purchase with the store. |
Purchase states
State | Description |
|---|---|
| The purchase has been paid but not yet fulfilled. |
| The purchase has been fulfilled and acknowledged. |
| The purchase failed due to an error. |
| The purchase is waiting for payment. |
Process purchases
TheOnPurchasePendingOnPurchasePendingOnPurchasePending// Handle restore on initializationprivate async void Start(){ // Setup, e.g. add listeners to your StoreController... m_StoreController.OnPurchasePending += OnPurchasePending; m_StoreController.OnPurchasesFetched += OnPurchasesFetched; await m_StoreController.Connect(); // Fetch previous purchases (includes confirmed orders) m_StoreController.FetchPurchases();}// Handle new purchases and pending transactionsprivate void OnPurchasePending(PendingOrder order){ ProcessPurchase(order);}// Handle fetched purchases (includes previously confirmed orders)private void OnPurchasesFetched(Orders orders){ foreach (var confirmedOrder in orders.ConfirmedOrders) { if (confirmedOrder.CartOrdered.Items().FirstOrDefault()?.Product.definition.type != ProductType.Consumable) { // Mark non-consumable and subscription products as entitled on fetch, as they only need to be granted once MarkAsEntitled(confirmedOrder.CartOrdered.Items().FirstOrDefault().Product); } }}// Your ProcessPurchase logicprivate void ProcessPurchase(PendingOrder order){ foreach (var product in order.CartOrdered.Items()) { // Grant product GrantProduct(product); } // Confirm the order to finalize the transaction m_StoreController.ConfirmPurchase(order);}
Purchase acknowledgement and reliability
Unity IAP requires you to explicitly acknowledge purchases to ensure that purchases are reliably fulfilled, even during network outages or application crashes. If a purchase is paid for but not fulfilled, Unity IAP delivers the purchase to your application the next time it initializes. This process prevents purchases from being lost when the purchase flow is interrupted or when purchases are completed while the application is offline. After successfully fulfilling a purchase, callConfirmPurchasePendingOrderAcknowledge purchases persisted to the cloud
If you are saving consumable purchases to the cloud, you must callConfirmPurchasePendingRestore purchases
Enable users to regain access to previously owned products and subscriptions when they reinstall your app or switch devices. Understand how IAP retrieves a record of entitlements and grants access:- When a user reinstalls the application, Unity IAP restores owned products on the first call.
StoreController.FetchPurchases() - IAP invokes the listener with an
OnPurchasesFetchedobject that includes all purchases (all states).Orders - If the setting is set to
PurchaseService.ProcessPendingOrdersOnPurchasesFetched, IAP invokes thetruelistener for each unfulfilled purchase.OnPurchasePending - Subsequent calls in the same session don't trigger
FetchPurchases()for orders that you have already seen in the same session.OnPurchasePending
Use server-side validation alongside the SDK
You can make calls to the backend API while using the Unity In-App Purchases (IAP) SDK. This hybrid approach allows your server to act as the authority by validating transactions directly against Unity’s records before you reward the player. Unlike the standard backend API method, this doesn't require you to expose a public endpoint for webhooks, which can reduce your attack surface and simplify your server infrastructure.Client-side implementation
When a purchase is initiated, the SDK returns aPendingOrderOrderInfo.TransactionIdBackend authentication
Use the Key ID and Secret Key from your Service Account to perform a Token Exchange to receive a stateless Bearer token. For more information, refer to how to Authenticate an API using a stateless token.
Refer to the following token exchange endpoint:
POST https://services.api.unity.com/auth/v1/token-exchange?projectId={projectId}&environmentId={envId}
Validate the order
Once your backend has a Bearer token, query the Unity IAP Order service with the following request:TheGET https://iap.services.api.unity.com/v1/projects/{projectId}/environments/{envId}/orders/{orderId}
orderIdTransactionId- must be
statusbefore you grant the items.paid - contains the underlying Stripe Checkout Session ID, if you need it to cross-reference in the Stripe Dashboard.
paymentProviderResourceId
Fulfill and complete the order
Refer to the following workflow to complete orders with server authority:- Verify that the status is paid and the productSku matches the expected item.
- Update your player's database with the new entitlement.
- After the backend returns a success code to the client, use one of the following methods to finalize the transaction in the Unity IAP system:
- Update the order directly with the API
- Make the client call .
m_StoreController.ConfirmPurchase(order)