From 384ccd24f83dacbc67b0418cf4643d87dbf07a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Mon, 13 Jul 2026 17:13:03 +0800 Subject: [PATCH] 1 --- short_drama.db | Bin 126976 -> 126976 bytes shortdrama/controller/config_controller.go | 43 +++++++++++--- shortdrama/dao/payment_config_dao.go | 11 +++- shortdrama/model/dto/config_dto.go | 29 ++++++---- shortdrama/model/entity/payment_config.go | 21 +++---- shortdrama/service/payment_service.go | 64 ++++++++++++--------- 6 files changed, 109 insertions(+), 59 deletions(-) diff --git a/short_drama.db b/short_drama.db index f8015ee09c4357a63f1cb2f9ff132c0732c98a11..4054a3207dc5f2f7802e5ad0ca5cb43e7ee83d0a 100644 GIT binary patch delta 605 zcmZp8z~1nHeS);$F$M+(c_4;?^ocsgtj8Gi(q3&$*&xrvpSM|2A%b6o{|SQ#V?knN zZfaghd~$wXT4p+XbCdq|CVj?c18Ii047~HWEjd?lT5(+C$YkqflV|2(eZtzq@OHAG zz*(*q85wqQO-;u3;^_*BjI()?GZORiQgh-EH5(6uB+LNe#GFi^FR&=aB9vHAU{%kGq+5^_-5I8~EJ%9ANzt2CoLG=)%?#9D o4|jQaDlpVaus9Qo5Xj|BNR|tdq8aFLMz~%RV`FhrG=p6Z0NbdG)Y$xk-O}lRjg!fi$BS z1MfU;OU_lCRvgzjGTD0B 0 && typeOpt[0] != "" { + m = m.Where("channel_type", typeOpt[0]) + } + r, err := m.Limit(1).One() if err != nil { return nil, err } @@ -48,7 +53,7 @@ func (d *paymentConfigDao) GetByChannel(ctx context.Context, channel string) (re } func (d *paymentConfigDao) Save(ctx context.Context, data *entity.PaymentConfig) error { - existing, err := d.GetByChannel(ctx, data.Channel) + existing, err := d.GetByChannel(ctx, data.Channel, data.ChannelType) if err != nil { return err } diff --git a/shortdrama/model/dto/config_dto.go b/shortdrama/model/dto/config_dto.go index 11a5d56..1d7259f 100644 --- a/shortdrama/model/dto/config_dto.go +++ b/shortdrama/model/dto/config_dto.go @@ -42,19 +42,26 @@ type GetPaymentConfigReq struct { g.Meta `path:"/payment" method:"get" tags:"支付配置" summary:"获取支付配置"` } +type PaymentTypeConfig struct { + Jsapi *entity.PaymentConfig `json:"jsapi,omitempty"` + H5 *entity.PaymentConfig `json:"h5,omitempty"` + App *entity.PaymentConfig `json:"app,omitempty"` +} + type GetPaymentConfigRes struct { - Wechat *entity.PaymentConfig `json:"wechat"` - Alipay *entity.PaymentConfig `json:"alipay"` - Offline *entity.PaymentConfig `json:"offline"` + Wechat *PaymentTypeConfig `json:"wechat,omitempty"` + Alipay *PaymentTypeConfig `json:"alipay,omitempty"` + Offline *entity.PaymentConfig `json:"offline,omitempty"` } type SavePaymentConfigReq struct { - g.Meta `path:"/payment" method:"post" tags:"支付配置" summary:"保存支付配置"` - Channel string `json:"channel" v:"required" dc:"支付渠道: wechat/alipay/offline"` - AppId string `json:"appId"` - MchId string `json:"mchId"` - ApiKey string `json:"apiKey"` - AppSecret string `json:"appSecret"` - PrivateKey string `json:"privateKey"` - PublicKey string `json:"publicKey"` + g.Meta `path:"/payment" method:"post" tags:"支付配置" summary:"保存支付配置"` + Channel string `json:"channel" v:"required" dc:"支付渠道: wechat/alipay/offline"` + ChannelType string `json:"channelType" dc:"支付类型: jsapi/h5/app/native/manual"` + AppId string `json:"appId"` + MchId string `json:"mchId"` + ApiKey string `json:"apiKey"` + AppSecret string `json:"appSecret"` + PrivateKey string `json:"privateKey"` + PublicKey string `json:"publicKey"` } diff --git a/shortdrama/model/entity/payment_config.go b/shortdrama/model/entity/payment_config.go index ded8070..9f21f5f 100644 --- a/shortdrama/model/entity/payment_config.go +++ b/shortdrama/model/entity/payment_config.go @@ -5,14 +5,15 @@ import ( ) type PaymentConfig struct { - Id int64 `orm:"id" json:"id"` - Channel string `orm:"channel" json:"channel"` - AppId string `orm:"app_id" json:"appId"` - MchId string `orm:"mch_id" json:"mchId"` - ApiKey string `orm:"api_key" json:"apiKey"` - AppSecret string `orm:"app_secret" json:"appSecret"` - PrivateKey string `orm:"private_key" json:"privateKey"` - PublicKey string `orm:"public_key" json:"publicKey"` - CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` - UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` + Id int64 `orm:"id" json:"id"` + Channel string `orm:"channel" json:"channel"` + ChannelType string `orm:"channel_type" json:"channelType"` + AppId string `orm:"app_id" json:"appId"` + MchId string `orm:"mch_id" json:"mchId"` + ApiKey string `orm:"api_key" json:"apiKey"` + AppSecret string `orm:"app_secret" json:"appSecret"` + PrivateKey string `orm:"private_key" json:"privateKey"` + PublicKey string `orm:"public_key" json:"publicKey"` + CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` + UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` } diff --git a/shortdrama/service/payment_service.go b/shortdrama/service/payment_service.go index e2234ac..94b928a 100644 --- a/shortdrama/service/payment_service.go +++ b/shortdrama/service/payment_service.go @@ -41,25 +41,38 @@ type AlipayConfig struct { PublicKey string `json:"public_key"` } -func (s *paymentService) GetPaymentConfig() PaymentConfig { +func (s *paymentService) GetPaymentConfig(channel, channelType string) (WechatConfig, AlipayConfig, error) { ctx := context.Background() - pc := PaymentConfig{} - if wc, _ := dao.PaymentConfigDao.GetByChannel(ctx, "wechat"); wc != nil { - pc.Wechat = WechatConfig{ + switch channel { + case "wechat": + wc, err := dao.PaymentConfigDao.GetByChannel(ctx, "wechat", channelType) + if err != nil { + return WechatConfig{}, AlipayConfig{}, err + } + if wc == nil || wc.AppId == "" || wc.MchId == "" || wc.ApiKey == "" { + return WechatConfig{}, AlipayConfig{}, errors.New("微信支付(" + channelType + ")未配置") + } + return WechatConfig{ AppId: wc.AppId, MchId: wc.MchId, ApiKey: wc.ApiKey, AppSecret: wc.AppSecret, + }, AlipayConfig{}, nil + case "alipay": + ac, err := dao.PaymentConfigDao.GetByChannel(ctx, "alipay", channelType) + if err != nil { + return WechatConfig{}, AlipayConfig{}, err } - } - if ac, _ := dao.PaymentConfigDao.GetByChannel(ctx, "alipay"); ac != nil { - pc.Alipay = AlipayConfig{ + if ac == nil || ac.AppId == "" || ac.PrivateKey == "" { + return WechatConfig{}, AlipayConfig{}, errors.New("支付宝支付(" + channelType + ")未配置") + } + return WechatConfig{}, AlipayConfig{ AppId: ac.AppId, PrivateKey: ac.PrivateKey, PublicKey: ac.PublicKey, - } + }, nil } - return pc + return WechatConfig{}, AlipayConfig{}, errors.New("不支持的支付渠道") } // Prepay 创建支付订单并调起渠道 @@ -98,16 +111,8 @@ func (s *paymentService) Prepay(ctx context.Context, userId int64, amount int64, // 外部渠道需要检查支付配置 if channel != "offline" { - pc := s.GetPaymentConfig() - switch channel { - case "wechat": - if pc.Wechat.AppId == "" || pc.Wechat.MchId == "" || pc.Wechat.ApiKey == "" { - return nil, "", "", "", errors.New("微信支付未配置,请先联系管理员配置支付参数") - } - case "alipay": - if pc.Alipay.AppId == "" || pc.Alipay.PrivateKey == "" { - return nil, "", "", "", errors.New("支付宝支付未配置,请先联系管理员配置支付参数") - } + if _, _, err := s.GetPaymentConfig(channel, chanType); err != nil { + return nil, "", "", "", err } } @@ -141,9 +146,11 @@ func (s *paymentService) Prepay(ctx context.Context, userId int64, amount int64, switch channel { case "wechat": - codeUrl, prepayJson, err = s.callWechat(ctx, order, s.GetPaymentConfig().Wechat) + wechatCfg, _, _ := s.GetPaymentConfig("wechat", chanType) + codeUrl, prepayJson, err = s.callWechat(ctx, order, wechatCfg) case "alipay": - codeUrl, redirectUrl, err = s.callAlipay(ctx, order, s.GetPaymentConfig().Alipay) + _, alipayCfg, _ := s.GetPaymentConfig("alipay", chanType) + codeUrl, redirectUrl, err = s.callAlipay(ctx, order, alipayCfg) case "offline": // 线下支付无需调起外部渠道,直接返回 } @@ -223,19 +230,24 @@ func (s *paymentService) ConfirmOffline(ctx context.Context, orderNo string) err // HandleNotify 处理渠道回调 func (s *paymentService) HandleNotify(ctx context.Context, channel string, body []byte) error { - pc := s.GetPaymentConfig() var orderNo, tradeNo string switch channel { case "wechat": - var err error - orderNo, tradeNo, err = s.verifyWechatNotify(body, pc.Wechat) + wechatCfg, _, err := s.GetPaymentConfig("wechat", "native") + if err != nil { + return err + } + orderNo, tradeNo, err = s.verifyWechatNotify(body, wechatCfg) if err != nil { return err } case "alipay": - var err error - orderNo, tradeNo, err = s.verifyAlipayNotify(body, pc.Alipay) + _, alipayCfg, err := s.GetPaymentConfig("alipay", "native") + if err != nil { + return err + } + orderNo, tradeNo, err = s.verifyAlipayNotify(body, alipayCfg) if err != nil { return err }