feat(tenant): 按租户隔离支付退款与商户配置
This commit is contained in:
@@ -18,6 +18,7 @@ type StartMohongRefundRequest struct {
|
||||
|
||||
type PaymentDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
PaymentNo string `json:"payment_no"`
|
||||
OrderID uint64 `json:"order_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
@@ -42,6 +43,7 @@ type PaymentDTO struct {
|
||||
|
||||
type RefundDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
PaymentNo string `json:"payment_no"`
|
||||
OrderID uint64 `json:"order_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
@@ -79,6 +81,7 @@ type PaginatedResult struct {
|
||||
|
||||
type AdminPaymentDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
PaymentNo string `json:"payment_no"`
|
||||
OrderID uint64 `json:"order_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
@@ -114,6 +115,7 @@ func (r *Repository) StartMohong(ctx context.Context, userID uint64, orderID uin
|
||||
func (r *Repository) prepareMohongPayment(ctx context.Context, userID uint64, orderID uint64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (*model.PaymentOrder, *model.MohongOrder, error) {
|
||||
var paymentID uint64
|
||||
var orderRow model.MohongOrder
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var row model.MohongOrder
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
@@ -137,6 +139,7 @@ func (r *Repository) prepareMohongPayment(ctx context.Context, userID uint64, or
|
||||
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, runtimeConfig.PayWay, "ZFBZF")
|
||||
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, runtimeConfig.JSPayFlag, "2")
|
||||
existing.AmountCent = row.AmountCent
|
||||
existing.TenantID = firstNonZero(existing.TenantID, tenantID)
|
||||
existing.PaymentConfigID = firstNonZero(existing.PaymentConfigID, runtimeConfig.ID)
|
||||
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
|
||||
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
|
||||
@@ -153,7 +156,7 @@ func (r *Repository) prepareMohongPayment(ctx context.Context, userID uint64, or
|
||||
} else if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
payment, err := newMohongPayment(row, req, runtimeConfig)
|
||||
payment, err := newMohongPayment(row, req, runtimeConfig, tenantID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -174,12 +177,13 @@ func (r *Repository) prepareMohongPayment(ctx context.Context, userID uint64, or
|
||||
return payment, &orderRow, nil
|
||||
}
|
||||
|
||||
func newMohongPayment(row model.MohongOrder, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (model.PaymentOrder, error) {
|
||||
func newMohongPayment(row model.MohongOrder, req StartPaymentRequest, runtimeConfig runtimePaymentConfig, tenantID uint64) (model.PaymentOrder, error) {
|
||||
paymentNo, err := newPaymentNo()
|
||||
if err != nil {
|
||||
return model.PaymentOrder{}, err
|
||||
}
|
||||
payment := model.PaymentOrder{
|
||||
TenantID: tenantID,
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: row.ID,
|
||||
OrderNo: row.OrderNo,
|
||||
@@ -205,8 +209,12 @@ func newMohongPayment(row model.MohongOrder, req StartPaymentRequest, runtimeCon
|
||||
// QueryMohong 查询撞车订单支付状态。
|
||||
func (r *Repository) QueryMohong(ctx context.Context, userID uint64, orderID uint64) (*PaymentDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("order_id = ? AND user_id = ? AND biz_type = ?", orderID, userID, bizTypeMohongPay).
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("order_id = ? AND user_id = ? AND biz_type = ?", orderID, userID, bizTypeMohongPay)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
Order("id DESC").
|
||||
First(&payment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -136,11 +137,16 @@ func (r *Repository) preparePayment(ctx context.Context, userID uint64, orderID
|
||||
if row.Status != "pending_payment" {
|
||||
return ErrPaymentCannotStart
|
||||
}
|
||||
requestTenantID := tenancy.IDFromContext(ctx)
|
||||
paymentTenantID := orderPaymentTenantID(row)
|
||||
if requestTenantID > 0 && paymentTenantID > 0 && requestTenantID != paymentTenantID {
|
||||
return ErrPaymentCannotStart
|
||||
}
|
||||
amountCent := row.RentAmountCent + row.DepositAmountCent
|
||||
if amountCent <= 0 {
|
||||
return ErrPaymentCannotStart
|
||||
}
|
||||
runtimeConfig, err := r.resolveOrderRuntimeConfig(ctx, tx, row.DistributionSiteID, req.PayWay)
|
||||
runtimeConfig, err := r.resolveOrderRuntimeConfig(ctx, tx, paymentTenantID, row.DistributionSiteID, req.PayWay)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,6 +160,7 @@ func (r *Repository) preparePayment(ctx context.Context, userID uint64, orderID
|
||||
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, runtimeConfig.PayWay, "ZFBZF")
|
||||
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, runtimeConfig.JSPayFlag, "2")
|
||||
existing.AmountCent = amountCent
|
||||
existing.TenantID = firstNonZero(existing.TenantID, paymentTenantID)
|
||||
existing.PaymentConfigID = firstNonZero(existing.PaymentConfigID, runtimeConfig.ID)
|
||||
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
|
||||
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
|
||||
@@ -240,6 +247,7 @@ func newOrderPayment(row model.RentalOrder, amountCent int64, req StartPaymentRe
|
||||
return model.PaymentOrder{}, err
|
||||
}
|
||||
payment := model.PaymentOrder{
|
||||
TenantID: orderPaymentTenantID(row),
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: row.ID,
|
||||
OrderNo: row.OrderNo,
|
||||
@@ -261,3 +269,10 @@ func newOrderPayment(row model.RentalOrder, amountCent int64, req StartPaymentRe
|
||||
}
|
||||
return payment, nil
|
||||
}
|
||||
|
||||
func orderPaymentTenantID(row model.RentalOrder) uint64 {
|
||||
if row.PaymentTenantID > 0 {
|
||||
return row.PaymentTenantID
|
||||
}
|
||||
return row.BuyerTenantID
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
func toDTO(payment model.PaymentOrder) PaymentDTO {
|
||||
return PaymentDTO{
|
||||
ID: payment.ID,
|
||||
TenantID: payment.TenantID,
|
||||
PaymentNo: payment.PaymentNo,
|
||||
OrderID: payment.OrderID,
|
||||
OrderNo: payment.OrderNo,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -42,10 +43,14 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
err = r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Select("id").
|
||||
Select("id, tenant_id").
|
||||
First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
requestTenantID := tenancy.IDFromContext(ctx)
|
||||
if requestTenantID > 0 && user.TenantID > 0 && requestTenantID != user.TenantID {
|
||||
return ErrPaymentCannotStart
|
||||
}
|
||||
var existing model.PaymentOrder
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID).
|
||||
@@ -56,6 +61,7 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
existing.PayWay = firstNonEmpty(req.PayWay, existing.PayWay, runtimeConfig.PayWay, "ZFBZF")
|
||||
existing.JSPayFlag = firstNonEmpty(req.JSPayFlag, existing.JSPayFlag, runtimeConfig.JSPayFlag, "2")
|
||||
existing.AmountCent = amountCent
|
||||
existing.TenantID = firstNonZero(existing.TenantID, user.TenantID, requestTenantID)
|
||||
existing.PaymentConfigID = firstNonZero(existing.PaymentConfigID, runtimeConfig.ID)
|
||||
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
|
||||
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
|
||||
@@ -71,7 +77,7 @@ func (r *Repository) prepareQualityOwnerPayment(ctx context.Context, userID uint
|
||||
} else if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
payment, err := newQualityOwnerPayment(userID, amountCent, req, *runtimeConfig)
|
||||
payment, err := newQualityOwnerPayment(userID, amountCent, req, *runtimeConfig, firstNonZero(user.TenantID, requestTenantID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -96,13 +102,14 @@ func canReuseQualityOwnerPayment(payment model.PaymentOrder, runtimeConfig runti
|
||||
return canReuseOrderPayment(payment, runtimeConfig)
|
||||
}
|
||||
|
||||
func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (model.PaymentOrder, error) {
|
||||
func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig, tenantID uint64) (model.PaymentOrder, error) {
|
||||
paymentNo, err := newPaymentNo()
|
||||
if err != nil {
|
||||
return model.PaymentOrder{}, err
|
||||
}
|
||||
orderNo := fmt.Sprintf("QO%d", userID)
|
||||
payment := model.PaymentOrder{
|
||||
TenantID: tenantID,
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: 0,
|
||||
OrderNo: orderNo,
|
||||
@@ -127,8 +134,12 @@ func newQualityOwnerPayment(userID uint64, amountCent int64, req StartPaymentReq
|
||||
|
||||
func (r *Repository) QueryQualityOwner(ctx context.Context, userID uint64) (*PaymentDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID).
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND biz_type = ? AND biz_id = ?", userID, qualityOwnerPayBizType, userID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
Order("id DESC").
|
||||
First(&payment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
|
||||
@@ -83,7 +83,7 @@ func TestNewQualityOwnerPaymentUsesBizIDAndMockCashier(t *testing.T) {
|
||||
MerchantID: "mock-merchant",
|
||||
PayWay: "WXZF",
|
||||
JSPayFlag: "2",
|
||||
})
|
||||
}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("newQualityOwnerPayment() error = %v", err)
|
||||
}
|
||||
|
||||
@@ -4,11 +4,16 @@ import (
|
||||
"context"
|
||||
"gorm.io/gorm"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
)
|
||||
|
||||
func (r *Repository) Query(ctx context.Context, userID uint64, orderID uint64) (*PaymentDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).Where("order_id = ? AND user_id = ? AND biz_type = ?", orderID, userID, "order_pay").Order("id DESC").First(&payment).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("order_id = ? AND user_id = ? AND biz_type = ?", orderID, userID, "order_pay")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Order("id DESC").First(&payment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrPaymentNotFound
|
||||
}
|
||||
@@ -48,6 +53,10 @@ func (r *Repository) AdminList(ctx context.Context, query AdminPaymentQuery) (*P
|
||||
Select("p.*, COALESCE(u.phone, '') AS user_phone").
|
||||
Joins("LEFT JOIN users AS u ON u.id = p.user_id")
|
||||
countDB := r.db.WithContext(ctx).Model(&model.PaymentOrder{})
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
db = db.Where("p.tenant_id = ?", tenantID)
|
||||
countDB = countDB.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if query.UserID > 0 {
|
||||
db = db.Where("p.user_id = ?", query.UserID)
|
||||
countDB = countDB.Where("user_id = ?", query.UserID)
|
||||
@@ -104,6 +113,7 @@ func (row adminPaymentRow) toDTO() AdminPaymentDTO {
|
||||
errorCode, errorMessage := paymentErrorSummary(row.Status, row.RawResponse)
|
||||
return AdminPaymentDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
PaymentNo: row.PaymentNo,
|
||||
OrderID: row.OrderID,
|
||||
OrderNo: row.OrderNo,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/distribution"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/datatypes"
|
||||
@@ -163,7 +164,11 @@ func (r *Repository) StartRefund(ctx context.Context, orderID uint64, refundAmou
|
||||
}
|
||||
func (r *Repository) QueryRefundStatus(ctx context.Context, orderID uint64) (*RefundDTO, error) {
|
||||
var payment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).Where("order_id = ? AND biz_type IN ?", orderID, refundBizTypes).Order("id DESC").First(&payment).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("order_id = ? AND biz_type IN ?", orderID, refundBizTypes)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Order("id DESC").First(&payment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrPaymentNotFound
|
||||
}
|
||||
@@ -196,7 +201,11 @@ func (r *Repository) SyncRefundStatusByPaymentID(ctx context.Context, paymentID
|
||||
}
|
||||
func (r *Repository) findOriginalPayment(ctx context.Context, orderID uint64) (model.PaymentOrder, error) {
|
||||
var originalPayment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID).Order("id DESC").First(&originalPayment).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Where("order_id = ? AND status = 'paid' AND biz_type = 'order_pay'", orderID)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Order("id DESC").First(&originalPayment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return originalPayment, ErrPaymentNotFound
|
||||
}
|
||||
@@ -216,7 +225,7 @@ func (r *Repository) prepareRefundOrder(ctx context.Context, originalPayment mod
|
||||
}
|
||||
var existingRefund model.PaymentOrder
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("order_id = ? AND biz_type = ?", originalPayment.OrderID, bizType).
|
||||
Where("order_id = ? AND biz_type = ? AND tenant_id = ?", originalPayment.OrderID, bizType, originalPayment.TenantID).
|
||||
Order("id DESC").
|
||||
First(&existingRefund).Error
|
||||
if err == nil {
|
||||
@@ -233,6 +242,7 @@ func (r *Repository) prepareRefundOrder(ctx context.Context, originalPayment mod
|
||||
}
|
||||
merchantRefundID := "REF" + paymentNo[3:]
|
||||
refundOrder := model.PaymentOrder{
|
||||
TenantID: originalPayment.TenantID,
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: originalPayment.OrderID,
|
||||
OrderNo: originalPayment.OrderNo,
|
||||
@@ -505,6 +515,7 @@ func (r *Repository) markRefundFailed(ctx context.Context, paymentID uint64, ord
|
||||
func toRefundDTO(payment model.PaymentOrder) RefundDTO {
|
||||
return RefundDTO{
|
||||
ID: payment.ID,
|
||||
TenantID: payment.TenantID,
|
||||
PaymentNo: payment.PaymentNo,
|
||||
OrderID: payment.OrderID,
|
||||
OrderNo: payment.OrderNo,
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
@@ -125,8 +126,12 @@ func (r *Repository) StartMohongRefund(ctx context.Context, orderID uint64, refu
|
||||
|
||||
func (r *Repository) findOriginalMohongPayment(ctx context.Context, orderID uint64) (model.PaymentOrder, error) {
|
||||
var originalPayment model.PaymentOrder
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("order_id = ? AND status = 'paid' AND biz_type = ?", orderID, bizTypeMohongPay).
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("order_id = ? AND status = 'paid' AND biz_type = ?", orderID, bizTypeMohongPay)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.
|
||||
Order("id DESC").
|
||||
First(&originalPayment).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
@@ -158,7 +163,7 @@ func (r *Repository) prepareMohongRefundOrder(ctx context.Context, originalPayme
|
||||
|
||||
var existingRefund model.PaymentOrder
|
||||
err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
Where("order_id = ? AND biz_type = ?", originalPayment.OrderID, bizTypeMohongRefund).
|
||||
Where("order_id = ? AND biz_type = ? AND tenant_id = ?", originalPayment.OrderID, bizTypeMohongRefund, originalPayment.TenantID).
|
||||
Order("id DESC").
|
||||
First(&existingRefund).Error
|
||||
if err == nil {
|
||||
@@ -176,6 +181,7 @@ func (r *Repository) prepareMohongRefundOrder(ctx context.Context, originalPayme
|
||||
}
|
||||
merchantRefundID := "REF" + paymentNo[3:]
|
||||
refundOrder := model.PaymentOrder{
|
||||
TenantID: originalPayment.TenantID,
|
||||
PaymentNo: paymentNo,
|
||||
OrderID: originalPayment.OrderID,
|
||||
OrderNo: originalPayment.OrderNo,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/order"
|
||||
"hfb_sys/backend/internal/modules/paymentconfig"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
@@ -176,8 +177,9 @@ func (c runtimePaymentConfig) isMockMode() bool {
|
||||
}
|
||||
|
||||
// resolveOrderRuntimeConfig 根据订单所属分销站点配置或全局默认配置解析运行时支付配置。
|
||||
func (r *Repository) resolveOrderRuntimeConfig(ctx context.Context, tx *gorm.DB, siteID *uint64, payWay string) (*runtimePaymentConfig, error) {
|
||||
if siteID != nil && *siteID > 0 {
|
||||
func (r *Repository) resolveOrderRuntimeConfig(ctx context.Context, tx *gorm.DB, tenantID uint64, siteID *uint64, payWay string) (*runtimePaymentConfig, error) {
|
||||
configCtx := scopedTenantContext(ctx, tenantID)
|
||||
if tenantID == 0 && siteID != nil && *siteID > 0 {
|
||||
var configID uint64
|
||||
queryDB := tx
|
||||
if queryDB == nil {
|
||||
@@ -190,13 +192,13 @@ func (r *Repository) resolveOrderRuntimeConfig(ctx context.Context, tx *gorm.DB,
|
||||
Limit(1).
|
||||
Pluck("pmc.id", &configID).Error
|
||||
if err == nil && configID > 0 && r.configRepo != nil {
|
||||
dto, err := r.configRepo.FindRuntimeByID(ctx, configID, true)
|
||||
dto, err := r.configRepo.FindRuntimeByID(configCtx, configID, true)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return r.defaultRuntimeConfig(ctx, payWay)
|
||||
return r.defaultRuntimeConfig(configCtx, payWay)
|
||||
}
|
||||
|
||||
// defaultRuntimeConfig 按支付方式获取默认启用配置,用于新支付单创建。
|
||||
@@ -213,11 +215,12 @@ func (r *Repository) defaultRuntimeConfig(ctx context.Context, payWay string) (*
|
||||
|
||||
// runtimeConfigForPayment 回溯支付单当时命中的配置,优先使用 payment_config_id。
|
||||
func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model.PaymentOrder) (*runtimePaymentConfig, error) {
|
||||
configCtx := scopedTenantContext(ctx, payment.TenantID)
|
||||
provider := firstNonEmpty(payment.Provider, "mock")
|
||||
merchantID := payment.MerchantID
|
||||
payWay := normalizePayWay(payment.PayWay)
|
||||
if r.configRepo != nil && payment.PaymentConfigID > 0 {
|
||||
dto, err := r.configRepo.FindRuntimeByID(ctx, payment.PaymentConfigID, true)
|
||||
dto, err := r.configRepo.FindRuntimeByID(configCtx, payment.PaymentConfigID, true)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
@@ -226,14 +229,14 @@ func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model
|
||||
}
|
||||
}
|
||||
if r.configRepo != nil && merchantID != "" {
|
||||
dto, err := r.configRepo.FindByProviderMerchantPayWay(ctx, provider, merchantID, payWay, true)
|
||||
dto, err := r.configRepo.FindByProviderMerchantPayWay(configCtx, provider, merchantID, payWay, true)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
if err != paymentconfig.ErrConfigNotFound {
|
||||
return nil, err
|
||||
}
|
||||
dto, err = r.configRepo.FindByProviderMerchant(ctx, provider, merchantID, true)
|
||||
dto, err = r.configRepo.FindByProviderMerchant(configCtx, provider, merchantID, true)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
@@ -253,10 +256,13 @@ func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model
|
||||
MerchantID: merchantID,
|
||||
}, nil
|
||||
}
|
||||
if payment.TenantID > 0 {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
if r.configRepo == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
dto, err := r.configRepo.FindDefaultByProviderPayWay(ctx, provider, payWay, true)
|
||||
dto, err := r.configRepo.FindDefaultByProviderPayWay(configCtx, provider, payWay, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -292,7 +298,7 @@ func (r *Repository) recordConfigUsage(ctx context.Context, runtimeConfig *runti
|
||||
if (payment.BizType != "order_pay" && payment.BizType != "mohong_pay") || payment.Status != "paid" {
|
||||
return
|
||||
}
|
||||
if err := r.configRepo.RecordUsage(ctx, runtimeConfig.ID, payment.ID, runtimeConfig.Provider, runtimeConfig.MerchantID, payment.AmountCent, payment.BizType); err != nil {
|
||||
if err := r.configRepo.RecordUsage(ctx, payment.TenantID, runtimeConfig.ID, payment.ID, runtimeConfig.Provider, runtimeConfig.MerchantID, payment.AmountCent, payment.BizType); err != nil {
|
||||
r.log().Warn("支付配置使用记录保存失败", paymentLogFields(ctx,
|
||||
zap.Uint64("payment_config_id", runtimeConfig.ID),
|
||||
zap.Uint64("payment_id", payment.ID),
|
||||
@@ -300,3 +306,10 @@ func (r *Repository) recordConfigUsage(ctx context.Context, runtimeConfig *runti
|
||||
)...)
|
||||
}
|
||||
}
|
||||
|
||||
func scopedTenantContext(ctx context.Context, tenantID uint64) context.Context {
|
||||
if tenantID == 0 || tenancy.IDFromContext(ctx) == tenantID {
|
||||
return ctx
|
||||
}
|
||||
return tenancy.WithScope(ctx, tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/paymentconfig"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func paymentTenantContext(tenantID uint64) context.Context {
|
||||
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
||||
}
|
||||
|
||||
func createPaymentTenantOrder(t *testing.T, db *gorm.DB, tenantID uint64) model.RentalOrder {
|
||||
t.Helper()
|
||||
order := model.RentalOrder{
|
||||
BuyerTenantID: tenantID, SourceTenantID: 101, PaymentTenantID: tenantID,
|
||||
OrderNo: "RO-PAY-TENANT-" + string(rune('0'+tenantID%10)),
|
||||
RenterID: 1, RentAmountCent: 1000, DepositAmountCent: 500,
|
||||
Status: "pending_payment",
|
||||
}
|
||||
if err := db.Create(&order).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return order
|
||||
}
|
||||
|
||||
func TestOrderPaymentUsesBuyerTenantMerchant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
dachuiConfig := model.PaymentMerchantConfig{
|
||||
OwnerTenantID: 101, Name: "大锤支付宝", Provider: "mock", MerchantID: "DACHUI-MOCK",
|
||||
PayWay: "ZFBZF", Status: "active", IsDefault: true, Environment: "production",
|
||||
}
|
||||
jieyouConfig := model.PaymentMerchantConfig{
|
||||
OwnerTenantID: 202, Name: "解忧支付宝", Provider: "mock", MerchantID: "JIEYOU-MOCK",
|
||||
PayWay: "ZFBZF", Status: "active", IsDefault: true, Environment: "production",
|
||||
}
|
||||
if err := db.Create(&dachuiConfig).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&jieyouConfig).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configRepo := paymentconfig.NewRepository(db, &paymentconfig.MockEncryptor{})
|
||||
repo := NewRepository(db, configRepo, nil)
|
||||
jieyouOrder := createPaymentTenantOrder(t, db, 202)
|
||||
|
||||
payment, _, err := repo.preparePayment(paymentTenantContext(202), jieyouOrder.RenterID, jieyouOrder.ID, StartPaymentRequest{PayWay: "ZFBZF"})
|
||||
if err != nil {
|
||||
t.Fatalf("prepare tenant payment: %v", err)
|
||||
}
|
||||
if payment.TenantID != 202 || payment.PaymentConfigID != jieyouConfig.ID || payment.MerchantID != "JIEYOU-MOCK" {
|
||||
t.Fatalf("tenant payment = %+v", payment)
|
||||
}
|
||||
if _, _, err := repo.preparePayment(paymentTenantContext(101), jieyouOrder.RenterID, jieyouOrder.ID, StartPaymentRequest{PayWay: "ZFBZF"}); !errors.Is(err, ErrPaymentCannotStart) {
|
||||
t.Fatalf("cross-tenant payment error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantPaymentDoesNotFallbackToAnotherTenantMerchant(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
configRepo := paymentconfig.NewRepository(db, &paymentconfig.MockEncryptor{})
|
||||
dachuiConfig := model.PaymentMerchantConfig{
|
||||
OwnerTenantID: 101, Name: "大锤商户", Provider: "leshua", MerchantID: "SHARED-MERCHANT",
|
||||
PayWay: "ZFBZF", Status: "active", IsDefault: true, Environment: "production",
|
||||
}
|
||||
if err := db.Create(&dachuiConfig).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db, configRepo, nil)
|
||||
payment := model.PaymentOrder{
|
||||
TenantID: 202, PaymentNo: "PAY-NO-FALLBACK", OrderID: 1, OrderNo: "RO-NO-FALLBACK",
|
||||
UserID: 1, PaymentConfigID: dachuiConfig.ID, Provider: "leshua",
|
||||
MerchantID: "SHARED-MERCHANT", ThirdOrderID: "PAY-NO-FALLBACK",
|
||||
PayWay: "ZFBZF", AmountCent: 1000, BizType: "order_pay", Status: "paid",
|
||||
}
|
||||
if _, err := repo.runtimeConfigForPayment(context.Background(), &payment); !errors.Is(err, ErrPaymentUnavailable) {
|
||||
t.Fatalf("cross-tenant merchant fallback error = %v", err)
|
||||
}
|
||||
|
||||
jieyouConfig := model.PaymentMerchantConfig{
|
||||
OwnerTenantID: 202, Name: "解忧商户", Provider: "leshua", MerchantID: "JIEYOU-MERCHANT",
|
||||
PayWay: "ZFBZF", Status: "active", IsDefault: true, Environment: "production",
|
||||
}
|
||||
if err := db.Create(&jieyouConfig).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payment.PaymentConfigID = jieyouConfig.ID
|
||||
payment.MerchantID = "JIEYOU-MERCHANT"
|
||||
runtimeConfig, err := repo.runtimeConfigForPayment(context.Background(), &payment)
|
||||
if err != nil {
|
||||
t.Fatalf("load original tenant merchant: %v", err)
|
||||
}
|
||||
if runtimeConfig.ID != jieyouConfig.ID || runtimeConfig.MerchantID != "JIEYOU-MERCHANT" {
|
||||
t.Fatalf("original merchant runtime = %+v", runtimeConfig)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantOrderWithoutOwnConfigDoesNotUseOtherTenantDefault(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
config := model.PaymentMerchantConfig{
|
||||
OwnerTenantID: 101, Name: "大锤默认", Provider: "mock", MerchantID: "DACHUI-ONLY",
|
||||
PayWay: "ZFBZF", Status: "active", IsDefault: true, Environment: "production",
|
||||
}
|
||||
if err := db.Create(&config).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
order := createPaymentTenantOrder(t, db, 202)
|
||||
repo := NewRepository(db, paymentconfig.NewRepository(db, &paymentconfig.MockEncryptor{}), nil)
|
||||
if _, _, err := repo.preparePayment(paymentTenantContext(202), order.RenterID, order.ID, StartPaymentRequest{PayWay: "ZFBZF"}); !errors.Is(err, paymentconfig.ErrNoActiveConfigFound) {
|
||||
t.Fatalf("missing tenant config error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user