微信支付密钥使用 base64 字符串替换文件配置
This commit is contained in:
75
web/globals/wechatpay.go
Normal file
75
web/globals/wechatpay.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"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() {
|
||||
|
||||
// 加载商户私钥
|
||||
private, err := base64.StdEncoding.DecodeString(env.WechatPayMchPrivateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
appPrivateKey, err := utils.LoadPrivateKey(string(private))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 加载微信支付公钥
|
||||
public, err := base64.StdEncoding.DecodeString(env.WechatPayPublicKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wechatPublicKey, err := utils.LoadPublicKey(string(public))
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user