Files
online-backend/pkg/paymentgateway/client.go
T
guwandyk f42ad01828
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
added print
2026-09-19 12:26:35 +05:00

107 lines
4.1 KiB
Go

// Package paymentgateway talks to GBÜS (mpi.gov.tm), Turkmenistan's
// national MPI, a "Сбербанк АС МПС"/PaymentGate-family gateway. Payment is
// two-phase and redirect-based, not a synchronous debit: Register hands
// back a hosted-payment-page URL for the customer to complete payment on,
// and CheckStatus is the only source of truth for whether it was actually
// paid - callers must never trust a browser redirect's query params alone.
package paymentgateway
import (
"context"
"encoding/json"
"time"
"online-back/pkg/logging"
)
// RegisterRequest registers a new payment with the gateway (GBÜS register.do).
type RegisterRequest struct {
OrderNumber string // our own unique order reference
AmountMinor int64 // amount in tetri (TMT * 100), matching the gateway's integer-minor-unit convention
ReturnURL string
Description string
Username string
Password string
}
// RegisterResult is the outcome of Register.
type RegisterResult struct {
OrderId string // the gateway's own order id - store this, it's what CheckStatus needs
FormUrl string // where to send the customer to complete payment
ErrorCode string
ErrorMessage string
RawResponse []byte
}
func (r RegisterResult) Success() bool {
return r.OrderId != "" && r.FormUrl != "" && (r.ErrorCode == "" || r.ErrorCode == "0")
}
// StatusRequest checks a previously registered payment's outcome (GBÜS
// getOrderStatusExtended.do).
type StatusRequest struct {
OrderId string
Username string
Password string
}
// StatusResult is the outcome of CheckStatus. Paid is the only field
// callers should act on; OrderStatus == 2 means fully paid/deposited per
// the AS MPS convention, anything else is not (yet) paid.
type StatusResult struct {
Paid bool
OrderStatus int
ActionCode string
ActionCodeDescription string
ErrorCode string
ErrorMessage string
RawResponse []byte
}
// Client is the gateway contract. Plug a real HTTPClient in once
// credentials are configured; LogClient is a stand-in for local/dev use.
type Client interface {
Register(ctx context.Context, req RegisterRequest) (RegisterResult, error)
CheckStatus(ctx context.Context, req StatusRequest) (StatusResult, error)
}
// LogClient is a stand-in used in debug mode so local/dev environments
// never hit the real bank gateway by accident. Register logs the request
// and returns a synthetic order id with no real form URL; CheckStatus
// immediately reports the payment as paid, so the full register ->
// callback -> finalize flow is exercisable end-to-end without real MPI
// credentials.
type LogClient struct {
logger *logging.Logger
}
func NewLogClient(logger *logging.Logger) *LogClient {
return &LogClient{logger: logger}
}
func (c *LogClient) Register(_ context.Context, req RegisterRequest) (RegisterResult, error) {
orderID := "LOG-" + req.OrderNumber
if c.logger != nil {
c.logger.Infof("payment gateway register (stub): orderNumber=%s amountMinor=%d orderId=%s", req.OrderNumber, req.AmountMinor, orderID)
}
raw, _ := json.Marshal(map[string]interface{}{
"stub": true, "note": "no real MPI client configured (debug mode) - pkg/paymentgateway.LogClient",
"orderId": orderID, "orderNumber": req.OrderNumber, "amountMinor": req.AmountMinor, "processedAt": time.Now().UTC(),
})
// FormUrl must be non-empty for RegisterResult.Success() to pass - reusing
// ReturnURL here means opening it goes straight to the callback, which
// this stub's CheckStatus always reports as paid, exercising the full
// register -> callback -> finalize flow without a real bank redirect page.
return RegisterResult{OrderId: orderID, FormUrl: req.ReturnURL, RawResponse: raw}, nil
}
func (c *LogClient) CheckStatus(_ context.Context, req StatusRequest) (StatusResult, error) {
if c.logger != nil {
c.logger.Infof("payment gateway status check (stub): orderId=%s -> paid", req.OrderId)
}
raw, _ := json.Marshal(map[string]interface{}{
"stub": true, "orderId": req.OrderId, "orderStatus": 2, "processedAt": time.Now().UTC(),
})
return StatusResult{Paid: true, OrderStatus: 2, RawResponse: raw}, nil
}