ch some
This commit is contained in:
1 parent
febd190d6e
commit
235ef5ec9e
8 files changed
+96
-19
No files matched your search
@@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -15,12 +16,13 @@ import (
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
publicBaseURL string
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) paymenthistory.Repository {
|
||||
return &repository{client: client, logger: logger}
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger, publicBaseURL string) paymenthistory.Repository {
|
||||
return &repository{client: client, logger: logger, publicBaseURL: strings.TrimRight(publicBaseURL, "/")}
|
||||
}
|
||||
|
||||
const historyFromClause = ` FROM visa_master_order_payments p
|
||||
@@ -32,7 +34,7 @@ const historySelectColumns = `p.id, p.order_id, COALESCE(o.unique_id, ''), COALE
|
||||
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,
|
||||
p.gateway_status, p.gateway_reference, p.created_by, ` + shared.UserDisplaySQL + `,
|
||||
p.gateway_status, p.gateway_reference, p.gateway_response, p.created_by, ` + shared.UserDisplaySQL + `,
|
||||
p.created_at, p.updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter paymenthistory.ListFilter) (paymenthistory.ListPaymentHistoryDTO, error) {
|
||||
@@ -60,7 +62,7 @@ func (r *repository) List(ctx context.Context, filter paymenthistory.ListFilter)
|
||||
|
||||
var items []paymenthistory.PaymentHistoryItemDTO
|
||||
for rows.Next() {
|
||||
item, err := scanHistoryItem(rows)
|
||||
item, err := r.scanHistoryItem(rows)
|
||||
if err != nil {
|
||||
return paymenthistory.ListPaymentHistoryDTO{}, err
|
||||
}
|
||||
@@ -109,16 +111,17 @@ func buildListQuery(filter paymenthistory.ListFilter) (whereClause string, args
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func scanHistoryItem(row pgx.Row) (paymenthistory.PaymentHistoryItemDTO, error) {
|
||||
func (r *repository) scanHistoryItem(row pgx.Row) (paymenthistory.PaymentHistoryItemDTO, error) {
|
||||
var item paymenthistory.PaymentHistoryItemDTO
|
||||
var gatewayStatus string
|
||||
var branchNameJSON []byte
|
||||
var gatewayResponse []byte
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.OrderId, &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,
|
||||
&gatewayStatus, &item.GatewayReference, &item.CreatedById, &item.CreatedByName,
|
||||
&gatewayStatus, &item.GatewayReference, &gatewayResponse, &item.CreatedById, &item.CreatedByName,
|
||||
&item.CreatedAt, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
return paymenthistory.PaymentHistoryItemDTO{}, err
|
||||
@@ -128,5 +131,29 @@ func scanHistoryItem(row pgx.Row) (paymenthistory.PaymentHistoryItemDTO, error)
|
||||
}
|
||||
item.GatewayStatus = gatewayStatus
|
||||
item.Paid = gatewayStatus == "success"
|
||||
|
||||
// formUrl only survives in gateway_response while the attempt is still
|
||||
// pending - once finalized, gateway_response is overwritten with the
|
||||
// status-check response, which has no formUrl, so there's nothing to
|
||||
// resume for a paid/failed row.
|
||||
if formURL := extractFormURL(gatewayResponse); formURL != "" {
|
||||
item.FormUrl = &formURL
|
||||
successURL := fmt.Sprintf("%s/api/admin/visa-master-payment-orders/callback?paymentId=%d", r.publicBaseURL, item.Id)
|
||||
item.SuccessUrl = &successURL
|
||||
}
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func extractFormURL(gatewayResponse []byte) string {
|
||||
if len(gatewayResponse) == 0 {
|
||||
return ""
|
||||
}
|
||||
var parsed struct {
|
||||
FormUrl string `json:"formUrl"`
|
||||
}
|
||||
if err := json.Unmarshal(gatewayResponse, &parsed); err != nil {
|
||||
return ""
|
||||
}
|
||||
return parsed.FormUrl
|
||||
}
|
||||
Reference in new issue
Block a user