Payment Flows
Starting a payment always follows the same pattern: build the gateway's payment data, call the gateway, then act on the typed result. Each flow has its own result type with exactly the fields that flow needs, and every result implements myanmarpayments.PaymentResult, whose Flow() returns FlowRedirect, FlowForm, FlowQR, FlowApp or FlowMiniApp, so a type switch tells them apart.
| Result | What you do | Returned by |
|---|---|---|
*RedirectPayment | Redirect the customer to payment.URL | kbz.PWA, wave.Initiate |
*FormPayment | Write payment.HTML() | aya.Initiate, cs.HostedCheckout |
*QrPayment | Show the QR to the customer | kbz.QR, yoma.Initiate, yoma.RenewQR |
*AppPayment | Return the signed payload to your mobile app | kbz.App |
*MiniAppPayment | Return the signed payload to your mini app page | miniApp.Payment |
Every method validates the payment data first and returns *myanmarpayments.InvalidPaymentDataError before any request is sent. Call data.Validate() yourself to check a request earlier, e.g. while handling a form. Callback, return and cancel URLs must be absolute http or https URLs by PHP's FILTER_VALIDATE_URL rules, the same in every Laranex SDK: printable ASCII only, an ASCII host name (labels of letters, digits and inner -, at most 63 characters each) or a bracketed IPv6 address, and an optional port from 0 to 65535; anything else fails with The callbackUrl field must be a valid http or https URL. The customer finishing on the gateway's side is never proof of payment: fulfill orders from the verified callback or a status check.
The samples on this page and the gateway pages are net/http handlers; Framework Integration shows chi, Gin and Echo.
Redirect Payments
Here is the flow with the KBZ Pay PWA; Wave Pay works the same way with its own payment page.
The gateway hosts its own payment page. Send the customer there.
func checkout(w http.ResponseWriter, r *http.Request) {
payment, err := kbz.PWA(r.Context(), kbzpay.PaymentData{
OrderID: "ORDER_1",
Amount: myanmarpayments.Kyat(10000),
CallbackURL: "https://shop.test/payments/kbz/callback",
})
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
http.Redirect(w, r, payment.URL, http.StatusFound)
}payment.GatewayReference holds the gateway's ID for the attempt (KBZ prepay_id, Wave transaction_id).
Form Payments
Here is the flow with AYA Pay; CyberSource works the same way with its hosted checkout.
The gateway expects the customer's browser to POST a signed form. HTML() returns a complete page that submits the form as soon as it loads, with every value escaped:
func ayaCheckout(w http.ResponseWriter, r *http.Request) {
// no network call, so no context
payment, err := aya.Initiate(data)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, payment.HTML())
}Under a Content-Security-Policy that allows scripts by nonce (script-src 'nonce-…'), write payment.HTMLWithNonce(nonce) instead: it renders <script nonce="…">. Send the page with Referrer-Policy: strict-origin if you serve it from a URL that holds anything secret, so the gateway only sees your origin.
To build the form yourself, use Action, Fields (an ordered []FormField) and Enctype with html/template, which escapes every value:
var form = template.Must(template.New("form").Parse(`
<form id="payment-form" method="POST"
action="{{.Action}}" enctype="{{.Enctype}}">
{{range .Fields}}
<input type="hidden" name="{{.Name}}" value="{{.Value}}">
{{end}}
</form>`))
form.Execute(w, payment)Post the fields unchanged: they are signed. payment.Field(name) looks up one value and payment.Values() returns them as a map[string]string. AYA expects multipart/form-data, which Enctype carries.
QR Payments
Here is the flow with Yoma MMQR, whose QR expires after 120 seconds; a KBZ Pay QR follows the same steps without renewals.
Gateways return QR codes in two shapes:
| Field | Gateway | Use it as |
|---|---|---|
QRString | KBZ Pay | A payload: encode it into a QR image with any QR library, e.g. skip2/go-qrcode |
QRImage | Yoma MMQR | A base64 image: display it as is, e.g. with QRImageDataURI("") |
func yomaCheckout(w http.ResponseWriter, r *http.Request) {
payment, err := yoma.Initiate(r.Context(), data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
page.Execute(w, payment)
}<img src="{{.QRImageDataURI ""}}" alt="Scan to pay">
<p>Payable until {{.ExpiresAt.Format "15:04:05"}}</p>ExpiresAt is when the QR stops being payable, and Reference holds the ID used for status checks (Yoma refLabel, KBZ prepay_id).
App Payments
Here is the flow with the KBZ Pay mobile SDK.
The KBZ Pay mobile SDK needs a signed order string. AppPayment encodes to JSON with the SDK's names, so return it to your app as is; the app passes the values to KBZPay.startPay():
func kbzAppCheckout(w http.ResponseWriter, r *http.Request) {
payment, err := kbz.App(r.Context(), data)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
w.Header().Set("Content-Type", "application/json")
// {"orderId", "orderInfo", "sign", "signType"}
json.NewEncoder(w).Encode(payment)
}The SDK's own result only means the payment screen closed; rely on the callback or kbz.Status.
Mini App Payments
Wave Pay Mini App runs your web page inside the WavePay app. Payment signs the object the page passes to Wave's JavaScript SDK; no request is sent. MiniAppPayment.JSON() is exactly that object:
func miniAppCheckout(w http.ResponseWriter, r *http.Request) {
payment, err := miniApp.Payment(&data)
if err != nil {
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", "application/json")
// {"amount", "miniAppID", "merchantRefID", "orderID", "callBackUrl", "hashValue", "items"}
io.WriteString(w, payment.JSON())
}The page calls WaveJsSDK.paymentModule.makePayment(payload) with it after logging in with your server's access token; the SDK's result is not proof of payment. See Wave Pay Mini App for the whole flow.
Handling Any Result
package shop
import (
"encoding/json"
"io"
"net/http"
myanmarpayments "github.com/laranex/go-myanmar-payments/v4"
)
func respond(
w http.ResponseWriter,
r *http.Request,
payment myanmarpayments.PaymentResult,
) {
switch payment := payment.(type) {
case *myanmarpayments.RedirectPayment:
http.Redirect(w, r, payment.URL, http.StatusFound)
case *myanmarpayments.FormPayment:
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, payment.HTML())
case *myanmarpayments.QrPayment:
qr := payment.QRImageDataURI("")
if qr == "" {
qr = payment.QRString
}
io.WriteString(w, qr)
case *myanmarpayments.AppPayment:
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(payment)
case *myanmarpayments.MiniAppPayment:
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, payment.JSON())
}
}See Results for every field.