Codeless IAP button configuration
Set up and use Codeless IAP buttons and IAP listeners to enable in-app purchases and restore purchases without writing complex code.
Read time 3 minutesLast updated 5 hours ago
The Codeless IAP button makes it easy to set up in-app purchases and restore buttons with minimal scripting. Use the IAP button to add purchase and restore functions directly to your UI buttons. This approach provides a simple way for users to buy or restore products in your app.
Use OnProductFetched to update product details
Unity IAP triggers theOnProductFetchedOnProductFetchedpublic class IAPButtonView : MonoBehaviour{ [SerializeField] TMP_Text title; [SerializeField] TMP_Text price; public void OnProductFetched(Product product) { if (title != null) { title.text = product.metadata.localizedTitle; } if (price != null) { price.text = product.metadata.localizedPriceString; } }}
IAP listeners
Codeless IAP normally sends successful and failed purchase events to an active IAP button component in your scene. However, there are situations where using an IAP button is not practical. For example, if purchase fulfillment is interrupted and not completed, Unity IAP attempts to process the purchase again the next time it initializes (usually immediately after the app starts, when IAP buttons might not yet be initialized). In these cases, you can use the IAP Listener component. The IAP listener receives any purchase events that can't be handled by an IAP button, ensuring that all purchases are processed correctly. Add the IAP listener to your scene to handle these events automatically.
To add an IAP listener, follow these steps:
- In the Unity Editor, select Services > In-App Purchasing > Create IAP Listener.
- Follow the steps for writing a purchase fulfillment script as a GameObject component. Refer to Purchase fulfillment for more information.
- Select the IAP Listener from the Hierarchy list and locate its IAP Listener script component in the Inspector. Then, select + to add a function for On Purchases Fetched (Orders) and On Order Pending (PendingOrder).
- On Order Pending handles all new transactions, and is invoked for individual s.
PendingOrder - On Purchases Fetched handles all existing transactions, and is invoked with an object containing all active Pending, Confirmed, and Deferred transactions.
Orders
- On Order Pending handles all new transactions, and is invoked for individual
- Drag the GameObject with the purchase fulfillment script onto the event field in the component’s Inspector, then select your function from the dropdown menu.
Restore button
Some app stores, including the Apple App Store, require apps to have a Restore button. Codeless IAP provides an easy way to implement a restore button in your app. To add a Restore button, follow these steps:- Add an IAP Button to your Scene (Services > In-App Purchasing > Create IAP Button).
- With your IAP Button selected, locate its IAP Button component in the Inspector.
- In the Button Type drop-down menu, select Restore. The Inspector window updates to display only the fields relevant for the Restore button.
- Optional: Select the plus (+) button to add a script to .
On Transactions Restored (Boolean, String)- Drag the GameObject with the restore transactions script onto the event field in the component’s Inspector, then select the function from the dropdown.
When a user selects this button at runtime, it calls the purchase restoration API for the current store. On the Apple App Store, this triggers Apple’s built-in restore or sync flows. On Google Play, restoration is a part of IAP initialization. Additional restore calls during runtime rarely yields results. Unity IAP always invokes thepublic void OnTransactionsRestored(bool success, string? error){ Debug.Log($"TransactionsRestored: {success} {error}");}
OnTransactionsRestoredOnPurchaseFetchedOnPurchasesFetchedpublic void OnOrderPending(PendingOrder order){ foreach (var cartItem in order.CartOrdered.Items()) { var product = cartItem.Product; // grant purchase... }}public void OnPurchasesFetched(Orders existingOrders){ foreach (var order in existingOrders.ConfirmedOrders) { foreach (var cartItem in order.CartOrdered.Items()) { var product = cartItem.Product; // grant access... } }}