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.WxSpMchId, enum.WxSpMchApiV3Key, enum.WxSpMchCertificateSerialNumber) var ( mchID string = sysCfg[enum.WxSpMchId] // 商户号 mchCertificateSerialNumber string = sysCfg[enum.WxSpMchCertificateSerialNumber] // 商户证书序列号 mchAPIv3Key string = sysCfg[enum.WxSpMchApiV3Key] // 商户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 }