f
This commit is contained in:
@@ -2,10 +2,16 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"bdrp-server/app/main/api/internal/config"
|
||||
"bdrp-server/app/main/model"
|
||||
"bdrp-server/common/ctxdata"
|
||||
@@ -151,11 +157,23 @@ func newWechatPayServiceWithWxPayPubKey(c config.Config, userAuthModel model.Use
|
||||
}
|
||||
}
|
||||
|
||||
// CreateWechatAppOrder 创建微信APP支付订单
|
||||
func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount float64, description string, outTradeNo string) (string, error) {
|
||||
// randomNonceWechat 生成微信调起支付用的随机串
|
||||
func randomNonceWechat(n int) (string, error) {
|
||||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
b := make([]byte, n)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for i := range b {
|
||||
b[i] = letters[int(b[i])%len(letters)]
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// CreateWechatAppOrder 创建微信 APP 支付:统一下单后按 V3「APP 调起支付」规则签名,供原生 SDK / uni.requestPayment(wxpay) 使用
|
||||
func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount float64, description string, outTradeNo string) (map[string]string, error) {
|
||||
totalAmount := lzUtils.ToWechatAmount(amount)
|
||||
|
||||
// 构建支付请求参数
|
||||
payRequest := app.PrepayRequest{
|
||||
Appid: core.String(w.config.Wxpay.AppID),
|
||||
Mchid: core.String(w.config.Wxpay.MchID),
|
||||
@@ -167,17 +185,41 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
|
||||
},
|
||||
}
|
||||
|
||||
// 初始化 AppApiService
|
||||
svc := app.AppApiService{Client: w.wechatClient}
|
||||
|
||||
// 发起预支付请求
|
||||
resp, result, err := svc.Prepay(ctx, payRequest)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
return nil, fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
}
|
||||
|
||||
// 返回预支付交易会话标识
|
||||
return *resp.PrepayId, nil
|
||||
prepayID := *resp.PrepayId
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(w.config.Wxpay.MchPrivateKeyPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("加载商户私钥失败: %w", err)
|
||||
}
|
||||
|
||||
ts := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
nonceStr, err := randomNonceWechat(32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appID := w.config.Wxpay.AppID
|
||||
msg := fmt.Sprintf("%s\n%s\n%s\n%s\n", appID, ts, nonceStr, prepayID)
|
||||
sum := sha256.Sum256([]byte(msg))
|
||||
sig, err := rsa.SignPKCS1v15(rand.Reader, mchPrivateKey, crypto.SHA256, sum[:])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("APP 调起支付签名失败: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{
|
||||
"appid": appID,
|
||||
"partnerid": w.config.Wxpay.MchID,
|
||||
"prepayid": prepayID,
|
||||
"package": "Sign=WXPay",
|
||||
"noncestr": nonceStr,
|
||||
"timestamp": ts,
|
||||
"sign": base64.StdEncoding.EncodeToString(sig),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CreateWechatMiniProgramOrder 创建微信小程序支付订单
|
||||
|
||||
Reference in New Issue
Block a user