> For the complete documentation index, see [llms.txt](https://docs.coda.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.coda.co/codapay/in-app-payments/unity-native-integration/integration-guide.md).

# Integration Guide

## Integrate with the SDK

Once the SDK is configured, you can embed it into your game by following steps:

* Add Coda SDK Prefab
* Handle the payment result

### Step 1: Add Coda SDK Prefab <a href="#add-coda-sdk-prefab" id="add-coda-sdk-prefab"></a>

Adding the Coda SDK Prefab into your game allows the SDK to initialize at runtime. One way to do this is by adding the Prefab to a scene.

{% hint style="danger" %}
Make sure to add the Coda SDK Prefab to the first scene that loads when the game starts
{% endhint %}

Once the required scene is open, you can view objects in the hierarchy panel and add the Coda SDK Prefab using either of the following methods:

* **Using the Coda menu:** `Coda → Create Prefab`<br>

  <figure><img src="/files/L2SZcAsGSUAzPyvveEpf" alt="" width="318"><figcaption></figcaption></figure>
* **Drag from the Project panel into the Hierarchy panel:** Navigate to `Assets > Plugins > Coda > Prefabs`\
  &#x20;

  <figure><img src="/files/G8HZQa3EJWaSdlNJ6k0S" alt="" width="375"><figcaption></figcaption></figure>

### Step 2: Handle the Payment Result

After adding the Coda SDK Prefab to your scene, you need to handle payment result events.\
There are two ways to register event listeners to the Coda SDK:

* Through the Inspector window
* Programmatically

#### Register your event listeners through the Inspector Window <a href="#register-your-event-listeners-through-inspector-window" id="register-your-event-listeners-through-inspector-window"></a>

To register event listeners via the Inspector window:

1. **Create a script** to handle callbacks from the event listeners. For example:

```java
public class TestCodaSDKCallbacksHandler : MonoBehaviour
{
    public void OnPurchaseSuccess(string orderId) {
        Debug.Log("Success: " + orderId);
    }
    public void OnPurchaseFailed(string orderId, string error) {
        Debug.Log("Failed: " + error);
    }
    public void OnPriceChanged(double updatedPrice) {
        Debug.Log("Price Updated: " + updatedPrice);
    }
}
```

2. **Create an Empty GameObject in the scene:** Right-click in the “Hierarchy” window → Select Create Empty. Rename the object and attach your script to it.\
   ![](/files/iivdlqtzElGDf9KYZkld)

<div align="left"><figure><img src="/files/EZyH9btFxGmoMO3CGeYx" alt="" width="375"><figcaption></figcaption></figure></div>

3. **Select the Coda SDK Prefab** in your scene (Hierarchy panel). In the **Inspector** panel, you will see the `On Payment Success`, `On Payment Failure`, and `On Price Update` events under the **PaymentSDK** script. This is where you register your GameObject methods as callbacks.

<figure><img src="/files/FqbfdutntoNTIbs48UZ9" alt="" width="563"><figcaption></figcaption></figure>

4. **Click the “+” button** under the `On Payment Success` event to add a callback slot.

<figure><img src="/files/4SMY0BbmMfJuGQLEYmRc" alt="" width="375"><figcaption></figcaption></figure>

5. **Drag your GameObject** from the Hierarchy panel into the callback slot.

<figure><img src="/files/MYbGw1h8VhLWrEVjGqoZ" alt="" width="375"><figcaption></figcaption></figure>

6. **Select the method** to be called when payment is successful.

<figure><img src="/files/ss8hR5UTkg0DJ5raHnIc" alt="" width="375"><figcaption></figcaption></figure>

Register event listeners for failure and price update events in the same way. Once registered, your Inspector window for the Coda prefab should look like this:

<figure><img src="/files/WS6h71ikEyIsPqJX73BS" alt="" width="375"><figcaption></figcaption></figure>

#### Register your event listeners programmatically

You can also register event listeners programmatically:

1. **Implement** the `Coda.IPaymentEventListener` interface in your script.
2. **Register** event listeners when your script is loaded:

```java
Coda.PaymentEvents.AddListener(this)
```

3. **Unregister** event listeners when your script is unloaded:

```java
Coda.PaymentEvents.RemoveListener(this)
```

{% hint style="warning" %}
`AddListener` should be called in either `Start` or `Awake`, and `RemoveListener` in `onDestroy`
{% endhint %}

{% hint style="danger" %}
Only call `AddListener` once to avoid duplicate triggers for a single event
{% endhint %}

Example implementation:

```java
public class TestCodaSDKCallbacksHandler : MonoBehaviour, Coda.IPaymentEventListener
{
    void Awake() {
        Coda.PaymentEvents.AddListener(this);
    }
    void OnDestroy() {
        Coda.PaymentEvents.RemoveListener(this);
    }
    public void OnPurchaseSuccess(string orderId)
    {
        Debug.Log("Success: " + orderId);
    }
    public void OnPurchaseFailed(string orderId, string error) {
        Debug.Log("Failed: " + error);
    }
    public void OnPriceChanged(double updatedPrice) {
        Debug.Log("Price Updated: " + updatedPrice);
    }
}
```

You can check full list of available event listeners [here](/references/contact-us.md).

## Initiate a Payment

Once everything is set up, you can initiate a payment flow.\
A payment flow can be triggered from anywhere in your game—typically when a user taps a **Buy**, **Purchase**, or **Upgrade** button.

To start the payment flow, call `Coda.PaymentSDK.InitiatePayment()`&#x20;

You can use the same script that handles callbacks or create a new one (e.g., `TestCodaSDKHandler`) with the `InitiatePayment` function, then attach it to an Empty GameObject in the scene (see step 2).

This launches the payment UI, allowing users to complete their purchase through Codapay’s network.

Example code:

```java
public class TestCodaSDKHandler : MonoBehaviour
{
    public void OnPurchaseButtonClick()
    { 
        Coda.PaymentSDK.InitiatePayment(
        orderId: "order1234",               // Required: Unique transaction ID
        price: 1000,                        // Required: In minor currency units
        countryCode: Coda.CountryCode.US,   // Required: ISO country code enum
        currencyCode: Coda.CurrencyCode.USD,// Required: ISO currency code enum
        userId: "user456",                  // Required: Unique user identifier
        // Optional
        itemName: "Special Sword",
        itemCode: "sword_001",
        dynamic_descriptor: "MYGAME",
        webhookUrl: "https://yourdomain.com/webhook",
        returnUrl: "https://yourgame.com/success");
    }
}
```

You can check full list of parameters [here](/references/contact-us.md).

If you haven’t built the UI yet, create a basic Purchase/IAP screen.\
Call `OnPurchaseButtonClick()` on the `OnClick` event of your Buy/Purchase/Upgrade button.

<figure><img src="/files/qLIZFEpCq33LEKUr2hp4" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.coda.co/codapay/in-app-payments/unity-native-integration/integration-guide.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
