visamaster: record order actions for the "Ammallar" table
Every create, update and delete of a visa/master or sber order is now
recorded in visa_master_order_actions: who did it, finished or failed
(with the exception), and for an update only the columns that changed -
original before, changes after. Marking an order paid when a payment is
finalized is recorded the same way, with no user when the GBÜS callback
did it.
Actions are written in the transaction that changes the order, so one is
on record exactly when its change is; failed ones are written after the
rollback with the values they attempted. The payment finalize records
after its commit instead, so recording never holds up a payment GBÜS has
already taken. Updates that change nothing and creates that fail (no
order to attach to) are not recorded.
GET /visa-master-payment-orders/{id}/actions lists them, newest first;
GET /visa-master-payment-orders/{id}/actions/{actionId} adds original,
changes and exception. Both are staff-only and order-scoped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/visamasteractions"
|
||||
actionsdb "online-back/internal/cards/visamasteractions/db"
|
||||
"online-back/internal/currency"
|
||||
"online-back/internal/payment/paymentsettings"
|
||||
"online-back/internal/payment/visamasterpay"
|
||||
@@ -409,32 +411,57 @@ func (r *repository) verifyPayment(ctx context.Context, paymentID int64, trigger
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var lockedPaid bool
|
||||
if err := tx.QueryRow(ctx, `SELECT paid FROM visa_master_payment_orders WHERE id = $1 FOR UPDATE`, orderID).Scan(&lockedPaid); err != nil {
|
||||
var lockedStatus *string
|
||||
if err := tx.QueryRow(ctx, `SELECT paid, status FROM visa_master_payment_orders WHERE id = $1 FOR UPDATE`, orderID).Scan(&lockedPaid, &lockedStatus); err != nil {
|
||||
fail("finalize_lock_order", err)
|
||||
return visamasterpay.PaymentResultDTO{}, err
|
||||
}
|
||||
// orderAction is the order's update action once it is marked paid,
|
||||
// recorded only after the commit (or as failed if it never lands):
|
||||
// recording it must never hold up finalizing a payment GBÜS has
|
||||
// already taken.
|
||||
var orderAction *visamasteractions.CreateOrderActionDTO
|
||||
failFinalize := func(stage string, err error) {
|
||||
fail(stage, err)
|
||||
if orderAction != nil {
|
||||
exception := err.Error()
|
||||
orderAction.Status, orderAction.Exception = visamasteractions.StatusFailed, &exception
|
||||
r.recordOrderAction(ctx, *orderAction)
|
||||
}
|
||||
}
|
||||
if lockedPaid {
|
||||
step("order_already_marked_paid", nil)
|
||||
} else {
|
||||
original, changes := visamasteractions.Diff(
|
||||
map[string]interface{}{"paid": false, "status": derefOrNil(lockedStatus)},
|
||||
map[string]interface{}{"paid": true, "status": "completed"},
|
||||
)
|
||||
orderAction = &visamasteractions.CreateOrderActionDTO{
|
||||
OrderId: orderID, Name: visamasteractions.NameUpdate, Status: visamasteractions.StatusFinished,
|
||||
Original: original, Changes: changes, CreatedBy: requestedBy,
|
||||
}
|
||||
if _, err := tx.Exec(ctx,
|
||||
`UPDATE visa_master_payment_orders SET paid = true, status = 'completed', updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $1`,
|
||||
orderID,
|
||||
); err != nil {
|
||||
fail("finalize_mark_order_paid", err)
|
||||
failFinalize("finalize_mark_order_paid", err)
|
||||
return visamasterpay.PaymentResultDTO{}, err
|
||||
}
|
||||
}
|
||||
|
||||
item, err = updatePaymentStatus(ctx, tx, paymentID, "success", status.RawResponse)
|
||||
if err != nil {
|
||||
fail("finalize_save_payment_status", err)
|
||||
failFinalize("finalize_save_payment_status", err)
|
||||
return visamasterpay.PaymentResultDTO{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
fail("finalize_commit", err)
|
||||
failFinalize("finalize_commit", err)
|
||||
return visamasterpay.PaymentResultDTO{}, err
|
||||
}
|
||||
if orderAction != nil {
|
||||
r.recordOrderAction(ctx, *orderAction)
|
||||
}
|
||||
} 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,
|
||||
@@ -483,6 +510,15 @@ func (r *repository) Status(ctx context.Context, orderID int64, requestedBy *int
|
||||
return r.verifyPayment(ctx, paymentID, "status", requestedBy)
|
||||
}
|
||||
|
||||
// recordOrderAction records an order action ("Ammallar") of finalizing a
|
||||
// payment through the plain client. Best-effort, like logStep: a write
|
||||
// failure is logged, not propagated.
|
||||
func (r *repository) recordOrderAction(ctx context.Context, dto visamasteractions.CreateOrderActionDTO) {
|
||||
if err := actionsdb.Insert(context.WithoutCancel(ctx), r.client, dto); err != nil && r.logger != nil {
|
||||
r.logger.Errorf("visa master order action write failed (order %d, %s %s): %v", dto.OrderId, dto.Name, dto.Status, err)
|
||||
}
|
||||
}
|
||||
|
||||
// branchCredentials picks the sber or visa/master MPI merchant credentials
|
||||
// for a branch, matching the type of order being paid.
|
||||
func (r *repository) branchCredentials(ctx context.Context, branchID int64, sber bool) (username, password string, err error) {
|
||||
@@ -527,6 +563,15 @@ func isSber(orderType *string) bool {
|
||||
return orderType != nil && strings.EqualFold(strings.TrimSpace(*orderType), orderTypeSber)
|
||||
}
|
||||
|
||||
// derefOrNil keeps a null column null in an action's original values
|
||||
// rather than turning it into a typed nil pointer.
|
||||
func derefOrNil(s *string) interface{} {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func derefOrEmpty(s *string) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user