From 599c12ef872695e3724b99e4adc4922cd78ad7c4 Mon Sep 17 00:00:00 2001 From: GuwandykA Date: Sat, 19 Sep 2026 04:47:43 +0500 Subject: [PATCH] add some --- ...yment_settings_and_visamaster_pay.down.sql | 9 +++- ...payment_settings_and_visamaster_pay.up.sql | 47 ++++++++++--------- docs/docs.go | 3 ++ docs/swagger.json | 3 ++ docs/swagger.yaml | 2 + .../payment/visamasterpay/db/postgresql.go | 19 ++++++++ internal/payment/visamasterpay/dto.go | 4 ++ internal/payment/visamasterpay/handler.go | 2 +- internal/payment/visamasterpay/storage.go | 4 ++ 9 files changed, 68 insertions(+), 25 deletions(-) diff --git a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql index 67a0f97..63b1527 100644 --- a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql +++ b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.down.sql @@ -1,8 +1,15 @@ --- Reverse 20260918130000_add_payment_settings_and_visamaster_pay.up.sql +-- Reverse 20260918130000_add_payment_settings_and_visamaster_pay.up.sql, +-- in reverse order of that file's sections. + +-- 4. visa_master_payment_logs DROP TABLE IF EXISTS visa_master_payment_logs; + +-- 3. visa_master_order_payments DROP TABLE IF EXISTS visa_master_order_payments; +-- 2. visa_master_payment_orders.amount_usd ALTER TABLE visa_master_payment_orders DROP COLUMN IF EXISTS amount_usd; +-- 1. payment_settings DROP TABLE IF EXISTS payment_settings; diff --git a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql index 3971f7f..d337eba 100644 --- a/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql +++ b/db/migrations/20260918130000_add_payment_settings_and_visamaster_pay.up.sql @@ -1,27 +1,12 @@ -- Adds the "Töleg" payment feature for Visa/Master/Sber money-transfer --- orders: --- 1. payment_settings is a single-row settings table for the admin- --- configurable commission fees used on every payment. Visa/Master --- orders use bank_fee_tmt + gbus_fee_tmt; Sber orders use their own --- sber_bank_fee_tmt plus a USD-denominated tveb_fee_usd (converted to --- TMT at payout time via the USD rate) - Sber has no GBÜS fee. --- 2. visa_master_payment_orders gains only the requested USD amount --- (order data, set when the order is created). It already has `paid`. --- 3. visa_master_order_payments records every payout attempt for an --- order as its own row - kept as a full audit trail (including failed --- gateway attempts) rather than columns on the order itself, so a --- retry after a failure doesn't erase what the previous attempt did. --- The Sber-specific columns (exchange_rate_rub, tveb_fee_usd, --- tveb_fee_tmt) stay null for non-Sber attempts, same as --- gbus_fee_tmt stays null for Sber ones. --- 4. visa_master_payment_logs records every individual step taken while --- processing a payout (validated, rate fetched, gateway request sent, --- gateway response received, order marked paid/attempt recorded) as --- its own row, so there's a step-by-step, timestamped record of what --- actually happened for a given order - evidence if a payout is later --- disputed, independent of (and more granular than) the one summary --- row in visa_master_order_payments. +-- orders. Each section below is a distinct, independently reversible piece +-- of that feature - see the matching comment in each section for why it's +-- shaped the way it is. +-- 1. Single-row, admin-configurable commission fees used on every payment. +-- Visa/Master orders use bank_fee_tmt + gbus_fee_tmt; Sber orders use their +-- own sber_bank_fee_tmt plus a USD-denominated tveb_fee_usd (converted to +-- TMT at payout time via the USD rate) - Sber has no GBÜS fee. CREATE TABLE IF NOT EXISTS payment_settings ( id smallint PRIMARY KEY DEFAULT 1, bank_fee_tmt numeric(12, 2) NOT NULL DEFAULT 100, @@ -39,9 +24,19 @@ INSERT INTO payment_settings (id, bank_fee_tmt, gbus_fee_tmt, sber_bank_fee_tmt, VALUES (1, 100, 15, 100, 18) ON CONFLICT (id) DO NOTHING; +-- 2. The requested USD amount for a visa/master/sber order (order data, set +-- when the order is created). visa_master_payment_orders already has `paid`. ALTER TABLE visa_master_payment_orders - ADD COLUMN amount_usd numeric(18, 2); + ADD COLUMN IF NOT EXISTS amount_usd numeric(18, 2); +-- 3. Every payout attempt for an order, as its own row - a full audit trail +-- (including failed gateway attempts) rather than columns on the order +-- itself, so a retry after a failure doesn't erase what the previous +-- attempt did. The Sber-specific columns (exchange_rate_rub, tveb_fee_usd, +-- tveb_fee_tmt) stay null for non-Sber attempts, same as gbus_fee_tmt stays +-- null for Sber ones. gateway_status/gateway_reference/gateway_response +-- track the GBÜS register→callback round trip (pending until the callback +-- verifies the outcome server-to-server). CREATE TABLE IF NOT EXISTS visa_master_order_payments ( id bigserial PRIMARY KEY, order_id bigint NOT NULL, @@ -73,6 +68,12 @@ CREATE TABLE IF NOT EXISTS visa_master_order_payments ( CREATE INDEX IF NOT EXISTS visa_master_order_payments_order_id_idx ON visa_master_order_payments (order_id); CREATE INDEX IF NOT EXISTS visa_master_order_payments_user_id_idx ON visa_master_order_payments (user_id); +-- 4. Every individual step taken while processing a payout (validated, rate +-- fetched, gateway request sent, gateway response received, order marked +-- paid/attempt recorded) as its own row, so there's a step-by-step, +-- timestamped record of what actually happened for a given order - +-- evidence if a payout is later disputed, independent of (and more +-- granular than) the one summary row in visa_master_order_payments. CREATE TABLE IF NOT EXISTS visa_master_payment_logs ( id bigserial PRIMARY KEY, order_id bigint NOT NULL, diff --git a/docs/docs.go b/docs/docs.go index 3b3d27e..4a208eb 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -9901,6 +9901,9 @@ const docTemplate = `{ "maxAmountTmt": { "type": "number" }, + "payableToday": { + "type": "boolean" + }, "tvebFeeTmt": { "type": "number" }, diff --git a/docs/swagger.json b/docs/swagger.json index 287c0e2..a16b627 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -9895,6 +9895,9 @@ "maxAmountTmt": { "type": "number" }, + "payableToday": { + "type": "boolean" + }, "tvebFeeTmt": { "type": "number" }, diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8b53f10..d8582a8 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1714,6 +1714,8 @@ definitions: type: number maxAmountTmt: type: number + payableToday: + type: boolean tvebFeeTmt: type: number tvebFeeUsd: diff --git a/internal/payment/visamasterpay/db/postgresql.go b/internal/payment/visamasterpay/db/postgresql.go index 4684c17..63b9836 100644 --- a/internal/payment/visamasterpay/db/postgresql.go +++ b/internal/payment/visamasterpay/db/postgresql.go @@ -101,6 +101,7 @@ func (r *repository) Quote(ctx context.Context, orderID int64) (visamasterpay.Pa Type: derefOrEmpty(orderType), ExchangeRateUsd: usdRate, MaxAmountTmt: round2(*amountUsd * usdRate), + PayableToday: isPayableDay(time.Now()), } if isSber(orderType) { @@ -142,6 +143,10 @@ func (r *repository) Pay(ctx context.Context, orderID int64, dto visamasterpay.P if err != nil { return visamasterpay.PaymentInitResultDTO{}, err } + if !isPayableDay(time.Now()) { + r.logStep(ctx, orderID, dto.CreatedBy, "rejected", map[string]string{"reason": "payment_window_closed"}) + return visamasterpay.PaymentInitResultDTO{}, visamasterpay.ErrPaymentWindowClosed + } if paid { r.logStep(ctx, orderID, dto.CreatedBy, "rejected", map[string]string{"reason": "already_paid"}) return visamasterpay.PaymentInitResultDTO{}, visamasterpay.ErrAlreadyPaid @@ -411,6 +416,20 @@ func (r *repository) fetchRate(ctx context.Context, from string) (float64, error return strconv.ParseFloat(result.Currencies[0].Value.String(), 64) } +// ashgabatZone is Turkmenistan's fixed UTC+5 offset (no DST observed). +var ashgabatZone = time.FixedZone("+05", 5*60*60) + +// isPayableDay reports whether GBÜS payouts can be initiated on t's +// Ashgabat-local calendar day - false on Saturday/Sunday, since GBÜS/the +// bank does not process payouts on weekends. Because Saturday and Sunday +// are always adjacent, this alone also produces the right month-end cutoff +// (e.g. if the 31st is a Sunday, the 30th is a Saturday, so the 29th - +// Friday - is the last payable day) without any separate month-end logic. +func isPayableDay(t time.Time) bool { + wd := t.In(ashgabatZone).Weekday() + return wd != time.Saturday && wd != time.Sunday +} + func isSber(orderType *string) bool { return orderType != nil && strings.EqualFold(strings.TrimSpace(*orderType), orderTypeSber) } diff --git a/internal/payment/visamasterpay/dto.go b/internal/payment/visamasterpay/dto.go index 29ed0af..326e443 100644 --- a/internal/payment/visamasterpay/dto.go +++ b/internal/payment/visamasterpay/dto.go @@ -12,6 +12,9 @@ import ( // Sber orders have no GBÜS fee but add a USD-denominated TVEB fee (shown // both as-is and converted to TMT) and also show the RUB rate for // reference, using their own SberBankFeeTmt instead of BankFeeTmt. +// PayableToday is false on Saturday/Sunday (Asia/Ashgabat) - GBÜS/the bank +// does not process payouts on weekends, so the frontend should disable the +// Pay action rather than let the request fail. type PaymentQuoteDTO struct { Type string `json:"type"` ExchangeRateUsd float64 `json:"exchangeRateUsd"` @@ -21,6 +24,7 @@ type PaymentQuoteDTO struct { TvebFeeUsd *float64 `json:"tvebFeeUsd,omitempty"` TvebFeeTmt *float64 `json:"tvebFeeTmt,omitempty"` MaxAmountTmt float64 `json:"maxAmountTmt"` + PayableToday bool `json:"payableToday"` } // PayOrderDTO starts a payout attempt for a visa/master/sber order. diff --git a/internal/payment/visamasterpay/handler.go b/internal/payment/visamasterpay/handler.go index df4b798..f37cbab 100644 --- a/internal/payment/visamasterpay/handler.go +++ b/internal/payment/visamasterpay/handler.go @@ -171,7 +171,7 @@ func (h *handler) handleError(c *gin.Context, err error, action string) bool { switch { case errors.Is(err, pgx.ErrNoRows): c.JSON(http.StatusNotFound, appresult.ErrNotFound) - case errors.Is(err, ErrAlreadyPaid), errors.Is(err, ErrAmountNotSet), errors.Is(err, ErrAmountExceedsMax): + case errors.Is(err, ErrAlreadyPaid), errors.Is(err, ErrAmountNotSet), errors.Is(err, ErrAmountExceedsMax), errors.Is(err, ErrPaymentWindowClosed): c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable) default: h.logger.Errorf("%s visa master payment order failed: %v", action, err) diff --git a/internal/payment/visamasterpay/storage.go b/internal/payment/visamasterpay/storage.go index fd3cbdc..3181dfd 100644 --- a/internal/payment/visamasterpay/storage.go +++ b/internal/payment/visamasterpay/storage.go @@ -20,6 +20,10 @@ var ErrAmountExceedsMax = errors.New("amount exceeds the order's requested amoun // attempt id that doesn't exist. var ErrPaymentNotFound = errors.New("payment attempt not found") +// ErrPaymentWindowClosed is returned when today is not a business day +// (Saturday/Sunday) - GBÜS/the bank does not process payouts on weekends. +var ErrPaymentWindowClosed = errors.New("payment window is closed on weekends") + type Repository interface { // OwnerInfo returns the order's owner/branch so the handler can run // rbac.GuardOrder without importing cards/visamaster.