visamasterpay: keep a payment pending while GBÜS still reports it in progress
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user