64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package globals
|
|
|
|
import (
|
|
"context"
|
|
"platform/pkg/env"
|
|
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
|
|
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
|
)
|
|
|
|
var WechatPay *WechatPayClient
|
|
|
|
type WechatPayClient struct {
|
|
Native *native.NativeApiService
|
|
Notify *notify.Handler
|
|
}
|
|
|
|
func InitWechatPay() {
|
|
|
|
// 加载 rsa 密钥文件
|
|
appPrivateKey, err := utils.LoadPrivateKeyWithPath(env.WechatPayMchPrivateKeyPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
wechatPublicKey, err := utils.LoadPublicKeyWithPath(env.WechatPayPublicKeyPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// 创建 WechatPay 客户端
|
|
client, err := core.NewClient(context.Background(),
|
|
option.WithWechatPayPublicKeyAuthCipher(
|
|
env.WechatPayMchId,
|
|
env.WechatPayMchPrivateKeySerial,
|
|
appPrivateKey,
|
|
env.WechatPayPublicKeyId,
|
|
wechatPublicKey,
|
|
),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// 创建 WechatPay 通知处理器
|
|
handler, err := notify.NewRSANotifyHandler(env.WechatPayApiCert, verifiers.NewSHA256WithRSAPubkeyVerifier(
|
|
env.WechatPayPublicKeyId,
|
|
*wechatPublicKey,
|
|
))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// 创建 WechatPay 服务
|
|
WechatPay = &WechatPayClient{
|
|
Native: &native.NativeApiService{Client: client},
|
|
Notify: handler,
|
|
}
|
|
}
|