package utils import ( "context" "fmt" "github.com/beego/beego/v2/core/utils" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core/option" payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils" ) var WechatCertClient *core.Client const ( //MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem" MchPKFileName = "/home/code/wechat_cert/cygx/apiclient_key.pem" Mchid = "1624495680" MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418" MchAPIv3Key = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX" ) // 微信商户建立连接 func init() { if !utils.FileExists(MchPKFileName) { //fmt.Println("商户私钥文件不存在") go SendAlarmMsg("微信商户链接失败 商户私钥文件不存在", 2) return } // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名 mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName) if err != nil { fmt.Println(err) go SendAlarmMsg("微信商户链接失败 LoadPrivateKeyWithPath"+err.Error(), 2) } ctx := context.Background() // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力 opts := []core.ClientOption{ option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key), } client, err := core.NewClient(ctx, opts...) if err != nil { fmt.Println(err) go SendAlarmMsg("微信商户链接失败 NewClient"+err.Error(), 2) } WechatCertClient = client return }