wx_pay.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/wechatpay-apiv3/wechatpay-go/core"
  6. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
  7. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
  8. "hongze/hongze_web_mfyx/models/order"
  9. "hongze/hongze_web_mfyx/utils"
  10. "time"
  11. )
  12. //const (
  13. // //MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem"
  14. // MchPKFileName = "/home/code/wechat_cert/cygx/apiclient_key.pem"
  15. // Mchid = "1624495680"
  16. // MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
  17. // MchAPIv3Key = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
  18. //)
  19. // 微信商户建立连接
  20. //func getWechatClient() (context.Context, *core.Client, error) {
  21. // // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  22. // mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
  23. // if err != nil {
  24. // log.Print("load merchant private key error")
  25. // return nil, nil, err
  26. // }
  27. // ctx := context.Background()
  28. // // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  29. // opts := []core.ClientOption{
  30. // option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
  31. // }
  32. //
  33. // client, err := core.NewClient(ctx, opts...)
  34. // if err != nil {
  35. // log.Printf("new wechat pay client err:%s", err)
  36. // return nil, nil, err
  37. // }
  38. // return ctx, client, nil
  39. //}
  40. //func init() {
  41. // ExampleNativeApiService_Prepay()
  42. //}
  43. func ExampleNativeApiServicePrepay(orderDetail *order.CygxOrder) (codeUrl string, err error) {
  44. defer func() {
  45. if err != nil {
  46. fmt.Println(err)
  47. go utils.SendAlarmMsg(fmt.Sprint("生成预支付交易单失败 ExampleNativeApiServicePrepay, err:", err.Error(), "OrderCode:", orderDetail.OrderCode), 2)
  48. }
  49. }()
  50. //如果是试用卡,商品名称就显示日卡月卡,如果是单场付费就显示,单场活动名称
  51. if orderDetail.OrderType == 1 {
  52. orderDetail.SourceTitle = "畅读" + orderDetail.GoodsName
  53. }
  54. //ctx, client, _ := getWechatClient()
  55. ctx := context.Background()
  56. svc := native.NativeApiService{Client: utils.WechatCertClient}
  57. resp, _, err := svc.Prepay(ctx,
  58. native.PrepayRequest{
  59. Appid: core.String(utils.WxAppIdMfyx),
  60. Mchid: core.String(utils.Mchid),
  61. Description: core.String(orderDetail.SourceTitle),
  62. OutTradeNo: core.String(orderDetail.OutTradeNo),
  63. TimeExpire: core.Time(time.Now().Add(10 * time.Minute)),
  64. Attach: core.String(""),
  65. NotifyUrl: core.String(utils.WxPayJsapiNotifyUrl),
  66. GoodsTag: core.String(""),
  67. SupportFapiao: core.Bool(false),
  68. Amount: &native.Amount{
  69. Currency: core.String("CNY"),
  70. Total: core.Int64(int64(orderDetail.OrderMoney * 100)), // 分
  71. },
  72. },
  73. )
  74. if err != nil {
  75. fmt.Println(err)
  76. return
  77. }
  78. codeUrl = *resp.CodeUrl
  79. return
  80. }
  81. type WechatPayCallback struct {
  82. MchID string `json:"mchid"`
  83. AppID string `json:"appid"`
  84. OutTradeNo string `json:"out_trade_no"`
  85. TransactionID string `json:"transaction_id"`
  86. TradeType string `json:"trade_type"`
  87. TradeState string `json:"trade_state"`
  88. TradeStateDesc string `json:"trade_state_desc"`
  89. BankType string `json:"bank_type"`
  90. Attach string `json:"attach"`
  91. SuccessTime time.Time `json:"success_time"`
  92. Payer struct {
  93. OpenID string `json:"openid"`
  94. } `json:"payer"`
  95. Amount struct {
  96. Total int `json:"total"`
  97. PayerTotal int `json:"payer_total"`
  98. Currency string `json:"currency"`
  99. PayerCurrency string `json:"payer_currency"`
  100. } `json:"amount"`
  101. }
  102. // 订单超时手动关闭订单
  103. func ExampleNativeApiService_CloseOrder(OutTradeNo string) {
  104. var err error
  105. defer func() {
  106. if err != nil {
  107. fmt.Println(err)
  108. go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleNativeApiService_CloseOrder, err:", err.Error(), "OutTradeNo:", OutTradeNo), 2)
  109. }
  110. }()
  111. ctx := context.Background()
  112. svc := native.NativeApiService{Client: utils.WechatCertClient}
  113. _, err = svc.CloseOrder(ctx,
  114. native.CloseOrderRequest{
  115. OutTradeNo: core.String(OutTradeNo),
  116. Mchid: core.String(utils.Mchid),
  117. },
  118. )
  119. return
  120. }
  121. // 订单超时手动关闭订单(两者功能更一样,与小程序端代码统一)
  122. func ExampleJsapiApiService_CloseOrder(OutTradeNo string) {
  123. var err error
  124. defer func() {
  125. if err != nil {
  126. fmt.Println(err)
  127. go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleJsapiApiService_CloseOrder, err:", err.Error(), "OutTradeNo:", OutTradeNo), 2)
  128. }
  129. }()
  130. ctx := context.Background()
  131. svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
  132. _, err = svc.CloseOrder(ctx,
  133. jsapi.CloseOrderRequest{
  134. OutTradeNo: core.String(OutTradeNo),
  135. Mchid: core.String(utils.Mchid),
  136. },
  137. )
  138. return
  139. }