wechat_cert.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package utils
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/beego/beego/v2/core/utils"
  6. "github.com/wechatpay-apiv3/wechatpay-go/core"
  7. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  8. payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
  9. )
  10. var WechatCertClient *core.Client
  11. const (
  12. //MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem"
  13. MchPKFileName = "/home/code/wechat_cert/cygx/apiclient_key.pem"
  14. Mchid = "1624495680"
  15. MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
  16. MchAPIv3Key = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
  17. )
  18. // 微信商户建立连接
  19. func init() {
  20. if !utils.FileExists(MchPKFileName) {
  21. //fmt.Println("商户私钥文件不存在")
  22. go SendAlarmMsg("微信商户链接失败 商户私钥文件不存在", 2)
  23. return
  24. }
  25. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  26. mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
  27. if err != nil {
  28. fmt.Println(err)
  29. go SendAlarmMsg("微信商户链接失败 LoadPrivateKeyWithPath"+err.Error(), 2)
  30. }
  31. ctx := context.Background()
  32. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  33. opts := []core.ClientOption{
  34. option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
  35. }
  36. client, err := core.NewClient(ctx, opts...)
  37. if err != nil {
  38. fmt.Println(err)
  39. go SendAlarmMsg("微信商户链接失败 NewClient"+err.Error(), 2)
  40. }
  41. WechatCertClient = client
  42. return
  43. }