A status check that ran before the card was entered (orderStatus 0, "no payment attempts") finalized the payment as failed, and later callbacks returned it as already finalized without asking GBÜS again - so cardholderName/maskedPan stayed null and a real payment was never recorded. Unpaid in-progress statuses (0, 1, 5) now keep the row pending with the latest response stored, and rows already failed that way are re-checked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
118 lines
4.6 KiB
Go
118 lines
4.6 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
|
||
}
|
||
|
||
// InProgress reports an unpaid order GBÜS may still complete - registered
|
||
// but not attempted yet (0), pre-authorized (1) or in 3-D Secure (5) - so
|
||
// it must stay re-checkable instead of being finalized as failed. Reversed
|
||
// (3), refunded (4), declined (6) or any gateway error are final.
|
||
func (r StatusResult) InProgress() bool {
|
||
if r.ErrorCode != "" && r.ErrorCode != "0" {
|
||
return false
|
||
}
|
||
return r.OrderStatus == 0 || r.OrderStatus == 1 || r.OrderStatus == 5
|
||
}
|
||
|
||
// 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
|
||
}
|