---
url: https://laranex.vercel.app/go-myanmar-payments/callbacks.md
description: >-
  Verify gateway callbacks with HandleCallback, read a gateway-independent
  PaymentStatus, acknowledge the callback, and check payment status when a
  callback is late.
---

# Callbacks & Status

Every gateway notifies your server of the payment result. `HandleCallback` takes a `*myanmarpayments.CallbackRequest`, verifies the gateway's signature and returns a `*myanmarpayments.PaymentCallback`. It takes no context: verifying needs no network call.

::: tip Production setup
For production, follow [Handling Webhooks (recommended)](/go-myanmar-payments/webhooks): verify, store the call, acknowledge immediately, then process it once in the background with retries. The example below handles everything inline to show the API.
:::

Every callback goes through the same steps; KBZ Pay is shown here.

```go
package shop

import (
	"database/sql"
	"log"
	"net/http"
	"time"

	myanmarpayments "github.com/laranex/go-myanmar-payments/v4"
	"github.com/laranex/go-myanmar-payments/v4/kbzpay"
)

type Handlers struct {
	DB  *sql.DB
	KBZ *kbzpay.Gateway
}

func (h *Handlers) KBZCallback(w http.ResponseWriter, r *http.Request) {
	request, err := myanmarpayments.NewCallbackRequestFromHTTP(r)
	if err != nil {
		http.Error(w, "invalid callback", http.StatusBadRequest)
		return
	}
	callback, err := h.KBZ.HandleCallback(request)
	if err != nil {
		log.Printf("rejected KBZ callback: %v", err)
		http.Error(w, "invalid callback", http.StatusBadRequest)
		return
	}

	var amount string
	var paidAt sql.NullInt64
	err = h.DB.QueryRowContext(r.Context(),
		`SELECT amount, paid_at FROM orders WHERE number = ?`,
		callback.OrderID,
	).Scan(&amount, &paidAt)
	if err != nil {
		http.Error(w, "try again", http.StatusInternalServerError)
		return
	}
	order, err := myanmarpayments.ParseAmount(amount)
	if err != nil {
		http.Error(w, "try again", http.StatusInternalServerError)
		return
	}
	paid := order.Equals(callback.Amount)
	if callback.IsSuccessful() && !paidAt.Valid && paid {
		// paid_at IS NULL keeps a duplicate callback from fulfilling twice
		_, err = h.DB.ExecContext(r.Context(), `UPDATE orders
			SET paid_at = ?, gateway_reference = ?
			WHERE number = ? AND paid_at IS NULL`,
			time.Now().Unix(), callback.GatewayReference, callback.OrderID)
		if err != nil {
			http.Error(w, "try again", http.StatusInternalServerError)
			return
		}
	}

	// KBZ Pay: HTTP 200 with plain-text "success"
	callback.Acknowledgement.Write(w)
}
```

## Building a CallbackRequest

Signatures are checked against what the gateway actually sent, so build the request from the real incoming request: the raw body bytes, the headers and the query string. Never rebuild it from parsed input such as `r.ParseForm()` values, a struct decoded with `json.Unmarshal` or Gin's `c.ShouldBind`: a JSON number such as `1000.50` would come back as `1000.5` and break a signature over the exact text.

| Constructor | Use when |
|---|---|
| `myanmarpayments.NewCallbackRequestFromHTTP(r)` | Every framework that exposes an `*http.Request`: reads the body once and keeps the headers and query. The body of `r` is restored, so it can still be read. A body over 64 KiB returns [`ErrCallbackBodyTooLarge`](/go-myanmar-payments/references/errors#errcallbackbodytoolarge): answer `413` |
| `myanmarpayments.NewCallbackRequest(body, header, query)` | Any other server: pass the raw body bytes, an `http.Header` and `url.Values`. `nil` header and query are allowed |
| `myanmarpayments.NewCallbackRequestFromJSON(payload, header)` | Replaying a payload you stored as decoded JSON, e.g. from a queue or a failed-callback table |

| Framework | `*http.Request` |
|---|---|
| `net/http`, chi | `r` |
| Gin | `c.Request` |
| Echo | `c.Request()` |

See [CallbackRequest](/go-myanmar-payments/references/payment-callback#callbackrequest) for its members.

## Rules

* **Verify, then trust.** A callback that fails verification returns `*myanmarpayments.SignatureVerificationError`, and so does one whose signed or hashed field holds an object or array instead of a single value, since no gateway signs nested values. Never act on its payload; `Raw` carries the unverified data for logging only.
* **Check the amount.** Compare `callback.Amount` (the exact text the gateway sent) with your order before fulfilling, e.g. with `Amount.Equals`. Yoma MMQR callbacks carry no amount: confirm those with [`Status`](/go-myanmar-payments/drivers/yoma-mmqr#status-checks).
* **Signed is not enough on its own.** Where a gateway joins the signed values with a separator (AYA Pay `:`, CyberSource `,` and `=`) or with none (Wave Pay, Wave Pay Mini App), the package also rejects values that could be re-split into other fields: a correctly signed callback that fails these checks reports `... is malformed.` instead of `... verification failed.`. See each driver's Handling Callbacks section.
* **Be idempotent.** Gateways retry and may deliver the same callback more than once.

## Acknowledging

`callback.Acknowledgement` is an `Acknowledgement` with `Status`, `Body` and `Headers`. `Write` sets the headers, then writes the status (`200` when it is `0`) and the body to any `http.ResponseWriter`:

| Framework | Response |
|---|---|
| `net/http`, chi | `callback.Acknowledgement.Write(w)` |
| Gin | `callback.Acknowledgement.Write(c.Writer)` |
| Echo | `callback.Acknowledgement.Write(c.Response())` |

`myanmarpayments.DefaultAcknowledgement()` is the empty `200 text/plain` response most gateways expect.

## PaymentStatus

Every gateway's own status values are mapped onto one `string` type. The original value stays in `callback.GatewayStatus`.

| Constant | Value | Meaning |
|---|---|---|
| `StatusSuccessful` | `successful` | The customer paid. The only status that means money was collected. |
| `StatusPending` | `pending` | Still in progress or waiting on the customer. |
| `StatusFailed` | `failed` | Attempted and failed or rejected. |
| `StatusCanceled` | `canceled` | Canceled or closed before completing. |
| `StatusExpired` | `expired` | The payment window ran out. |
| `StatusUnknown` | `unknown` | A status this package does not recognize yet. Inspect `GatewayStatus`. |

`PaymentStatus` is a `string` type, so `callback.Status == "successful"` works and `string(callback.Status)` is the value. `status.IsFinal()` is `false` for `StatusPending` and `StatusUnknown`. Unknown statuses never return an error.

Each gateway page lists its exact mapping.

## Status Checks

When a callback is late, ask KBZ Pay, AYA or Yoma directly; Wave Pay, Wave Pay Mini App and CyberSource have no status API in this package.

Status checks return a `*myanmarpayments.PaymentStatusResult` with the same `Status`, `GatewayStatus`, `GatewayReference` and `Amount` fields.

| Gateway | Call |
|---|---|
| KBZ Pay | `kbz.Status(ctx, orderID)` |
| AYA Payment Gateway | `aya.Status(ctx, orderID)` |
| Yoma MMQR | `yoma.Status(ctx, payment.Reference)` |
| Wave Pay | No status API: rely on the callback |
| Wave Pay Mini App | No status API: rely on the callback |
| CyberSource | No status API in this package: rely on the callback |

```go
result, err := kbz.Status(ctx, "ORDER_1")
if err != nil {
	var apiErr *myanmarpayments.APIError
	if errors.As(err, &apiErr) {
		log.Printf("KBZ %s: %s", apiErr.GatewayCode, apiErr.GatewayMessage)
	}
	return err
}

if result.IsSuccessful() {
	// ...
}
```

See [PaymentCallback & Status](/go-myanmar-payments/references/payment-callback) for every field.
