weChatServer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/go-pay/gopay"
  6. "github.com/go-pay/gopay/wechat/v3"
  7. "github.com/shopspring/decimal"
  8. "github.com/spf13/viper"
  9. "go.uber.org/zap"
  10. "go_zh/pkg/utils"
  11. "sync"
  12. )
  13. type WeChatServer struct {
  14. MchId string
  15. PrivateKey string
  16. CertFile string //公钥证书地址
  17. PrivateKeyFile string //私钥证书地址
  18. CertificateSerialNo string
  19. WechatPayPublicKeyId string
  20. AppId string //appid
  21. AppSecret string
  22. AppletId string
  23. AppletKey string
  24. PublicKeyInfo string
  25. PrivateKeyFileInfo string
  26. }
  27. var (
  28. weChatServerInstance *WeChatServer
  29. weChatServerOnce sync.Once
  30. )
  31. // GetWeChatServerInstance 获取单例实例
  32. func GetWeChatServerInstance() *WeChatServer {
  33. weChatServerOnce.Do(func() {
  34. weChatServerInstance = &WeChatServer{
  35. MchId: viper.GetString("weChat.mchId"),
  36. PrivateKey: viper.GetString("weChat.privateKey"),
  37. CertFile: viper.GetString("weChat.certFile"),
  38. PrivateKeyFile: viper.GetString("weChat.keyFile"),
  39. CertificateSerialNo: viper.GetString("weChat.certificateSerialNo"),
  40. WechatPayPublicKeyId: viper.GetString("weChat.wechatPayPublicKeyId"),
  41. AppId: viper.GetString("weChat.appId"),
  42. AppSecret: viper.GetString("weChat.appSecret"),
  43. AppletId: viper.GetString("weChat.appletId"),
  44. AppletKey: viper.GetString("weChat.appletKey"),
  45. }
  46. weChatPublicKeyStr := utils.LoadPublicKeyWithPath(weChatServerInstance.CertFile)
  47. weChatServerInstance.PublicKeyInfo = weChatPublicKeyStr
  48. weChatPrivateKeyStr := utils.LoadPrivateKeyWithPath(weChatServerInstance.PrivateKeyFile)
  49. weChatServerInstance.PrivateKeyFileInfo = weChatPrivateKeyStr
  50. })
  51. return weChatServerInstance
  52. }
  53. // GetAliWapPay 获取支付宝手机网站支付
  54. func (weChat *WeChatServer) WeChatJsPay(orderSn string, payMoney decimal.Decimal, notifyUrl string, boby string, openId string) string {
  55. // NewClientV3 初始化微信客户端 v3
  56. // mchid:商户ID 或者服务商模式的 sp_mchid
  57. // serialNo:商户证书的证书序列号
  58. // apiV3Key:apiV3Key,商户平台获取
  59. // privateKey:私钥 apiclient_key.pem 读取后的内容
  60. client, err := wechat.NewClientV3(weChat.MchId, weChat.CertificateSerialNo, weChat.PrivateKey, weChat.PrivateKeyFileInfo)
  61. if err != nil {
  62. zap.L().Error("微信支付NewClient failed", zap.Error(err))
  63. }
  64. // 微信支付公钥自动同步验签(新微信支付用户推荐)
  65. err = client.AutoVerifySignByPublicKey([]byte(weChat.PublicKeyInfo), weChat.WechatPayPublicKeyId)
  66. if err != nil {
  67. zap.L().Error("微信支付同步验签err", zap.Error(err))
  68. return ""
  69. }
  70. bm := make(gopay.BodyMap)
  71. bm.Set("appid", weChat.AppId).
  72. Set("description", boby).
  73. Set("out_trade_no", orderSn).
  74. Set("notify_url", notifyUrl).
  75. SetBodyMap("amount", func(bm gopay.BodyMap) {
  76. bm.Set("total", payMoney.Mul(decimal.NewFromInt(100)).IntPart()).
  77. Set("currency", "CNY")
  78. }).
  79. SetBodyMap("payer", func(bm gopay.BodyMap) {
  80. bm.Set("openid", openId)
  81. })
  82. wxRsp, err := client.V3TransactionJsapi(context.Background(), bm)
  83. fmt.Println(wxRsp)
  84. return ""
  85. }