支持微信支付宝独立支付渠道
This commit is contained in:
@@ -16,6 +16,7 @@ type PaymentDTO struct {
|
||||
PaymentNo string `json:"payment_no"`
|
||||
OrderID uint64 `json:"order_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
PaymentConfigID uint64 `json:"payment_config_id"`
|
||||
Provider string `json:"provider"`
|
||||
ThirdOrderID string `json:"third_order_id"`
|
||||
ProviderOrderID string `json:"provider_order_id"`
|
||||
@@ -75,6 +76,7 @@ type AdminPaymentDTO struct {
|
||||
OrderID uint64 `json:"order_id"`
|
||||
OrderNo string `json:"order_no"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
PaymentConfigID uint64 `json:"payment_config_id"`
|
||||
UserPhone string `json:"user_phone"`
|
||||
Provider string `json:"provider"`
|
||||
MerchantID string `json:"merchant_id"`
|
||||
|
||||
@@ -11,8 +11,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Start 发起订单支付,并按请求支付方式选择对应默认渠道配置。
|
||||
func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, req StartPaymentRequest, clientIP string) (*PaymentDTO, error) {
|
||||
defaultConfig, err := r.defaultRuntimeConfig(ctx)
|
||||
req.PayWay = normalizePayWay(req.PayWay)
|
||||
defaultConfig, err := r.defaultRuntimeConfig(ctx, req.PayWay)
|
||||
if err != nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
@@ -103,6 +105,8 @@ func (r *Repository) Start(ctx context.Context, userID uint64, orderID uint64, r
|
||||
dto := toDTO(*latest)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// preparePayment 创建或复用订单支付单,并写入本次命中的支付配置 ID。
|
||||
func (r *Repository) preparePayment(ctx context.Context, userID uint64, orderID uint64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (*model.PaymentOrder, *model.RentalOrder, error) {
|
||||
var paymentID uint64
|
||||
var orderRow model.RentalOrder
|
||||
@@ -130,6 +134,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.PaymentConfigID = firstNonZero(existing.PaymentConfigID, runtimeConfig.ID)
|
||||
existing.Provider = firstNonEmpty(existing.Provider, runtimeConfig.Provider)
|
||||
existing.MerchantID = firstNonEmpty(existing.MerchantID, runtimeConfig.MerchantID)
|
||||
if existing.Provider == "mock" && existing.ProviderOrderID == "" {
|
||||
@@ -165,6 +170,8 @@ func (r *Repository) preparePayment(ctx context.Context, userID uint64, orderID
|
||||
}
|
||||
return payment, &orderRow, nil
|
||||
}
|
||||
|
||||
// canReuseOrderPayment 判断旧支付单是否还能复用,支付方式或配置不同则必须重新下单。
|
||||
func canReuseOrderPayment(payment model.PaymentOrder, runtimeConfig runtimePaymentConfig) bool {
|
||||
if payment.Status == "paid" {
|
||||
return true
|
||||
@@ -178,11 +185,19 @@ func canReuseOrderPayment(payment model.PaymentOrder, runtimeConfig runtimePayme
|
||||
if payment.MerchantID != "" && runtimeConfig.MerchantID != "" && payment.MerchantID != runtimeConfig.MerchantID {
|
||||
return false
|
||||
}
|
||||
if payment.PaymentConfigID != 0 && runtimeConfig.ID != 0 && payment.PaymentConfigID != runtimeConfig.ID {
|
||||
return false
|
||||
}
|
||||
if payment.PayWay != "" && runtimeConfig.PayWay != "" && normalizePayWay(payment.PayWay) != normalizePayWay(runtimeConfig.PayWay) {
|
||||
return false
|
||||
}
|
||||
if payment.Status == "paying" && paymentCashierExpired(payment) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// paymentCashierExpired 判断渠道收银台是否已过期,过期后不再复用支付单。
|
||||
func paymentCashierExpired(payment model.PaymentOrder) bool {
|
||||
if len(payment.RawRequest) == 0 {
|
||||
return false
|
||||
@@ -197,6 +212,8 @@ func paymentCashierExpired(payment model.PaymentOrder) bool {
|
||||
}
|
||||
return !timeutil.ShanghaiNow().Before(*deadline)
|
||||
}
|
||||
|
||||
// newOrderPayment 创建新的订单支付单,并固化支付方式和支付配置 ID。
|
||||
func newOrderPayment(row model.RentalOrder, amountCent int64, req StartPaymentRequest, runtimeConfig runtimePaymentConfig) (model.PaymentOrder, error) {
|
||||
paymentNo, err := newPaymentNo()
|
||||
if err != nil {
|
||||
@@ -207,6 +224,7 @@ func newOrderPayment(row model.RentalOrder, amountCent int64, req StartPaymentRe
|
||||
OrderID: row.ID,
|
||||
OrderNo: row.OrderNo,
|
||||
UserID: row.RenterID,
|
||||
PaymentConfigID: runtimeConfig.ID,
|
||||
Provider: runtimeConfig.Provider,
|
||||
MerchantID: runtimeConfig.MerchantID,
|
||||
ThirdOrderID: paymentNo,
|
||||
@@ -223,4 +241,3 @@ func newOrderPayment(row model.RentalOrder, amountCent int64, req StartPaymentRe
|
||||
}
|
||||
return payment, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,6 +19,7 @@ func toDTO(payment model.PaymentOrder) PaymentDTO {
|
||||
PaymentNo: payment.PaymentNo,
|
||||
OrderID: payment.OrderID,
|
||||
OrderNo: payment.OrderNo,
|
||||
PaymentConfigID: payment.PaymentConfigID,
|
||||
Provider: payment.Provider,
|
||||
ThirdOrderID: payment.ThirdOrderID,
|
||||
ProviderOrderID: payment.ProviderOrderID,
|
||||
@@ -115,3 +117,25 @@ func firstNonEmpty(values ...string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// firstNonZero 返回第一个非零 uint64,用于保留历史支付单已有配置 ID。
|
||||
func firstNonZero(values ...uint64) uint64 {
|
||||
for _, value := range values {
|
||||
if value != 0 {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// normalizePayWay 将前端或渠道别名统一为系统内部支付方式编码。
|
||||
func normalizePayWay(value string) string {
|
||||
switch strings.ToUpper(strings.TrimSpace(value)) {
|
||||
case "", "ZFBZF", "ALIPAY", "ALIPAYPAY":
|
||||
return "ZFBZF"
|
||||
case "WXZF", "WECHAT", "WECHATPAY":
|
||||
return "WXZF"
|
||||
default:
|
||||
return strings.ToUpper(strings.TrimSpace(value))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +104,7 @@ func (row adminPaymentRow) toDTO() AdminPaymentDTO {
|
||||
OrderID: row.OrderID,
|
||||
OrderNo: row.OrderNo,
|
||||
UserID: row.UserID,
|
||||
PaymentConfigID: row.PaymentConfigID,
|
||||
UserPhone: row.UserPhone,
|
||||
Provider: row.Provider,
|
||||
MerchantID: row.MerchantID,
|
||||
|
||||
@@ -160,6 +160,8 @@ func (r *Repository) findOriginalPayment(ctx context.Context, orderID uint64) (m
|
||||
}
|
||||
return originalPayment, nil
|
||||
}
|
||||
|
||||
// prepareRefundOrder 创建退款支付单,并继承原支付单回溯出的支付配置。
|
||||
func (r *Repository) prepareRefundOrder(ctx context.Context, originalPayment model.PaymentOrder, runtimeConfig runtimePaymentConfig, refundAmountCent int64, bizType string) (*model.PaymentOrder, bool, error) {
|
||||
var paymentID uint64
|
||||
existing := false
|
||||
@@ -191,6 +193,7 @@ func (r *Repository) prepareRefundOrder(ctx context.Context, originalPayment mod
|
||||
OrderID: originalPayment.OrderID,
|
||||
OrderNo: originalPayment.OrderNo,
|
||||
UserID: originalPayment.UserID,
|
||||
PaymentConfigID: runtimeConfig.ID,
|
||||
Provider: runtimeConfig.Provider,
|
||||
MerchantID: runtimeConfig.MerchantID,
|
||||
ThirdOrderID: merchantRefundID,
|
||||
|
||||
@@ -50,6 +50,7 @@ func RefundBizTypes() []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// NewRepository 创建支付仓库,注入支付配置仓库和订单仓库。
|
||||
func NewRepository(db *gorm.DB, configRepo *paymentconfig.Repository, orderRepo *order.Repository) *Repository {
|
||||
return &Repository{
|
||||
db: db,
|
||||
@@ -57,24 +58,47 @@ func NewRepository(db *gorm.DB, configRepo *paymentconfig.Repository, orderRepo
|
||||
orderRepo: orderRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// isMockMode 判断当前运行时配置是否为模拟支付。
|
||||
func (c runtimePaymentConfig) isMockMode() bool {
|
||||
return c.Provider == "mock"
|
||||
}
|
||||
func (r *Repository) defaultRuntimeConfig(ctx context.Context) (*runtimePaymentConfig, error) {
|
||||
|
||||
// defaultRuntimeConfig 按支付方式获取默认启用配置,用于新支付单创建。
|
||||
func (r *Repository) defaultRuntimeConfig(ctx context.Context, payWay string) (*runtimePaymentConfig, error) {
|
||||
if r.configRepo == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
dto, err := r.configRepo.FindDefaultAny(ctx, true)
|
||||
dto, err := r.configRepo.FindDefaultByPayWay(ctx, payWay, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
|
||||
// runtimeConfigForPayment 回溯支付单当时命中的配置,优先使用 payment_config_id。
|
||||
func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model.PaymentOrder) (*runtimePaymentConfig, error) {
|
||||
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)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
if err != paymentconfig.ErrConfigNotFound {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if r.configRepo != nil && merchantID != "" {
|
||||
dto, err := r.configRepo.FindByProviderMerchant(ctx, provider, merchantID, true)
|
||||
dto, err := r.configRepo.FindByProviderMerchantPayWay(ctx, 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)
|
||||
if err == nil {
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
@@ -84,7 +108,7 @@ func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model
|
||||
}
|
||||
if provider != "leshua" {
|
||||
if provider == "lakala" && r.configRepo != nil {
|
||||
dto, err := r.configRepo.FindDefaultByProvider(ctx, provider, true)
|
||||
dto, err := r.configRepo.FindDefaultByProviderPayWay(ctx, provider, payWay, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -98,15 +122,17 @@ func (r *Repository) runtimeConfigForPayment(ctx context.Context, payment *model
|
||||
if r.configRepo == nil {
|
||||
return nil, ErrPaymentUnavailable
|
||||
}
|
||||
dto, err := r.configRepo.FindDefaultByProvider(ctx, provider, true)
|
||||
dto, err := r.configRepo.FindDefaultByProviderPayWay(ctx, provider, payWay, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return runtimeConfigFromDTO(dto), nil
|
||||
}
|
||||
|
||||
// runtimeConfigFromDTO 将后台支付配置 DTO 转为支付运行时配置。
|
||||
func runtimeConfigFromDTO(dto *paymentconfig.ConfigDTO) *runtimePaymentConfig {
|
||||
provider := firstNonEmpty(dto.Provider, "mock")
|
||||
payWay := firstNonEmpty(dto.PayWay, "ZFBZF")
|
||||
payWay := normalizePayWay(dto.PayWay)
|
||||
jsPayFlag := firstNonEmpty(dto.JSPayFlag, "2")
|
||||
client, err := buildChannelClient(dto)
|
||||
if err != nil {
|
||||
@@ -123,6 +149,8 @@ func runtimeConfigFromDTO(dto *paymentconfig.ConfigDTO) *runtimePaymentConfig {
|
||||
Channel: client,
|
||||
}
|
||||
}
|
||||
|
||||
// recordConfigUsage 记录支付配置命中情况,失败只写日志不影响主流程。
|
||||
func (r *Repository) recordConfigUsage(ctx context.Context, runtimeConfig *runtimePaymentConfig, payment *model.PaymentOrder) {
|
||||
if r.configRepo == nil || runtimeConfig == nil || payment == nil || runtimeConfig.ID == 0 {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user