40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package wechat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sundynix-go/global"
|
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
|
)
|
|
|
|
// GetWxPayClient 初始化微信支付客户端
|
|
func GetWxPayClient() (*core.Client, error) {
|
|
|
|
//2.加载私钥
|
|
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(global.Config.WechatPay.PrivateKeyPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mchPublicKey, err := utils.LoadPublicKeyWithPath(global.Config.WechatPay.PublicKeyPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx := context.Background()
|
|
// 3. 创建客户端配置 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
|
|
opts := []core.ClientOption{
|
|
option.WithWechatPayPublicKeyAuthCipher(global.Config.WechatPay.MchId,
|
|
global.Config.WechatPay.MchCertificateSerialNumber,
|
|
mchPrivateKey, global.Config.WechatPay.PublicKeyId, mchPublicKey), // 自动处理签名/验签
|
|
}
|
|
client, err := core.NewClient(ctx, opts...)
|
|
|
|
if err != nil {
|
|
fmt.Printf("new wechat pay client err:%s", err)
|
|
}
|
|
return client, err
|
|
|
|
}
|