feat(main): add mp-weixin
This commit is contained in:
@@ -16,6 +16,8 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"net/http"
|
||||
"qnc-server/app/user/cmd/api/internal/config"
|
||||
"qnc-server/app/user/model"
|
||||
"qnc-server/common/ctxdata"
|
||||
"qnc-server/pkg/lzkit/lzUtils"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -35,10 +37,11 @@ type WechatPayService struct {
|
||||
config config.WxpayConfig
|
||||
wechatClient *core.Client
|
||||
notifyHandler *notify.Handler
|
||||
userAuthModel model.UserAuthModel
|
||||
}
|
||||
|
||||
// NewWechatPayService 初始化微信支付服务
|
||||
func NewWechatPayService(c config.Config) *WechatPayService {
|
||||
func NewWechatPayService(c config.Config, userAuthModel model.UserAuthModel) *WechatPayService {
|
||||
// 从配置中加载商户信息
|
||||
mchID := c.Wxpay.MchID
|
||||
mchCertificateSerialNumber := c.Wxpay.MchCertificateSerialNumber
|
||||
@@ -71,6 +74,7 @@ func NewWechatPayService(c config.Config) *WechatPayService {
|
||||
config: c.Wxpay,
|
||||
wechatClient: client,
|
||||
notifyHandler: notifyHandler,
|
||||
userAuthModel: userAuthModel,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,7 +108,7 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
|
||||
}
|
||||
|
||||
// CreateWechatMiniProgramOrder 创建微信小程序支付订单
|
||||
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (string, error) {
|
||||
func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (interface{}, error) {
|
||||
totalAmount := lzUtils.ToWechatAmount(amount)
|
||||
|
||||
// 构建支付请求参数
|
||||
@@ -131,24 +135,32 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
|
||||
}
|
||||
|
||||
// 返回预支付交易会话标识
|
||||
return *resp.PrepayId, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// CreateWechatOrder 创建微信支付订单(集成 APP、H5、小程序)
|
||||
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (string, error) {
|
||||
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
|
||||
// 根据 ctx 中的 platform 判断平台
|
||||
platform := ctx.Value("platform").(string)
|
||||
|
||||
var prepayId string
|
||||
var prepayData interface{}
|
||||
var err error
|
||||
|
||||
switch platform {
|
||||
case "mp-weixin":
|
||||
userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
|
||||
if getUidErr != nil {
|
||||
return "", fmt.Errorf("获取用户信息失败: %s", getUidErr)
|
||||
}
|
||||
userAuthModel, findUserAuthErr := w.userAuthModel.FindOneByUserIdAuthType(ctx, userID, "wx_mini")
|
||||
if findUserAuthErr != nil {
|
||||
return "", fmt.Errorf("获取用户认证信息失败: %s", findUserAuthErr)
|
||||
}
|
||||
// 如果是小程序平台,调用小程序支付订单创建
|
||||
prepayId, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, "asdasd")
|
||||
prepayData, err = w.CreateWechatMiniProgramOrder(ctx, amount, description, outTradeNo, userAuthModel.AuthKey)
|
||||
case "app":
|
||||
// 如果是 APP 平台,调用 APP 支付订单创建
|
||||
prepayId, err = w.CreateWechatAppOrder(ctx, amount, description, outTradeNo)
|
||||
prepayData, err = w.CreateWechatAppOrder(ctx, amount, description, outTradeNo)
|
||||
default:
|
||||
return "", fmt.Errorf("不支持的支付平台: %s", platform)
|
||||
}
|
||||
@@ -159,7 +171,7 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
|
||||
}
|
||||
|
||||
// 返回预支付ID
|
||||
return prepayId, nil
|
||||
return prepayData, nil
|
||||
}
|
||||
|
||||
// HandleWechatPayNotification 处理微信支付回调
|
||||
|
||||
Reference in New Issue
Block a user