|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/enum"
- "applet/app/lib/wechatpay/core"
- "applet/app/lib/wechatpay/core/option"
- "applet/app/lib/wechatpay/utils"
- "github.com/gin-gonic/gin"
- )
-
- 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
- }
|