1 Star 1 Fork 0

颜言/gopay

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client.go 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
颜言 提交于 2024-09-13 09:11 +08:00 . 重构:统一gopay包的依赖路径
package wechat
import (
"context"
"crypto/rsa"
"sync"
"gitee.com/ujq/gopay/pkg/xlog"
"gitee.com/ujq/gopay"
"gitee.com/ujq/gopay/pkg/crypto/xpem"
"gitee.com/ujq/gopay/pkg/xhttp"
)
// ClientV3 微信支付 V3
type ClientV3 struct {
Mchid string
ApiV3Key []byte
SerialNo string
WxSerialNo string
autoSign bool
rwMu sync.RWMutex
hc *xhttp.Client
privateKey *rsa.PrivateKey
wxPublicKey *rsa.PublicKey
ctx context.Context
DebugSwitch gopay.DebugSwitch
logger xlog.XLogger
SnCertMap map[string]*rsa.PublicKey // key: serial_no
}
// NewClientV3 初始化微信客户端 V3
// mchid:商户ID 或者服务商模式的 sp_mchid
// serialNo:商户API证书的证书序列号
// apiV3Key:APIv3Key,商户平台获取
// privateKey:商户API证书下载后,私钥 apiclient_key.pem 读取后的字符串内容
func NewClientV3(mchid, serialNo, apiV3Key, privateKey string) (client *ClientV3, err error) {
if mchid == gopay.NULL || serialNo == gopay.NULL || apiV3Key == gopay.NULL || privateKey == gopay.NULL {
return nil, gopay.MissWechatInitParamErr
}
priKey, err := xpem.DecodePrivateKey([]byte(privateKey))
if err != nil {
return nil, err
}
logger := xlog.NewLogger()
logger.SetLevel(xlog.DebugLevel)
client = &ClientV3{
Mchid: mchid,
SerialNo: serialNo,
ApiV3Key: []byte(apiV3Key),
privateKey: priKey,
ctx: context.Background(),
DebugSwitch: gopay.DebugOff,
logger: logger,
hc: xhttp.NewClient(),
}
return client, nil
}
// AutoVerifySign 开启请求完自动验签功能(默认不开启,推荐开启)
// 开启自动验签,自动开启每12小时一次轮询,请求最新证书操作
func (c *ClientV3) AutoVerifySign(autoRefresh ...bool) (err error) {
wxSerialNo, certMap, err := c.GetAndSelectNewestCert()
if err != nil {
return err
}
if len(c.SnCertMap) <= 0 {
c.SnCertMap = make(map[string]*rsa.PublicKey)
}
for sn, cert := range certMap {
// decode cert
pubKey, err := xpem.DecodePublicKey([]byte(cert))
if err != nil {
return err
}
c.SnCertMap[sn] = pubKey
}
c.WxSerialNo = wxSerialNo
c.wxPublicKey = c.SnCertMap[wxSerialNo]
if len(autoRefresh) == 1 && !autoRefresh[0] {
return
}
c.autoSign = true
go c.autoCheckCertProc()
return
}
// SetBodySize 设置http response body size(MB)
func (c *ClientV3) SetBodySize(sizeMB int) {
if sizeMB > 0 {
c.hc.SetBodySize(sizeMB)
}
}
// SetHttpClient 设置自定义的xhttp.Client
func (c *ClientV3) SetHttpClient(client *xhttp.Client) {
if client != nil {
c.hc = client
}
}
func (c *ClientV3) SetLogger(logger xlog.XLogger) {
if logger != nil {
c.logger = logger
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ujq/gopay.git
git@gitee.com:ujq/gopay.git
ujq
gopay
gopay
95cb943fb81a

搜索帮助