> For the complete documentation index, see [llms.txt](https://docs.akinon.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akinon.com/tutorials/commerce/how-to-configure-gift-card.md).

# How to Configure Gift Card Payment

This document provides a step-by-step guide on how to configure and integrate the **Gift Card Payment Flow** in Commerce. The Gift Card Payment Flow connects Commerce to an external gift card provider through a standardised extension interface, allowing customers to redeem gift cards during checkout — with optional OTP verification support.

This integration is particularly useful for providers that manage their own card balance logic, require OTP-based identity verification, or need idempotent refund handling.

## <mark style="color:red;">1. Overview</mark> <a href="#id-1-overview" id="id-1-overview"></a>

When a customer applies a gift card during checkout, Commerce communicates with the configured provider through a standardised interface. The provider handles card balance queries, optional OTP verification, and payment transactions. Commerce orchestrates the flow and applies the result to the basket.

Key characteristics of this flow:

* **Provider abstraction:** Each provider implements its own gift card logic; Commerce communicates through a fixed interface without provider-specific changes.
* **OTP support:** Providers that require OTP verification are fully supported. The user must verify their identity before the card can be charged.
* **Multi-card support:** Multiple gift cards can be applied to a single order when enabled.
* **Idempotent refunds:** Refund operations are safe to retry; the provider is expected to handle duplicate requests gracefully.
* **Automatic void on failure:** If an order cannot be created after a successful gift card charge, Commerce schedules an automatic void to clean up the orphaned transaction.

## <mark style="color:red;">2. Setup</mark> <a href="#id-2-setup" id="id-2-setup"></a>

Configuring the gift card feature requires two steps: setting the behavioural parameters via the `GIFT_CARD_CONFIG` dynamic setting, and registering the gift card provider through the Omnitron provider management screen.

### <mark style="color:red;">2.1. Configuring GIFT\_CARD\_CONFIG</mark>

1. Log in to **Omnitron** using your own credentials.

   <figure><img src="https://3333414532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbwGN7KwvYi0iLbjtnXz%2Fuploads%2Fgit-blob-5cd5a2a1310f907019c50287320e196b352392d7%2Flogin-omnitron.png?alt=media" alt=""><figcaption></figcaption></figure>
2. Navigate to **Sales Channels → Sales Channel Settings → Dynamic Settings**.

   <figure><img src="https://3333414532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbwGN7KwvYi0iLbjtnXz%2Fuploads%2Fgit-blob-0eb1b599ab81d87c69b562147edc34c625ab515a%2Ftax-dynamic-settings-1.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Search for **GIFT\_CARD\_CONFIG** in the search box and click on it in the list.

   <figure><img src="https://3333414532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbwGN7KwvYi0iLbjtnXz%2Fuploads%2Fgit-blob-2ea978215923fb18248539e168aa8240cef87144%2Fgift-card-dynamic-settings-2.png?alt=media" alt=""><figcaption></figcaption></figure>
4. Update the configuration on the edit screen with the following fields.

   <figure><img src="https://3333414532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIbwGN7KwvYi0iLbjtnXz%2Fuploads%2Fgit-blob-10dd8c920702f72e2caca4cae2139d553a31ac9a%2Fgift-card-dynamic-settings-3.png?alt=media" alt=""><figcaption></figcaption></figure>

| Field                  | Description                                                                                                                                            |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `multi_card_enabled`   | Set to `true` to allow more than one gift card per order. When `false`, a second card is rejected if one is already applied.                           |
| `max_amount_per_order` | Maximum total gift card amount per order (in the order's currency). `0` means unlimited.                                                               |
| `balance_cache_ttl`    | Duration in seconds that a queried card balance is kept in cache. Should be long enough to cover the full application flow including OTP verification. |
| `otp_resend_cooldown`  | Minimum seconds a user must wait before requesting a new OTP.                                                                                          |
| `void_task_countdown`  | Delay in seconds before Commerce automatically voids an orphaned gift card charge when order creation fails.                                           |

**Example configuration:**

```json
{
  "multi_card_enabled": true,
  "max_amount_per_order": 500,
  "balance_cache_ttl": 3600,
  "otp_resend_cooldown": 120,
  "void_task_countdown": 3600
}
```

{% hint style="warning" %}
Set `balance_cache_ttl` high enough to cover the entire gift card application flow — from card entry through OTP verification to amount confirmation. A value lower than the expected user interaction time will cause the reservation to expire and the user will need to re-add the card.
{% endhint %}

### <mark style="color:red;">2.2. Registering the Gift Card Provider</mark>

{% hint style="warning" %}
The Gift Card Providers screen is not yet available in Omnitron. This section will be updated once the screen is released.
{% endhint %}

The gift card provider connection details (service URL and authentication credentials) are managed through the **Gift Card Providers** screen in Omnitron. This is separate from Dynamic Settings.

1. Navigate to **Gift Card Providers** in Omnitron.
2. Click **Add Provider** and fill in the following fields.

| Field          | Description                                             |
| -------------- | ------------------------------------------------------- |
| `resource_url` | Base URL of the gift card provider's extension service. |
| `username`     | Username for HTTP Basic Authentication.                 |
| `password`     | Password for HTTP Basic Authentication.                 |

**Example provider configuration:**

```json
{
  "resource_url": "https://gift-card-service.example.com",
  "config": {
    "auth": {
      "username": "your-username",
      "password": "your-password"
    }
  }
}
```

## <mark style="color:red;">3. How the Flow Works</mark> <a href="#id-3-how-the-flow-works" id="id-3-how-the-flow-works"></a>

### <mark style="color:red;">3.1. Applying a Gift Card (Non-OTP)</mark>

When a customer enters a gift card number, Commerce calls the provider's `check-balance` endpoint. If the provider does not require OTP, the response includes the card balance and a `purchaseToken` directly. The card is reserved in the basket and the customer can proceed to payment.

```
check-balance → card applied to basket
```

### <mark style="color:red;">3.2. Applying a Gift Card (OTP Flow)</mark>

If the provider requires OTP verification, `check-balance` returns `otpRequired: true` and an `otpRef`. Commerce prompts the user to enter the OTP sent to their masked phone number. The user may request the OTP to be resent; Commerce enforces the `otp_resend_cooldown` between consecutive resend requests. Once the user submits the correct OTP, `verify-otp` returns the `purchaseToken` and the card is reserved.

```
check-balance → [send-otp (optional resend)] → verify-otp → card applied to basket
```

{% hint style="info" %}
The card balance and currency may be returned in either `check-balance` or `verify-otp` depending on the provider. If the provider returns `currency`, it must match the order currency — a mismatch causes the transaction to be rejected.
{% endhint %}

### <mark style="color:red;">3.3. Purchase</mark>

When the order is placed, Commerce charges each applied gift card by calling the `purchase` endpoint with the corresponding `purchaseToken`. If multiple gift cards are applied, a separate `purchase` request is sent per card. If any charge fails, all previously successful charges for that order are automatically voided.

{% hint style="info" %}
Gift card reservations with `amount=0` are skipped and no purchase request is sent.
{% endhint %}

### <mark style="color:red;">3.4. Void</mark>

The `void` endpoint is used to reverse a purchase transaction. If the order has not yet been created at the time of the void, `orderNumber` is sent as `null`.

Commerce does not halt the flow on void errors — the void is best-effort. However, the provider is still expected to return a successful response.

If order creation fails after a successful gift card charge (e.g. due to a server error or timeout), Commerce schedules a background void task after `void_task_countdown` seconds to clean up the orphaned transaction automatically.

### <mark style="color:red;">3.5. Refund</mark>

Partial and full refunds are supported. Commerce calls the `refund` endpoint with the original `transactionId`, the refund amount, and the order number. The provider is expected to handle duplicate refund requests idempotently.

## <mark style="color:red;">4. Payment & Balance Behavior</mark> <a href="#id-4-payment-and-balance-behavior" id="id-4-payment-and-balance-behavior"></a>

This section describes how gift cards behave during checkout, payment, and post-order — partial payment, insufficient balance, cancellation/refund, multiple cards, and the errors a customer may see.

### <mark style="color:red;">4.1. Partial Payment (Gift Card + Another Method)</mark>

A gift card is **not** a payment step that hands off to the credit card page in sequence. When a card is applied, Commerce **reserves** it on the basket and lowers the amount left to pay. The remaining amount is then charged on the customer's regular payment method (e.g. credit card) in the same payment step.

The amount still owed after gift cards is:

```
amount_to_pay = order_total − loyalty_amount − gift_card_amount
```

where `gift_card_amount` is the sum of all applied gift card reservations.

* **Partial coverage:** The gift card is charged as a pre-payment alongside the regular payment method. The customer selects a payment option as usual, and only `amount_to_pay` is charged to that method. There is no separate request to "move" the remainder — Commerce computes it and charges the regular method for that figure.
* **Full coverage:** When the applied gift cards cover the entire total (`amount_to_pay = 0`), the order is completed directly on the gift card payment page, with no credit card involved.

{% hint style="info" %}
Checkout page flow: `GiftCardPage → GiftCardOtpPage (OTP providers only) → GiftCardApplyPage`. After the card is applied, control returns to the normal checkout flow; the remainder is paid through the standard payment-option page. Applying a gift card does **not** require a separate handoff request to the payment page.
{% endhint %}

### <mark style="color:red;">4.2. Insufficient Balance</mark>

A gift card whose balance is lower than the requested amount (or lower than the order total) is **not rejected**. The requested amount is automatically **capped** and the card is partially applied. The applied amount is the smallest of:

* the requested amount,
* the card's available balance,
* the amount still left to pay on the order, and
* the remaining headroom under `max_amount_per_order` (when a per-order limit is configured).

The remaining order total is then paid by the regular payment method, as described in the partial payment behavior above. The customer is shown the maximum applicable amount for the card before confirming.

The only case in which a low balance leads to a rejection is when the capped amount comes out as **zero** — for example, when the order is already fully paid or the per-order limit is already reached. In that case the card is refused with the `gift_card_108` error.

### <mark style="color:red;">4.3. Cancellation & Refund</mark>

Gift card charges are reversed through two distinct provider operations — **void** and **refund** — both of which are invoked by the Omnitron backend through the corresponding methods under `omnicore` (the `GiftCardExtensionClient`).

* **Void (automatic cleanup):** If a gift card is charged but the order cannot be created afterwards (for example, a server error or timeout during order creation), Commerce reverses the charge by calling the provider's `void` endpoint. This runs automatically as a background safety-net after the configured `void_task_countdown` seconds (code default `1800`; the example configuration in [section 2](#id-2-setup) sets it to `3600`). If the order is created successfully in the meantime, the charge is linked to the order and the scheduled void becomes a no-op.
* **Refund:** Partial and full refunds are issued through the provider's `refund` endpoint. The provider is expected to handle duplicate refund requests idempotently.

{% hint style="warning" %}
Void reverses a charge for an order that was **never created**. It is a cleanup mechanism, not a customer-initiated cancellation. A successfully created order does not trigger an automatic gift card balance restoration on cancellation or return — any balance return for a completed order is handled through the `refund` operation.
{% endhint %}

### <mark style="color:red;">4.4. Multiple Gift Cards on One Order</mark>

Whether more than one gift card can be applied to a single order is controlled by `multi_card_enabled` in `GIFT_CARD_CONFIG`:

* When `multi_card_enabled` is `false` (the default), applying a second card is rejected with `gift_card_110` once one card is already applied.
* The **same** card cannot be added twice regardless of the setting; a duplicate is rejected with `gift_card_101`.
* `max_amount_per_order` limits the **combined** total of all applied gift cards. Each additional card can only reserve up to the remaining headroom under this limit; `0` means unlimited.

At payment time, each applied card is charged with a separate `purchase` request. If any card's charge fails, all previously successful charges for that order are automatically voided and the payment fails with `gift_card_106`.

### <mark style="color:red;">4.5. Common Errors</mark>

The following errors may be returned during the gift card flow. The message shown to the customer is the user-facing text; the cause column explains when each one occurs.

| Code            | Message                                                           | When it occurs                                                                                                        |
| --------------- | ----------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `gift_card_100` | Gift card not found.                                              | The referenced gift card reservation no longer exists (e.g. removed before an update).                                |
| `gift_card_101` | This gift card has already been added.                            | The same card is applied a second time on the same order.                                                             |
| `gift_card_102` | Gift card currency does not match the order currency.             | The card currency returned by the provider differs from the order currency.                                           |
| `gift_card_103` | Gift card balance session has expired. Please re-enter your card. | The cached card balance expired (`balance_cache_ttl`) before the amount was confirmed. The card must be re-added.     |
| `gift_card_104` | OTP verification failed.                                          | The submitted OTP is wrong or expired, or the provider reports `verified=false`.                                      |
| `gift_card_105` | Please wait before requesting a new OTP code.                     | An OTP resend was requested during the `otp_resend_cooldown` window. The response includes the retry-after delay.     |
| `gift_card_106` | Gift card payment failed.                                         | The provider did not return a successful (`RESOLVED`) purchase. Any prior successful charges on the order are voided. |
| `gift_card_107` | Transaction failed. Please try again.                             | A generic provider failure: balance check unsuccessful, `otpRequired=true` without an `otpRef`, or resend failed.     |
| `gift_card_108` | Gift card cannot be applied: the applicable amount is zero.       | The capped applicable amount is zero (order already paid, or per-order limit reached).                                |
| `gift_card_109` | OTP has already been verified.                                    | Re-verifying an OTP that was already verified for the card.                                                           |
| `gift_card_110` | Adding multiple gift cards is not allowed.                        | A second card is applied while `multi_card_enabled` is `false`.                                                       |

## <mark style="color:red;">5. Implementing the Extension Service</mark> <a href="#id-5-implementing-the-extension-service" id="id-5-implementing-the-extension-service"></a>

To integrate a gift card provider, you need to implement an HTTP service that Commerce will call during checkout, cancellation, and refund flows. Once the service is deployed, register its URL and credentials through the Gift Card Providers screen in Omnitron. For the full API specification — including endpoint definitions, request/response field descriptions, OTP flow details, and error handling — refer to the [Extension Gift Card Gateway Flows](https://apidocs.akinon.com/flows/extension-gift-card-flows) documentation.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akinon.com/tutorials/commerce/how-to-configure-gift-card.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
