修复编译问题和版本兼容问题

This commit is contained in:
WuYuuuub
2026-03-29 14:29:43 +08:00
parent 89a7c87bf6
commit a630ae733e
12 changed files with 157 additions and 118 deletions

View File

@@ -1,7 +1,6 @@
package pay
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/xml"
@@ -13,6 +12,7 @@ import (
"github.com/cloudprint/cloudprint-backend/config"
"github.com/iGoogle-ink/gopay"
"github.com/iGoogle-ink/gopay/wechat"
)
// RandomString generates a random string with given length
@@ -26,8 +26,9 @@ func RandomString(length int) string {
}
type WeChatPay struct {
client *gopay.WxPayClient
config *config.WeChatConfig
appID string
mchID string
apiKey string
}
type UnifiedOrderResponse struct {
@@ -59,13 +60,10 @@ type PayCallback struct {
}
func NewWeChatPay(cfg *config.WeChatConfig) *WeChatPay {
client := gopay.NewWxPayClient(cfg.AppID, cfg.MchID, cfg.ApiKey, cfg.NotifyURL)
if cfg.CertPath != "" {
client.SetCertFilePath(cfg.CertPath)
}
return &WeChatPay{
client: client,
config: cfg,
appID: cfg.AppID,
mchID: cfg.MchID,
apiKey: cfg.ApiKey,
}
}
@@ -83,70 +81,65 @@ func (p *WeChatPay) createSign(params map[string]string) string {
}
}
signStr := strings.Join(signParts, "&")
signStr += "&key=" + p.config.ApiKey
signStr += "&key=" + p.apiKey
md5Hash := md5.New()
md5Hash.Write([]byte(signStr))
return strings.ToUpper(hex.EncodeToString(md5Hash.Sum(nil)))
}
func (p *WeChatPay) UnifiedOrder(ctx context.Context, orderNo string, amount float64, description, openID, clientIP string) (*UnifiedOrderResponse, error) {
func (p *WeChatPay) UnifiedOrder(orderNo string, amount float64, description, openID, clientIP string) (*UnifiedOrderResponse, error) {
nonceStr := RandomString(32)
totalFee := int(amount * 100) // Convert to fen
// Build request body
body := gopay.UnifiedOrder{
AppID: p.config.AppID,
MchID: p.config.MchID,
NonceStr: nonceStr,
Body: description,
OutTradeNo: orderNo,
TotalFee: totalFee,
SpbillCreateIP: clientIP,
NotifyURL: p.config.NotifyURL,
TradeType: "JSAPI",
OpenID: openID,
}
// Build request body using gopay.BodyMap
body := gopay.BodyMap{}
body.Set("appid", p.appID)
body.Set("mch_id", p.mchID)
body.Set("nonce_str", nonceStr)
body.Set("body", description)
body.Set("out_trade_no", orderNo)
body.Set("total_fee", totalFee)
body.Set("spbill_create_ip", clientIP)
body.Set("trade_type", "JSAPI")
body.Set("openid", openID)
// Use go-pay client to call unified order
resp, err := p.client.UnifiedOrder(ctx, body)
client := wechat.NewClient(p.mchID, p.apiKey, p.appID, false)
resp, err := client.UnifiedOrder(body)
if err != nil {
return nil, fmt.Errorf("unified order failed: %w", err)
}
result := &UnifiedOrderResponse{
ReturnCode: resp.GetString("return_code"),
ReturnMsg: resp.GetString("return_msg"),
ResultCode: resp.GetString("result_code"),
PrepayID: resp.GetString("prepay_id"),
CodeURL: resp.GetString("code_url"),
MWebURL: resp.GetString("mweb_url"),
ErrCode: resp.GetString("err_code"),
ErrMsg: resp.GetString("err_msg"),
ReturnCode: resp.ReturnCode,
ReturnMsg: resp.ReturnMsg,
ResultCode: resp.ResultCode,
PrepayID: resp.PrepayId,
CodeURL: resp.CodeUrl,
MWebURL: resp.MwebUrl,
ErrCode: resp.ErrCode,
}
return result, nil
}
func (p *WeChatPay) QueryOrder(ctx context.Context, orderNo string) (string, int, error) {
func (p *WeChatPay) QueryOrder(orderNo string) (string, string, error) {
nonceStr := RandomString(32)
body := gopay.QueryOrder{
AppID: p.config.AppID,
MchID: p.config.MchID,
NonceStr: nonceStr,
OutTradeNo: orderNo,
}
body := gopay.BodyMap{}
body.Set("appid", p.appID)
body.Set("mch_id", p.mchID)
body.Set("nonce_str", nonceStr)
body.Set("out_trade_no", orderNo)
resp, err := p.client.QueryOrder(ctx, body)
client := wechat.NewClient(p.mchID, p.apiKey, p.appID, false)
resp, _, err := client.QueryOrder(body)
if err != nil {
return "", 0, err
return "", "", err
}
tradeState := resp.GetString("trade_state")
totalFee := resp.GetInt("total_fee")
return tradeState, totalFee, nil
return resp.TradeState, resp.TotalFee, nil
}
func (p *WeChatPay) ParseCallback(body []byte) (*PayCallback, error) {
@@ -180,7 +173,7 @@ func (p *WeChatPay) GenerateJSAPIPayParams(prepayID string) (map[string]string,
timestamp := fmt.Sprintf("%d", time.Now().Unix())
params := map[string]string{
"appId": p.config.AppID,
"appId": p.appID,
"timeStamp": timestamp,
"nonceStr": nonceStr,
"package": "prepay_id=" + prepayID,
@@ -188,7 +181,7 @@ func (p *WeChatPay) GenerateJSAPIPayParams(prepayID string) (map[string]string,
}
paySign := p.createSign(map[string]string{
"appId": p.config.AppID,
"appId": p.appID,
"timeStamp": timestamp,
"nonceStr": nonceStr,
"package": "prepay_id=" + prepayID,