From 78e1976e5372aedb3d3f51c3eb99237c3e204bd6 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Sat, 24 Oct 2026 22:58:49 +0500 Subject: [PATCH] =?UTF-8?q?visamasterpay:=20keep=20a=20payment=20pending?= =?UTF-8?q?=20while=20GB=C3=9CS=20still=20reports=20it=20in=20progress?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../payment/visamasterpay/db/postgresql.go | 32 +++++++++++++++---- pkg/paymentgateway/client.go | 11 +++++++ pkg/paymentgateway/client_test.go | 25 +++++++++++++-- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/internal/payment/visamasterpay/db/postgresql.go b/internal/payment/visamasterpay/db/postgresql.go index 5fdca22..09b47e7 100644 --- a/internal/payment/visamasterpay/db/postgresql.go +++ b/internal/payment/visamasterpay/db/postgresql.go @@ -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, diff --git a/pkg/paymentgateway/client.go b/pkg/paymentgateway/client.go index d15faf5..e432da7 100644 --- a/pkg/paymentgateway/client.go +++ b/pkg/paymentgateway/client.go @@ -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 { diff --git a/pkg/paymentgateway/client_test.go b/pkg/paymentgateway/client_test.go index abb0a41..81fb5e4 100644 --- a/pkg/paymentgateway/client_test.go +++ b/pkg/paymentgateway/client_test.go @@ -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 {