智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.3 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/enum"
  5. "applet/app/lib/wechatpay/core"
  6. "applet/app/lib/wechatpay/core/option"
  7. "applet/app/lib/wechatpay/utils"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func NewWxPayClient(ctx *gin.Context) (client *core.Client, err error) {
  11. sysCfgDb := db.SysCfgDb{}
  12. sysCfgDb.Set()
  13. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxSpMchId, enum.WxSpMchApiV3Key, enum.WxSpMchCertificateSerialNumber)
  14. var (
  15. mchID string = sysCfg[enum.WxSpMchId] // 商户号
  16. mchCertificateSerialNumber string = sysCfg[enum.WxSpMchCertificateSerialNumber] // 商户证书序列号
  17. mchAPIv3Key string = sysCfg[enum.WxSpMchApiV3Key] // 商户APIv3密钥
  18. )
  19. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  20. mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./static/wx/merchant/apiclient_key.pem")
  21. if err != nil {
  22. return
  23. }
  24. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  25. opts := []core.ClientOption{
  26. option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
  27. }
  28. client, err = core.NewClient(ctx, opts...)
  29. if err != nil {
  30. return
  31. }
  32. return
  33. }