← All projects

go

Go client SDK for aice-payment: checkout, transaction read-back, and refunds.

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

Latest commit

f936400a ·

README

payment/go

Go client SDK for the AiCE-Lab payment service.

Install

go get gitlab.com/aice-lab/payment/go

Usage

import payment "gitlab.com/aice-lab/payment/go"

c := payment.New("https://payment.aice-lab.org", os.Getenv("PAYMENT_API_KEY"), nil)

resp, err := c.CreateCheckout(ctx, payment.CheckoutRequest{
    OrderRef:    "order-001",
    Gateway:     "bkash",
    AmountMinor: 10000, // 100.00 BDT in minor units
    Currency:    "BDT",
    Customer:    map[string]string{"phone": "01711000000"},
    ReturnURLs:  map[string]string{"success": "https://yourapp.example.com/ok"},
})

Card terminals (pos)

A pos checkout queues a sale on one terminal; there is no redirect. Poll GetTransaction until the status leaves PENDING. A succeeded sale carries Card (approval code, RRN, last 4 digits); a failed one carries FailureReason (declined, bridge_offline, …).

resp, err := c.CreateCheckout(ctx, payment.CheckoutRequest{
    OrderRef: "order-002", Gateway: "pos", TerminalID: "counter-1",
    AmountMinor: 150000, Currency: "BDT",
})
// errors.Is(err, payment.ErrTerminalBusy): that terminal has an unresolved sale.

// A sale stuck in PENDING (the bridge died mid-sale): settle it from the slip.
tx, err := c.Resolve(ctx, resp.TransactionID, payment.ResolveRequest{
    Status: "SUCCEEDED", ApprovalCode: "A1B2C3", CardLast4: "4242", ResolvedBy: "cashier-7",
})
terminals, err := c.Terminals(ctx) // every configured terminal: label, TID, bridge, last poll
tx, err = c.Reconcile(ctx, resp.TransactionID) // "check terminal": asks it again

Typed slips and the daily close

Record writes down a card slip or a wallet payment the cashier types. It is born SUCCEEDED with Source: "manual". One slip pays once: a second record of it fails with ErrDuplicateReference, and the *APIError names the first transaction. A record after a failed checkout needs a fresh OrderRef.

tx, err := c.Record(ctx, payment.RecordRequest{
    OrderRef: "pay-0042", Gateway: "pos", AmountMinor: 150000, Currency: "BDT",
    TerminalID: "counter-3", SlipDate: "2026-11-12", ApprovalCode: "A1B2C3",
    RRN: "612345678901", CardLast4: "4242", RecordedBy: "cashier-7",
})
var apiErr *payment.APIError
if errors.Is(err, payment.ErrDuplicateReference) && errors.As(err, &apiErr) {
    // this slip already paid apiErr.TransactionID (order apiErr.OrderRef)
}

// A wallet the tenant switched on for records:
tx, err = c.Record(ctx, payment.RecordRequest{OrderRef: "pay-0043", Gateway: "bkash",
    AmountMinor: 50000, Currency: "BDT", Reference: "9GH3K2L1M0", RecordedBy: "cashier-7"})

// One local day, page by page (stored status, no reconcile):
loc := time.FixedZone("UTC+6", 6*3600)
req := payment.ListRequest{From: time.Date(2026, 11, 12, 0, 0, 0, 0, loc), To: time.Date(2026, 11, 13, 0, 0, 0, 0, loc)}
var day []payment.Transaction
for {
    page, err := c.ListTransactions(ctx, req)
    if err != nil {
        return err
    }
    day = append(day, page.Transactions...)
    if page.NextCursor == "" {
        break
    }
    req.Cursor = page.NextCursor
}

Error handling

All errors are typed. Use errors.Is for sentinel errors and errors.As for typed errors:

_, err := c.GetTransaction(ctx, id)
switch {
case errors.Is(err, payment.ErrNotFound):
    // transaction does not exist
case errors.Is(err, payment.ErrUnauthorized):
    // bad API key
case errors.Is(err, payment.ErrDuplicateReference):
    // record: the slip already paid another transaction
case errors.Is(err, payment.ErrTerminalBusy):
    // pos: the terminal has an unresolved sale
case errors.Is(err, payment.ErrConflict):
    // e.g. already refunded
case errors.Is(err, payment.ErrAmountMismatch):
    // callback amount differed from order
case errors.Is(err, payment.ErrBadRequest):
    // other 400
default:
    var gwErr *payment.GatewayUnavailableError
    if errors.As(err, &gwErr) {
        time.Sleep(time.Duration(gwErr.RetryAfter) * time.Second)
    }
    var tErr *payment.TransportError
    if errors.As(err, &tErr) {
        // connection-level failure
    }
}

Testing

Depend on the payment.Client interface and substitute *payment.MockClient in unit tests:

m := &payment.MockClient{
    OnCreateCheckout: func(_ context.Context, req payment.CheckoutRequest) (payment.CheckoutResponse, error) {
        return payment.CheckoutResponse{TransactionID: "test-tx", RedirectURL: "https://example.com"}, nil
    },
}

License

FSL-1.1-Apache-2.0 — see LICENSE.

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