package svc import ( "applet/app/db" "applet/app/enum" "github.com/gin-gonic/gin" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/utils" ) func NewWxPayClient(ctx *gin.Context) (client *core.Client, err error) { sysCfgDb := db.SysCfgDb{} sysCfgDb.Set() sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxMchId, enum.WxMchApiV3Key, enum.WxMchCertificateSerialNumber) var ( mchID string = sysCfg[enum.WxMchId] // 商户号 mchCertificateSerialNumber string = sysCfg[enum.WxMchCertificateSerialNumber] // 商户证书序列号 mchAPIv3Key string = sysCfg[enum.WxMchApiV3Key] // 商户APIv3密钥 ) // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名 mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./static/wx/merchant/apiclient_key.pem") if err != nil { return } // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力 opts := []core.ClientOption{ option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key), } client, err = core.NewClient(ctx, opts...) if err != nil { return } return }