| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package server
- import (
- "context"
- "fmt"
- "github.com/go-pay/gopay"
- "github.com/go-pay/gopay/wechat/v3"
- "github.com/shopspring/decimal"
- "github.com/spf13/viper"
- "go.uber.org/zap"
- "go_zh/pkg/utils"
- "sync"
- )
- type WeChatServer struct {
- MchId string
- PrivateKey string
- CertFile string //公钥证书地址
- PrivateKeyFile string //私钥证书地址
- CertificateSerialNo string
- WechatPayPublicKeyId string
- AppId string //appid
- AppSecret string
- AppletId string
- AppletKey string
- PublicKeyInfo string
- PrivateKeyFileInfo string
- }
- var (
- weChatServerInstance *WeChatServer
- weChatServerOnce sync.Once
- )
- // GetWeChatServerInstance 获取单例实例
- func GetWeChatServerInstance() *WeChatServer {
- weChatServerOnce.Do(func() {
- weChatServerInstance = &WeChatServer{
- MchId: viper.GetString("weChat.mchId"),
- PrivateKey: viper.GetString("weChat.privateKey"),
- CertFile: viper.GetString("weChat.certFile"),
- PrivateKeyFile: viper.GetString("weChat.keyFile"),
- CertificateSerialNo: viper.GetString("weChat.certificateSerialNo"),
- WechatPayPublicKeyId: viper.GetString("weChat.wechatPayPublicKeyId"),
- AppId: viper.GetString("weChat.appId"),
- AppSecret: viper.GetString("weChat.appSecret"),
- AppletId: viper.GetString("weChat.appletId"),
- AppletKey: viper.GetString("weChat.appletKey"),
- }
- weChatPublicKeyStr := utils.LoadPublicKeyWithPath(weChatServerInstance.CertFile)
- weChatServerInstance.PublicKeyInfo = weChatPublicKeyStr
- weChatPrivateKeyStr := utils.LoadPrivateKeyWithPath(weChatServerInstance.PrivateKeyFile)
- weChatServerInstance.PrivateKeyFileInfo = weChatPrivateKeyStr
- })
- return weChatServerInstance
- }
- // GetAliWapPay 获取支付宝手机网站支付
- func (weChat *WeChatServer) WeChatJsPay(orderSn string, payMoney decimal.Decimal, notifyUrl string, boby string, openId string) string {
- // NewClientV3 初始化微信客户端 v3
- // mchid:商户ID 或者服务商模式的 sp_mchid
- // serialNo:商户证书的证书序列号
- // apiV3Key:apiV3Key,商户平台获取
- // privateKey:私钥 apiclient_key.pem 读取后的内容
- client, err := wechat.NewClientV3(weChat.MchId, weChat.CertificateSerialNo, weChat.PrivateKey, weChat.PrivateKeyFileInfo)
- if err != nil {
- zap.L().Error("微信支付NewClient failed", zap.Error(err))
- }
- // 微信支付公钥自动同步验签(新微信支付用户推荐)
- err = client.AutoVerifySignByPublicKey([]byte(weChat.PublicKeyInfo), weChat.WechatPayPublicKeyId)
- if err != nil {
- zap.L().Error("微信支付同步验签err", zap.Error(err))
- return ""
- }
- bm := make(gopay.BodyMap)
- bm.Set("appid", weChat.AppId).
- Set("description", boby).
- Set("out_trade_no", orderSn).
- Set("notify_url", notifyUrl).
- SetBodyMap("amount", func(bm gopay.BodyMap) {
- bm.Set("total", payMoney.Mul(decimal.NewFromInt(100)).IntPart()).
- Set("currency", "CNY")
- }).
- SetBodyMap("payer", func(bm gopay.BodyMap) {
- bm.Set("openid", openId)
- })
- wxRsp, err := client.V3TransactionJsapi(context.Background(), bm)
- fmt.Println(wxRsp)
- return ""
- }
|