Documentation

Support

In-App Purchasing

In-App Purchasing

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 the
OnProductFetched
event when it retrieves product information from the app stores. Make sure to update all text related to the product in the UI with this event such as title, description and price.
An example of the
OnProductFetched
script:
public 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:
  1. In the Unity Editor, select Services > In-App Purchasing > Create IAP Listener.
  2. Follow the steps for writing a purchase fulfillment script as a GameObject component. Refer to Purchase fulfillment for more information.
  3. 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
      PendingOrder
      s.
    • On Purchases Fetched handles all existing transactions, and is invoked with an
      Orders
      object containing all active Pending, Confirmed, and Deferred transactions.
  4. 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:
  1. Add an IAP Button to your Scene (Services > In-App Purchasing > Create IAP Button).
  2. With your IAP Button selected, locate its IAP Button component in the Inspector.
    1. In the Button Type drop-down menu, select Restore. The Inspector window updates to display only the fields relevant for the Restore button.
  3. Optional: Select the plus (+) button to add a script to
    On Transactions Restored (Boolean, String)
    .
    1. Drag the GameObject with the restore transactions script onto the event field in the component’s Inspector, then select the function from the dropdown.
public void OnTransactionsRestored(bool success, string? error){ Debug.Log($"TransactionsRestored: {success} {error}");}
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 the
OnTransactionsRestored
method on the Restore IAP Button with the result and the associated error message if the restore fails.
When transactions are successfully restored, Unity IAP calls the
OnPurchaseFetched
callback on the IAP Button linked to the product, or the
OnPurchasesFetched
callback on the IAP Listener component.
public 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... } }}