wx_pay.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package cygx
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/wechatpay-apiv3/wechatpay-go/core"
  6. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  7. "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
  8. payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
  9. "hongze/hz_crm_api/models/cygx"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/utils"
  12. )
  13. const (
  14. //MchPKFileName = "./utils/cert/apiclient_key.pem"
  15. //MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem"
  16. MchPKFileName = "/home/code/wechat_cert/cygx/apiclient_key.pem"
  17. Mchid = "1624495680"
  18. MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
  19. MchAPIv3Key = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
  20. )
  21. // 微信商户建立连接
  22. //func getWechatClient() (context.Context, *core.Client, error) {
  23. // var err error
  24. // defer func() {
  25. // if err != nil {
  26. // fmt.Println(err)
  27. // go alarm_msg.SendAlarmMsg(fmt.Sprint("微信商户建立连接失败 getWechatClient, err:", err.Error()), 2)
  28. // }
  29. // }()
  30. // // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  31. // mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
  32. // if err != nil {
  33. // log.Print("load merchant private key error")
  34. // return nil, nil, err
  35. // }
  36. // ctx := context.Background()
  37. // // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  38. // opts := []core.ClientOption{
  39. // option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
  40. // }
  41. // client, err := core.NewClient(ctx, opts...)
  42. // if err != nil {
  43. // log.Printf("new wechat pay client err:%s", err)
  44. // return nil, nil, err
  45. // }
  46. // return ctx, client, nil
  47. //}
  48. var WechatCertClient *core.Client
  49. // 微信商户建立连接(全局)
  50. func init() {
  51. if !utils.FileIsExist(MchPKFileName) {
  52. //fmt.Println("商户私钥文件不存在")
  53. go alarm_msg.SendAlarmMsg("微信商户链接失败 商户私钥文件不存在", 2)
  54. return
  55. }
  56. // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  57. mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
  58. if err != nil {
  59. fmt.Println(err)
  60. go alarm_msg.SendAlarmMsg("微信商户链接失败 LoadPrivateKeyWithPath"+err.Error(), 2)
  61. }
  62. ctx := context.Background()
  63. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  64. opts := []core.ClientOption{
  65. option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
  66. }
  67. client, err := core.NewClient(ctx, opts...)
  68. if err != nil {
  69. fmt.Println(err)
  70. go alarm_msg.SendAlarmMsg("微信商户链接失败 NewClient"+err.Error(), 2)
  71. }
  72. WechatCertClient = client
  73. return
  74. }
  75. // 微信商户退款
  76. func RefundsApiService(orderDetail *cygx.CygxOrderResp) (statusCode int, err error) {
  77. defer func() {
  78. if err != nil {
  79. fmt.Println(err)
  80. go alarm_msg.SendAlarmMsg(fmt.Sprint("微信商户退款失败 RefundsApiService, err:", err.Error()), 2)
  81. }
  82. }()
  83. //ctx, client, err := getWechatClient()
  84. //if err != nil {
  85. // //log.Printf("getWechatClientt err:%s", err)
  86. // return
  87. //}
  88. ctx := context.Background()
  89. svc := refunddomestic.RefundsApiService{Client: WechatCertClient}
  90. resp, result, err := svc.Create(ctx,
  91. refunddomestic.CreateRequest{
  92. //SubMchid: core.String(Mchid),
  93. TransactionId: core.String(orderDetail.OutTradeCode),
  94. OutTradeNo: core.String(orderDetail.OutTradeNo),
  95. OutRefundNo: core.String("RE" + orderDetail.OrderCode),
  96. Reason: core.String("退款"),
  97. NotifyUrl: core.String(utils.Wx_MFYX_Refund_NotifyUrl),
  98. FundsAccount: refunddomestic.REQFUNDSACCOUNT_AVAILABLE.Ptr(),
  99. Amount: &refunddomestic.AmountReq{
  100. Currency: core.String("CNY"),
  101. //From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{
  102. // Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(),
  103. // Amount: core.Int64(444),
  104. //}},
  105. Refund: core.Int64(int64(orderDetail.PayMoney * 100)),
  106. Total: core.Int64(int64(orderDetail.PayMoney * 100)),
  107. },
  108. },
  109. )
  110. if err != nil {
  111. return
  112. } else {
  113. utils.FileLogCygx.Info("status=%d resp=%s", result.Response.StatusCode, resp)
  114. }
  115. statusCode = result.Response.StatusCode
  116. return
  117. }