← All projects

spec

The aice-payment contract: domain types, Provider adapter interface, transaction state machine, OpenAPI, conformance vectors, and Postgres migrations.

  • Go 100%
git@gitlab.com:aice-lab/payment/spec.git

Latest commit

bad54a88 ·

README

payment/spec

The payment contract for the AiCE-Lab payment framework: domain types, the Provider adapter interface, the transaction state machine, OpenAPI, conformance vectors, and Postgres migrations. Pinned by tag by payment-server and the SDKs.

Pure Go + static artifacts. No service, no network, no database.

Payer return from a gateway callback

return_urls are the CONSUMER’s pages; the gateway is never given them. The server registers its own callback URLs with the gateway so that verification happens server-side before anyone is told an outcome, then hands the payer on:

  • The browser-facing legs (GET for bKash, POST for SSLCommerz) answer 302 to the return_urls entry chosen by the transaction’s stored status — SUCCEEDED/REFUNDED → success, CANCELLED → cancel, anything else → fail — never by the success/fail/cancel leg the gateway picked, which is an unverified claim. transaction_id and status are appended as query parameters; any query already on the stored URL is preserved.
  • The ipn leg is server-to-server and keeps answering a bare 200.
  • A transaction with no usable (absolute http/https) return URL for its outcome answers 200 with a short plain-text body, not an empty page.

Outbound events

A tenant may register a webhook endpoint plus a signing secret. One event is emitted per REAL terminal transition — SUCCEEDED, FAILED, CANCELLED, EXPIRED, REFUNDED — while replays and no-op transitions emit nothing:

{"foundation_ref":"7c1e…","order_ref":"ORD-1","status":"SUCCEEDED",
 "amount_minor":50000,"currency":"BDT","gateway":"bkash",
 "occurred_at":"2026-01-02T03:04:05Z"}

amount_minor is minor units (poisha for BDT) and never a float. foundation_ref is the payment transaction id and is the consumer’s dedupe key.

Delivery is at-least-once: retried with backoff until any 2xx, resumed after a restart, and possibly duplicated (a producer replica or a lost response can re-send). Retries carry the byte-identical body, so the signature is stable across attempts — dedupe on foundation_ref, never on the signature. Do not treat the event as the source of truth for anything irreversible without also reading GET /v1/transactions/{id}, which stays authoritative.

Verifying the signature

X-Payment-Signature is the lowercase hex HMAC-SHA256 of the RAW request body under the tenant’s webhook secret. Compute it over the bytes as received — re-serialising the parsed JSON will change them and the check will fail. Compare in constant time:

import hashlib, hmac

def verify(raw_body: bytes, header: str, secret: str) -> bool:
    expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, header or "")

# e.g. Django/Flask: pass request.body (bytes), not request.POST / json.dumps(...)
if not verify(raw, request.headers.get("X-Payment-Signature", ""), SECRET):
    return HttpResponse(status=401)   # a non-2xx makes the server retry

In-person card terminals (pos)

A pos checkout queues a sale for one card terminal (terminal_id). A bridge on the merchant’s LAN drives the terminal. It only dials out: it long-polls POST /v1/tenants/{tenant}/pos/poll, runs what it claims, and posts the result to /v1/tenants/{tenant}/gateways/pos/ipn. Nothing listens on the merchant side.

Each bridge has its own secret, and the tenant config binds every terminal to one bridge. A bridge may poll, claim and report only for its own terminals; a poll naming any other terminal is refused. Every poll and result is a form carrying bridge_id, timestamp (unix seconds) and signature: the lowercase hex HMAC-SHA256 over the form encoding, keys sorted, of every field except signature itself. A timestamp more than 5 minutes off is refused. Only the bridge that claimed a command may report its result.

The sale maps onto the existing states; there is no new status:

  • Queued: PENDING. A terminal holds one open sale at a time; a second checkout for it answers 409 terminal_busy.
  • No bridge claims it within the claim window (default 20 s): FAILED, failure_reason: bridge_offline. It never reached the terminal.
  • Approved for the same amount: SUCCEEDED, with a card block (approval code, RRN, last 4, TID, MID, source: terminal). CORE-POLICY P4.7 allows nothing more.
  • Declined or cancelled at the terminal: FAILED / CANCELLED.
  • Claimed but no result: stays PENDING. It is never failed or retried on its own, because the card may have been charged. The consumer’s “check terminal” action calls POST /v1/transactions/{id}/reconcile, which queues an inquiry to the terminal; POST /v1/transactions/{id}/resolve settles it by hand from the slip, naming the person.

Card data stays in the terminal. A result with a full card number in any field is refused, not stored. Provider.Verify and Query may now return PENDING, meaning no decision yet.

Recording a payment from its slip

POST /v1/records writes down a payment taken outside the service: a card slip from a terminal no bridge drives, or a wallet payment the payer shows. It is born SUCCEEDED with source: manual. It is the word of the person in recorded_by, not a gateway verification, and it is never refunded or reconciled by the service.

  • Card: terminal_id (any terminal in the tenant config, bridged or not), slip_date as printed, approval_code, rrn (required when a bridge drives the terminal), and optionally card_last4. The card block takes the terminal’s TID from the config.
  • Wallet: a gateway the tenant switched on in record_gateways (bkash, nagad) and reference, the wallet’s transaction id.

One slip pays once. The server normalises the codes (trimmed, upper-case) and claims a key per slip in the tenant’s database, in the same transaction as the payment: terminal + RRN, terminal + slip date + approval code, wallet + transaction id. A second record of the slip answers 409 duplicate_reference with the first transaction’s transaction_id and order_ref. A pos sale or a hosted bKash payment that the gateway confirmed claims its RRN or transaction id the same way, so typing its slip later is refused too.

order_ref is the idempotency key, shared with checkouts. The same order_ref with the same gateway, amount and slip returns the first record (200); anything else answers 409 order_ref_conflict. A record after a failed checkout needs a fresh order_ref.

GET /v1/transactions?from=&to= lists what was created in [from, to), oldest first, next_cursor paging, optional gateway and status. It reads stored status and never reconciles. A sale started before midnight and settled after it is listed on the day it started, so join by transaction_id, not by date. Vectors: conformance/records/.

License: FSL-1.1-Apache-2.0 (converts to Apache-2.0 after two years).

This is a snapshot generated from GitLab. For the live README, see the project page.