// Package paymenthistory exposes a read-only, branch-scoped audit view over // every visa/master/sber payout attempt (visa_master_order_payments) - the // full trail visamasterpay.PaymentResultDTO deliberately keeps per-order // (see its doc comment), denormalized here with order/branch/operator info // so an admin/operator can browse and filter payment history across orders // without N+1 calls. package paymenthistory import ( "encoding/json" "time" "online-back/internal/shared" ) // PaymentHistoryItemDTO is one payout attempt (paid, failed, or still // pending) enriched with the order/branch/client/operator context needed to // display it in a history list, without requiring a separate lookup per row. 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"` BranchName shared.LocalizedText `json:"branchName"` PassportName string `json:"passportName"` PassportSurname string `json:"passportSurname"` UserId int64 `json:"userId"` Paid bool `json:"paid"` AmountTmt float64 `json:"amountTmt"` BankFeeTmt float64 `json:"bankFeeTmt"` GbusFeeTmt *float64 `json:"gbusFeeTmt,omitempty"` TvebFeeUsd *float64 `json:"tvebFeeUsd,omitempty"` TvebFeeTmt *float64 `json:"tvebFeeTmt,omitempty"` TotalAmountTmt float64 `json:"totalAmountTmt"` // AmountMinor is the amount registered with GBÜS, in tetri. AmountMinor int64 `json:"amountMinor"` // AmountUsd is amount_tmt converted back at the attempt's own // exchange_rate_usd - the USD figure the payout was quoted in. AmountUsd float64 `json:"amountUsd"` ExchangeRateUsd float64 `json:"exchangeRateUsd"` ExchangeRateRub *float64 `json:"exchangeRateRub,omitempty"` PaymentPeriod time.Time `json:"paymentPeriod"` GatewayStatus string `json:"gatewayStatus"` GatewayReference *string `json:"gatewayReference,omitempty"` // ApiClient is the branch's GBÜS merchant username for this order type // (visa/master or sber), ApiClientName the branch setting it comes from // (billing_visa_master_username or billing_sber_username), and // Description the text sent with register.do - all as visamasterpay's // Pay picks them. ApiClient string `json:"apiClient"` ApiClientName string `json:"apiClientName"` Description string `json:"description"` // CardholderName/MaskedPan come from GBÜS's getOrderStatusExtended // cardAuthInfo, so they're only known once an attempt has been // finalized by the callback/status check (unset while pending, and for // attempts that never reached the card form). CardholderName *string `json:"cardholderName,omitempty"` MaskedPan *string `json:"maskedPan,omitempty"` // FormUrl is GBÜS's hosted payment page and SuccessUrl the returnUrl it // was registered with - set for every attempt register.do accepted, // finalized ones included (unset if registration failed). FormUrl *string `json:"formUrl,omitempty"` SuccessUrl *string `json:"successUrl,omitempty"` // CallbackStatus is CallbackReceived once GBÜS's redirect reached the // callback for this attempt, at CallbackAt (its first time); both are // unset if it never did (e.g. finalized by a /status check instead). CallbackStatus *string `json:"callbackStatus,omitempty"` CallbackAt *time.Time `json:"callbackAt,omitempty"` CreatedById *int64 `json:"createdById,omitempty"` CreatedByName string `json:"createdByName,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` } // CallbackReceived is PaymentHistoryItemDTO.CallbackStatus once the GBÜS // callback reached an attempt. const CallbackReceived = "received" // PaymentHistoryDetailDTO is a single payout attempt with the raw GBÜS // response kept alongside it - the list deliberately leaves the jsonb out // so pages stay small. type PaymentHistoryDetailDTO struct { PaymentHistoryItemDTO GatewayResponse json.RawMessage `json:"gatewayResponse,omitempty" swaggertype:"object"` } // PaymentGatewayDTO is one payment the way the per-order payment table // shows it (paid or not, from which card, when, for which month) together // with the last GBÜS (HALKBANK) response stored for it. Card fields are // null until GBÜS reports cardAuthInfo; gatewayResponse is null if GBÜS // never answered. type PaymentGatewayDTO struct { Id int64 `json:"id"` OrderId int64 `json:"orderId"` CardholderName *string `json:"cardholderName"` MaskedPan *string `json:"maskedPan"` PaymentYear int `json:"paymentYear"` PaymentMonth int `json:"paymentMonth"` CreatedAt time.Time `json:"createdAt"` AmountUsd float64 `json:"amountUsd"` AmountTmt float64 `json:"amountTmt"` OrderNumber string `json:"orderNumber"` Paid bool `json:"paid"` GatewayStatus string `json:"gatewayStatus"` GatewayResponse json.RawMessage `json:"gatewayResponse" swaggertype:"object"` } // NewPaymentGatewayDTO trims a payment detail down to PaymentGatewayDTO. func NewPaymentGatewayDTO(detail PaymentHistoryDetailDTO) PaymentGatewayDTO { gatewayResponse := detail.GatewayResponse if len(gatewayResponse) == 0 { gatewayResponse = json.RawMessage("null") } return PaymentGatewayDTO{ Id: detail.Id, OrderId: detail.OrderId, CardholderName: detail.CardholderName, MaskedPan: detail.MaskedPan, PaymentYear: detail.PaymentPeriod.Year(), PaymentMonth: int(detail.PaymentPeriod.Month()), CreatedAt: detail.CreatedAt, AmountUsd: detail.AmountUsd, AmountTmt: detail.AmountTmt, OrderNumber: detail.OrderNumber, Paid: detail.Paid, GatewayStatus: detail.GatewayStatus, GatewayResponse: gatewayResponse, } } // ListFilter narrows the history list. BranchIds is populated by // rbac.BindBranchIDs and takes priority over BranchId when set (see // rbac.AppendBranchIDs), the same way visamaster.ListFilter works. type ListFilter struct { OrderId *int64 BranchId *int64 BranchIds []int64 UserId *int64 Status *string // gateway_status: pending/success/failed Search string // matches order passport name/surname or unique id Sort string // one of SortColumns' keys; empty means id Desc bool Page int Limit int } // SortColumns maps the accepted ?sort= values to their SQL expressions. var SortColumns = map[string]string{ "id": "p.id", "created_at": "p.created_at", "status": "p.gateway_status", "amount": "p.amount_tmt", } type ListPaymentHistoryDTO struct { Count int64 `json:"count"` Items []PaymentHistoryItemDTO `json:"items"` } // PaymentLogDTO is one step-by-step entry from visa_master_payment_logs - // the granular audit trail recorded while processing an order's payout // (see visamasterpay's logStep), independent of and more detailed than // the summary rows in visa_master_order_payments. type PaymentLogDTO struct { Id int64 `json:"id"` OrderId int64 `json:"orderId"` Step string `json:"step"` Detail json.RawMessage `json:"detail,omitempty" swaggertype:"object"` CreatedById *int64 `json:"createdById,omitempty"` CreatedByName string `json:"createdByName,omitempty"` CreatedAt time.Time `json:"createdAt"` }