visamasterpay: keep a payment pending while GBÜS still reports it in progress
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

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>
This commit is contained in:
2026-10-24 22:58:49 +05:00
parent f1337706f0
commit 78e1976e53
3 changed files with 60 additions and 8 deletions

View File

@@ -306,10 +306,17 @@ func (r *repository) verifyPayment(ctx context.Context, paymentID int64, trigger
var gatewayStatus string
var gatewayReference *string
var createdBy *int64
// staleFailure flags a row finalized as failed while GBÜS still reported
// it in progress (checked before the card was even entered, by code
// predating StatusResult.InProgress) - it is re-checked like a pending one.
var staleFailure bool
err := r.client.QueryRow(ctx,
`SELECT order_id, gateway_status, gateway_reference, created_by FROM visa_master_order_payments WHERE id = $1 AND deleted_at IS NULL`,
`SELECT order_id, gateway_status, gateway_reference, created_by,
COALESCE(gateway_status = 'failed' AND gateway_response->>'orderStatus' IN ('0', '1', '5')
AND COALESCE(gateway_response->>'errorCode', '0') IN ('', '0'), false)
FROM visa_master_order_payments WHERE id = $1 AND deleted_at IS NULL`,
paymentID,
).Scan(&orderID, &gatewayStatus, &gatewayReference, &createdBy)
).Scan(&orderID, &gatewayStatus, &gatewayReference, &createdBy, &staleFailure)
if errors.Is(err, pgx.ErrNoRows) {
// Unknown payment id: there's no order to attach a log row to.
return visamasterpay.PaymentResultDTO{}, visamasterpay.ErrPaymentNotFound
@@ -354,7 +361,7 @@ func (r *repository) verifyPayment(ctx context.Context, paymentID int64, trigger
return item
}
if gatewayStatus != "pending" {
if gatewayStatus != "pending" && !staleFailure {
item, err := fetchPaymentByID(ctx, r.client, paymentID)
if err != nil {
fail("load_payment", err)
@@ -428,6 +435,17 @@ func (r *repository) verifyPayment(ctx context.Context, paymentID int64, trigger
fail("finalize_commit", err)
return visamasterpay.PaymentResultDTO{}, err
}
} else if status.InProgress() {
// Not paid yet but not over either (e.g. checked before the card was
// entered): keep it pending so a later callback/Status re-checks it,
// storing the response so the card shows up once GBÜS reports it.
item, err = updatePaymentStatus(ctx, r.client, paymentID, "pending", status.RawResponse)
if err != nil {
fail("save_payment_status", err)
return visamasterpay.PaymentResultDTO{}, err
}
step("payment_still_pending", map[string]interface{}{"orderStatus": status.OrderStatus, "actionCode": status.ActionCode})
return finish(item), nil
} else {
item, err = updatePaymentStatus(ctx, r.client, paymentID, "failed", status.RawResponse)
if err != nil {
@@ -446,7 +464,8 @@ func (r *repository) verifyPayment(ctx context.Context, paymentID int64, trigger
// HandleCallback does - for when the frontend can't rely on GBÜS's redirect
// having reached the callback (e.g. the user closed the hosted payment page
// first). Safe to call repeatedly: it only hits GBÜS while the attempt is
// still "pending" and otherwise just returns the already-finalized row.
// still "pending" (or a stale failure, see verifyPayment) and otherwise
// just returns the already-finalized row.
func (r *repository) Status(ctx context.Context, orderID int64, requestedBy *int64) (visamasterpay.PaymentResultDTO, error) {
var paymentID int64
err := r.client.QueryRow(ctx,
@@ -643,8 +662,9 @@ func updatePaymentGatewayResult(ctx context.Context, q execer, paymentID int64,
return err
}
// updatePaymentStatus records the verified final outcome (from
// HandleCallback) and returns the updated row.
// updatePaymentStatus records a verified getOrderStatusExtended.do outcome
// (from HandleCallback/Status) - final, or still pending - and returns the
// updated row.
func updatePaymentStatus(ctx context.Context, q querier, paymentID int64, status string, response []byte) (visamasterpay.PaymentResultDTO, error) {
query := `
UPDATE visa_master_order_payments SET gateway_status = $1, gateway_response = $2,

View File

@@ -58,6 +58,17 @@ type StatusResult struct {
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 {

View File

@@ -27,6 +27,27 @@ func TestRegisterResultSuccess(t *testing.T) {
}
}
func TestStatusResultInProgress(t *testing.T) {
cases := []struct {
name string
r StatusResult
want bool
}{
{"registered, not attempted", StatusResult{OrderStatus: 0, ErrorCode: "0"}, true},
{"pre-authorized", StatusResult{OrderStatus: 1}, true},
{"3-D Secure", StatusResult{OrderStatus: 5, ErrorCode: "0"}, true},
{"paid", StatusResult{Paid: true, OrderStatus: 2}, false},
{"reversed", StatusResult{OrderStatus: 3}, false},
{"declined", StatusResult{OrderStatus: 6, ErrorCode: "0"}, false},
{"gateway error", StatusResult{OrderStatus: 0, ErrorCode: "6"}, false},
}
for _, c := range cases {
if got := c.r.InProgress(); got != c.want {
t.Errorf("%s: InProgress()=%v want %v", c.name, got, c.want)
}
}
}
func TestLogClientAlwaysPaid(t *testing.T) {
c := NewLogClient(nil)
reg, err := c.Register(context.Background(), RegisterRequest{OrderNumber: "vmo-1-2", ReturnURL: "https://x/cb"})
@@ -74,8 +95,8 @@ func TestHTTPClientCheckStatusPaidMapping(t *testing.T) {
body string
paid bool
}{
{`{"orderStatus":2,"actionCode":"0"}`, true}, // 2 = deposited
{`{"orderStatus":1,"actionCode":0}`, false}, // numeric actionCode still decodes
{`{"orderStatus":2,"actionCode":"0"}`, true}, // 2 = deposited
{`{"orderStatus":1,"actionCode":0}`, false}, // numeric actionCode still decodes
{`{"orderStatus":6,"actionCode":"declined"}`, false},
}
for _, tc := range cases {