← All projects

go

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

Latest commit

e25ebe38 ·

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"},
})

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