add order number
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
DROP INDEX IF EXISTS visa_master_order_payments_order_number_idx;
|
||||
ALTER TABLE visa_master_order_payments DROP COLUMN IF EXISTS order_number;
|
||||
13
db/migrations/20260929190000_payment_order_number.up.sql
Normal file
13
db/migrations/20260929190000_payment_order_number.up.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
-- order_number is the orderNumber sent to GBÜS register.do and shown on its
|
||||
-- hosted payment page ("Номер заказа"): ddMMyyHHmmss in Ashgabat time, e.g.
|
||||
-- 290926133808 for 29.09.2026 13:38:08. GBÜS rejects a repeated orderNumber
|
||||
-- per merchant, hence the unique index. Rows made before this column keep
|
||||
-- the vmo-<order>-<payment> number they were actually registered with.
|
||||
ALTER TABLE visa_master_order_payments ADD COLUMN IF NOT EXISTS order_number character varying(32);
|
||||
|
||||
UPDATE visa_master_order_payments
|
||||
SET order_number = 'vmo-' || order_id || '-' || id
|
||||
WHERE order_number IS NULL;
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS visa_master_order_payments_order_number_idx
|
||||
ON visa_master_order_payments (order_number);
|
||||
@@ -30,7 +30,7 @@ JOIN visa_master_payment_orders o ON o.id = p.order_id
|
||||
LEFT JOIN branches b ON b.id = o.branch_id
|
||||
LEFT JOIN users u ON u.id = p.created_by`
|
||||
|
||||
const historySelectColumns = `p.id, p.order_id, COALESCE(o.unique_id, ''), COALESCE(o.type, ''),
|
||||
const historySelectColumns = `p.id, p.order_id, COALESCE(p.order_number, ''), COALESCE(o.unique_id, ''), COALESCE(o.type, ''),
|
||||
o.branch_id, b.name, COALESCE(o.passport_name, ''), COALESCE(o.passport_surname, ''), p.user_id,
|
||||
p.amount_tmt, p.bank_fee_tmt, p.gbus_fee_tmt, p.tveb_fee_usd, p.tveb_fee_tmt, p.total_amount_tmt,
|
||||
p.exchange_rate_usd, p.exchange_rate_rub, p.payment_period,
|
||||
@@ -146,7 +146,7 @@ func buildListQuery(filter paymenthistory.ListFilter) (whereClause string, args
|
||||
args = append(args, shared.LikePattern(filter.Search))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
o.passport_name ILIKE $%[1]d OR o.passport_surname ILIKE $%[1]d OR o.unique_id ILIKE $%[1]d
|
||||
o.passport_name ILIKE $%[1]d OR o.passport_surname ILIKE $%[1]d OR o.unique_id ILIKE $%[1]d OR p.order_number ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ func (r *repository) scanHistoryItem(row pgx.Row) (paymenthistory.PaymentHistory
|
||||
var branchNameJSON []byte
|
||||
var gatewayResponse []byte
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.OrderId, &item.OrderUniqueId, &item.Type,
|
||||
&item.Id, &item.OrderId, &item.OrderNumber, &item.OrderUniqueId, &item.Type,
|
||||
&item.BranchId, &branchNameJSON, &item.PassportName, &item.PassportSurname, &item.UserId,
|
||||
&item.AmountTmt, &item.BankFeeTmt, &item.GbusFeeTmt, &item.TvebFeeUsd, &item.TvebFeeTmt, &item.TotalAmountTmt,
|
||||
&item.ExchangeRateUsd, &item.ExchangeRateRub, &item.PaymentPeriod,
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
type PaymentHistoryItemDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
OrderId int64 `json:"orderId"`
|
||||
OrderNumber string `json:"orderNumber"`
|
||||
OrderUniqueId string `json:"orderUniqueId"`
|
||||
Type string `json:"type"`
|
||||
BranchId int64 `json:"branchId"`
|
||||
|
||||
@@ -59,3 +59,11 @@ func TestIsPayableDay_GuardIsDisabled(t *testing.T) {
|
||||
t.Error("with skipPayableDayCheck=true, weekends must still report payable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewOrderNumber(t *testing.T) {
|
||||
// 29.09.2026 08:38:08 UTC is 13:38:08 in Ashgabat (+05).
|
||||
at := time.Date(2026, 9, 29, 8, 38, 8, 0, time.UTC)
|
||||
if got, want := newOrderNumber(at), "290926133808"; got != want {
|
||||
t.Errorf("newOrderNumber(%v)=%q want %q", at, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
// plus a RUB reference rate.
|
||||
const orderTypeSber = "sber"
|
||||
|
||||
const paymentReturningColumns = `id, order_id, payment_period, exchange_rate_usd, exchange_rate_rub, amount_tmt,
|
||||
const paymentReturningColumns = `id, order_id, order_number, payment_period, exchange_rate_usd, exchange_rate_rub, amount_tmt,
|
||||
bank_fee_tmt, gbus_fee_tmt, tveb_fee_usd, tveb_fee_tmt, total_amount_tmt,
|
||||
gateway_status, gateway_reference, created_at, updated_at`
|
||||
|
||||
@@ -226,7 +226,7 @@ func (r *repository) Pay(ctx context.Context, orderID int64, dto visamasterpay.P
|
||||
return visamasterpay.PaymentInitResultDTO{}, err
|
||||
}
|
||||
|
||||
paymentID, err := insertPendingPayment(ctx, r.client, insertParams{
|
||||
paymentID, orderNumber, err := r.createPendingPayment(ctx, insertParams{
|
||||
orderID: orderID, userID: userID, period: period,
|
||||
rateUsd: usdRate, rateRub: rubRate, amountTmt: amountTmt,
|
||||
bankFeeTmt: bankFeeTmt, gbusFeeTmt: gbusFeeTmt, tvebFeeUsd: tvebFeeUsd, tvebFeeTmt: tvebFeeTmt,
|
||||
@@ -238,11 +238,10 @@ func (r *repository) Pay(ctx context.Context, orderID int64, dto visamasterpay.P
|
||||
}
|
||||
|
||||
r.logStep(ctx, orderID, dto.CreatedBy, "payment_record_created", map[string]interface{}{
|
||||
"paymentId": paymentID, "paymentPeriod": period.Format("2006-01-02"), "amountTmt": amountTmt, "totalAmountTmt": total,
|
||||
"paymentId": paymentID, "orderNumber": orderNumber, "paymentPeriod": period.Format("2006-01-02"), "amountTmt": amountTmt, "totalAmountTmt": total,
|
||||
})
|
||||
|
||||
returnURL := fmt.Sprintf("%s/online/visa-master", r.publicBaseURL)
|
||||
orderNumber := fmt.Sprintf("vmo-%d-%d", orderID, paymentID)
|
||||
description := "Visa/Master tölegi"
|
||||
if sber {
|
||||
description = "Sber tölegi"
|
||||
@@ -291,7 +290,7 @@ func (r *repository) Pay(ctx context.Context, orderID int64, dto visamasterpay.P
|
||||
"paymentId": paymentID, "gbusOrderId": ref, "formUrl": result.FormUrl,
|
||||
})
|
||||
|
||||
return visamasterpay.PaymentInitResultDTO{PaymentId: paymentID, OrderId: orderID, FormUrl: result.FormUrl, SuccessUrl: returnURL}, nil
|
||||
return visamasterpay.PaymentInitResultDTO{PaymentId: paymentID, OrderId: orderID, OrderNumber: orderNumber, FormUrl: result.FormUrl, SuccessUrl: returnURL}, nil
|
||||
}
|
||||
|
||||
func (r *repository) HandleCallback(ctx context.Context, paymentID int64) (visamasterpay.PaymentResultDTO, error) {
|
||||
@@ -563,6 +562,7 @@ type execer interface {
|
||||
|
||||
type insertParams struct {
|
||||
orderID, userID int64
|
||||
orderNumber string
|
||||
period time.Time
|
||||
rateUsd float64
|
||||
rateRub *float64
|
||||
@@ -575,6 +575,41 @@ type insertParams struct {
|
||||
createdBy *int64
|
||||
}
|
||||
|
||||
// orderNumberLayout formats a GBÜS orderNumber as ddMMyyHHmmss, e.g.
|
||||
// 290926133808 for 29.09.2026 13:38:08 Ashgabat time.
|
||||
const orderNumberLayout = "020106150405"
|
||||
|
||||
// orderNumberAttempts bounds how many following seconds createPendingPayment
|
||||
// tries when the current one's number is already taken.
|
||||
const orderNumberAttempts = 10
|
||||
|
||||
// newOrderNumber is the GBÜS orderNumber for a payment created at t.
|
||||
func newOrderNumber(t time.Time) string {
|
||||
return t.In(ashgabatZone).Format(orderNumberLayout)
|
||||
}
|
||||
|
||||
// createPendingPayment inserts the attempt row under a fresh orderNumber.
|
||||
// Numbers have one-second resolution and must be unique (GBÜS rejects a
|
||||
// repeat), so when two payments start in the same second the later one
|
||||
// moves on to the next free second.
|
||||
func (r *repository) createPendingPayment(ctx context.Context, p insertParams) (int64, string, error) {
|
||||
now := time.Now()
|
||||
var err error
|
||||
for i := 0; i < orderNumberAttempts; i++ {
|
||||
p.orderNumber = newOrderNumber(now.Add(time.Duration(i) * time.Second))
|
||||
var id int64
|
||||
id, err = insertPendingPayment(ctx, r.client, p)
|
||||
if err == nil {
|
||||
return id, p.orderNumber, nil
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if !errors.As(err, &pgErr) || pgErr.Code != "23505" || pgErr.ConstraintName != "visa_master_order_payments_order_number_idx" {
|
||||
return 0, "", err
|
||||
}
|
||||
}
|
||||
return 0, "", fmt.Errorf("no free order number after %d attempts: %w", orderNumberAttempts, err)
|
||||
}
|
||||
|
||||
// insertPendingPayment creates the attempt row before GBÜS is even called,
|
||||
// so its id can be embedded in the returnUrl - see Pay.
|
||||
func insertPendingPayment(ctx context.Context, q querier, p insertParams) (int64, error) {
|
||||
@@ -582,15 +617,15 @@ func insertPendingPayment(ctx context.Context, q querier, p insertParams) (int64
|
||||
INSERT INTO visa_master_order_payments (
|
||||
order_id, user_id, payment_period, exchange_rate_usd, exchange_rate_rub, amount_tmt,
|
||||
bank_fee_tmt, gbus_fee_tmt, tveb_fee_usd, tveb_fee_tmt, total_amount_tmt,
|
||||
gateway_status, created_by
|
||||
gateway_status, created_by, order_number
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'pending', $12)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, 'pending', $12, $13)
|
||||
RETURNING id`
|
||||
|
||||
var id int64
|
||||
err := q.QueryRow(ctx, query,
|
||||
p.orderID, p.userID, p.period, p.rateUsd, p.rateRub, p.amountTmt,
|
||||
p.bankFeeTmt, p.gbusFeeTmt, p.tvebFeeUsd, p.tvebFeeTmt, p.total, p.createdBy,
|
||||
p.bankFeeTmt, p.gbusFeeTmt, p.tvebFeeUsd, p.tvebFeeTmt, p.total, p.createdBy, p.orderNumber,
|
||||
).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
@@ -628,7 +663,7 @@ func fetchPaymentByID(ctx context.Context, q querier, paymentID int64) (visamast
|
||||
func scanPayment(row pgx.Row) (visamasterpay.PaymentResultDTO, error) {
|
||||
var item visamasterpay.PaymentResultDTO
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.OrderId, &item.PaymentPeriod, &item.ExchangeRateUsd, &item.ExchangeRateRub, &item.AmountTmt,
|
||||
&item.Id, &item.OrderId, &item.OrderNumber, &item.PaymentPeriod, &item.ExchangeRateUsd, &item.ExchangeRateRub, &item.AmountTmt,
|
||||
&item.BankFeeTmt, &item.GbusFeeTmt, &item.TvebFeeUsd, &item.TvebFeeTmt, &item.TotalAmountTmt,
|
||||
&item.GatewayStatus, &item.GatewayReference, &item.CreatedAt, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
|
||||
@@ -49,10 +49,11 @@ type PayOrderDTO struct {
|
||||
// request as ReturnURL) - the frontend can watch for navigation to it
|
||||
// inside the hosted-payment-page webview to know the flow has ended.
|
||||
type PaymentInitResultDTO struct {
|
||||
PaymentId int64 `json:"paymentId"`
|
||||
OrderId int64 `json:"orderId"`
|
||||
FormUrl string `json:"formUrl"`
|
||||
SuccessUrl string `json:"successUrl"`
|
||||
PaymentId int64 `json:"paymentId"`
|
||||
OrderId int64 `json:"orderId"`
|
||||
OrderNumber string `json:"orderNumber"`
|
||||
FormUrl string `json:"formUrl"`
|
||||
SuccessUrl string `json:"successUrl"`
|
||||
}
|
||||
|
||||
// CallbackResultDTO is what the GBÜS callback responds with, once GBÜS
|
||||
@@ -72,6 +73,7 @@ type CallbackResultDTO struct {
|
||||
type PaymentResultDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
OrderId int64 `json:"orderId"`
|
||||
OrderNumber string `json:"orderNumber"`
|
||||
Type string `json:"type"`
|
||||
Paid bool `json:"paid"`
|
||||
AmountUsd float64 `json:"amountUsd"`
|
||||
|
||||
Reference in New Issue
Block a user