fixed error
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

This commit is contained in:
2026-09-21 17:51:10 +05:00
parent f42ad01828
commit 23d208769c
2 changed files with 64 additions and 6 deletions

View File

@@ -306,6 +306,7 @@ func (r *repository) Pay(ctx context.Context, orderID int64, dto visamasterpay.P
// replayed callback on an already-finalized attempt just returns its
// current state without calling GBÜS again.
func (r *repository) HandleCallback(ctx context.Context, paymentID int64) (visamasterpay.PaymentResultDTO, error) {
println("[callback] step 1: loading payment", paymentID)
var orderID int64
var gatewayStatus string
var gatewayReference *string
@@ -315,20 +316,26 @@ func (r *repository) HandleCallback(ctx context.Context, paymentID int64) (visam
paymentID,
).Scan(&orderID, &gatewayStatus, &gatewayReference, &createdBy)
if errors.Is(err, pgx.ErrNoRows) {
println("[callback] step 1 FAILED: payment not found")
return visamasterpay.PaymentResultDTO{}, visamasterpay.ErrPaymentNotFound
}
if err != nil {
println("[callback] step 1 FAILED: load payment:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 1 ok: orderId=", orderID, "gatewayStatus=", gatewayStatus)
println("[callback] step 2: loading order", orderID)
var orderType *string
var amountUsd *float64
var branchID int64
if err := r.client.QueryRow(ctx,
`SELECT type, amount_usd, branch_id FROM visa_master_payment_orders WHERE id = $1 AND deleted_at IS NULL`, orderID,
).Scan(&orderType, &amountUsd, &branchID); err != nil {
println("[callback] step 2 FAILED: load order:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 2 ok: type=", derefOrEmpty(orderType), "branchId=", branchID)
finish := func(item visamasterpay.PaymentResultDTO) visamasterpay.PaymentResultDTO {
item.Type = derefOrEmpty(orderType)
@@ -338,69 +345,97 @@ func (r *repository) HandleCallback(ctx context.Context, paymentID int64) (visam
return item
}
println("[callback] step 3: checking gateway status")
if gatewayStatus != "pending" {
println("[callback] step 3: already finalized, gatewayStatus=", gatewayStatus)
item, err := fetchPaymentByID(ctx, r.client, paymentID)
if err != nil {
println("[callback] step 3 FAILED: fetch payment:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
item.Paid = item.GatewayStatus == "success"
println("[callback] step 3 done: returning cached result, paid=", item.Paid)
return finish(item), nil
}
println("[callback] step 3 ok: still pending")
println("[callback] step 4: checking gateway reference")
if gatewayReference == nil || *gatewayReference == "" {
println("[callback] step 4 FAILED: no gateway reference yet")
return visamasterpay.PaymentResultDTO{}, fmt.Errorf("payment %d has no gateway reference yet", paymentID)
}
println("[callback] step 4 ok: gbusOrderId=", *gatewayReference)
println("[callback] step 5: fetching branch credentials for branchId=", branchID)
username, password, err := r.branchCredentials(ctx, branchID, isSber(orderType))
if err != nil {
println("[callback] step 5 FAILED: branch credentials:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 5 ok")
r.logStep(ctx, orderID, createdBy, "callback_received", map[string]interface{}{"paymentId": paymentID})
r.logStep(ctx, orderID, createdBy, "status_check_request", map[string]interface{}{"gbusOrderId": *gatewayReference})
println("[callback] step 6: sending status check request to gateway")
status, err := r.gateway.CheckStatus(ctx, paymentgateway.StatusRequest{OrderId: *gatewayReference, Username: username, Password: password})
if err != nil {
println("[callback] step 6 FAILED: check status:", err.Error())
r.logStep(ctx, orderID, createdBy, "status_check_response", map[string]interface{}{"error": errString(err)})
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 6 ok: paid=", status.Paid, "orderStatus=", status.OrderStatus, "actionCode=", status.ActionCode)
r.logStep(ctx, orderID, createdBy, "status_check_response", map[string]interface{}{
"paid": status.Paid, "orderStatus": status.OrderStatus, "actionCode": status.ActionCode,
"actionCodeDescription": status.ActionCodeDescription, "rawResponse": rawJSON(status.RawResponse),
})
println("[callback] step 7: finalizing payment, paid=", status.Paid)
var item visamasterpay.PaymentResultDTO
if status.Paid {
println("[callback] step 7a: starting transaction")
tx, err := r.client.Begin(ctx)
if err != nil {
println("[callback] step 7a FAILED: begin tx:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
defer tx.Rollback(ctx)
println("[callback] step 7b: locking order row")
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 {
println("[callback] step 7b FAILED: lock order:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 7b ok: lockedPaid=", lockedPaid)
if !lockedPaid {
println("[callback] step 7c: marking order paid")
if _, err := tx.Exec(ctx,
`UPDATE visa_master_payment_orders SET paid = true, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $1`,
orderID,
); err != nil {
println("[callback] step 7c FAILED: mark order paid:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
}
println("[callback] step 7d: updating payment status to success")
item, err = updatePaymentStatus(ctx, tx, paymentID, "success", status.RawResponse)
if err != nil {
println("[callback] step 7d FAILED:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
println("[callback] step 7e: committing transaction")
if err := tx.Commit(ctx); err != nil {
println("[callback] step 7e FAILED: commit tx:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
} else {
println("[callback] step 7f: updating payment status to failed")
item, err = updatePaymentStatus(ctx, r.client, paymentID, "failed", status.RawResponse)
if err != nil {
println("[callback] step 7f FAILED:", err.Error())
return visamasterpay.PaymentResultDTO{}, err
}
}
@@ -408,6 +443,7 @@ func (r *repository) HandleCallback(ctx context.Context, paymentID int64) (visam
r.logStep(ctx, orderID, createdBy, "payment_finalized", map[string]interface{}{"paymentId": paymentID, "paid": status.Paid})
item.Paid = status.Paid
println("[callback] step 8 done: paymentId=", paymentID, "paid=", item.Paid)
return finish(item), nil
}

View File

@@ -73,11 +73,33 @@ func (c *HTTPClient) Register(ctx context.Context, req RegisterRequest) (Registe
}
type statusResponse struct {
OrderStatus int `json:"orderStatus"`
ActionCode string `json:"actionCode"`
ActionCodeDescription string `json:"actionCodeDescription"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
OrderStatus int `json:"orderStatus"`
ActionCode flexibleString `json:"actionCode"`
ActionCodeDescription string `json:"actionCodeDescription"`
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
// flexibleString unmarshals a JSON string or a JSON number into a Go
// string. GBÜS is documented to send actionCode as a string, but some
// deployments send it as a bare number, so decoding must accept either.
type flexibleString string
func (s *flexibleString) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
*s = ""
return nil
}
if len(data) > 0 && data[0] == '"' {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
*s = flexibleString(str)
return nil
}
*s = flexibleString(data)
return nil
}
func (c *HTTPClient) CheckStatus(ctx context.Context, req StatusRequest) (StatusResult, error) {
@@ -101,7 +123,7 @@ func (c *HTTPClient) CheckStatus(ctx context.Context, req StatusRequest) (Status
return StatusResult{
Paid: parsed.OrderStatus == 2,
OrderStatus: parsed.OrderStatus,
ActionCode: parsed.ActionCode,
ActionCode: string(parsed.ActionCode),
ActionCodeDescription: parsed.ActionCodeDescription,
ErrorCode: parsed.ErrorCode,
ErrorMessage: parsed.ErrorMessage,