支持微信支付宝独立支付渠道
This commit is contained in:
@@ -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