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, } }