> 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-sdk-internal-beta-testing-unity-native-integration/unity-sdk-internal-beta-testing-introduction/integration-guide/register-callback-events.md).

# Register Callback Events

After adding the Coda SDK Prefab to your scene, you need to register to the callback events.

**Event Listeners**

| Event Name          | Description                                           |
| ------------------- | ----------------------------------------------------- |
| `OnPurchaseSuccess` | Triggered when a payment completes successfully.      |
| `OnPurchaseFailed`  | Triggered when a payment fails, with error details.   |
| `OnPriceChanged`    | Triggered when price updates (e.g., tax adjustments). |

**Two ways to register listeners**:

* **Inspector Method** —&#x20;

1. Create and attach your handler script to an empty GameObject, then link its methods to the events of *PaymentSDK Script* attached to the Coda prefab(Added to Hierarchy in the previous step) using the Inspector Tab. You can create the handler script similar to the following example script:

<pre class="language-csharp"><code class="lang-csharp"><strong>public class TestCodaSDKCallbacksHandler : MonoBehaviour
</strong>{
    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);
    }
}
</code></pre>

{% hint style="info" %}
You can checkout a step-by-step video walkthrough how to register event listeners through Inspect window [here](https://drive.google.com/file/d/1grkox0v90VaWPL_1wEjo-RTQAv_wMKqv/view?usp=drive_link).
{% endhint %}

{% hint style="warning" %}
While registering event listeners via Inspector as shown in the video, choose OnPurchaseSuccess, OnPurchaseFailed, OnPriceChanged functions listed under Dynamic params to receive the data from response, not the one listed under static params.
{% endhint %}

2. 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:

<div data-full-width="false"><figure><img src="/files/WS6h71ikEyIsPqJX73BS" alt="" width="375"><figcaption></figcaption></figure></div>

* **Programmatic Method** — Create and attach your handler script to an empty GameObject, implement `Coda.IPaymentEventListener`, and register the event listeners inside your handler script as shown in the following Example:

  ```csharp
  using Coda;

  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);
      }
  }
  ```

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>For best practices, <code>AddListener</code> should be called in either <code>Start()</code> or <code>Awake()</code>, and <code>RemoveListener</code> should be called in <code>OnDestroy()</code></p></div>

  <div data-gb-custom-block data-tag="hint" data-style="danger" class="hint hint-danger"><p><code>AddListener</code> should be called only once to avoid an event listener getting called multiple times for a single event.</p></div>

{% hint style="info" %}
If you are not aware of how you can "Create and attach your handler script to an empty GameObject", checkout a step-by-step video walkthrough how to register event listeners programmatically [here](https://drive.google.com/file/d/1-PyDoQbMluuEHsJZaCLrUVDjTLJGBR96/view?usp=drive_link).
{% endhint %}


---

# 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-sdk-internal-beta-testing-unity-native-integration/unity-sdk-internal-beta-testing-introduction/integration-guide/register-callback-events.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.
