wx_pay.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/wechatpay-apiv3/wechatpay-go/core"
  7. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
  8. "hongze/hongze_cygx/utils"
  9. "time"
  10. )
  11. //const (
  12. // MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem"
  13. // Mchid = "1624495680"
  14. // MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
  15. // MchAPIv3Key = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
  16. //)
  17. //// 微信商户建立连接
  18. //func getWechatClient() (context.Context, *core.Client, error) {
  19. // var err error
  20. // defer func() {
  21. // if err != nil {
  22. // fmt.Println("err", err)
  23. // go utils.SendAlarmMsg(fmt.Sprint("微信商户建立连接失败 getWechatClient, err:", err.Error()), 2)
  24. // }
  25. // }()
  26. // // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
  27. // mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
  28. // if err != nil {
  29. // log.Print("load merchant private key error")
  30. // return nil, nil, err
  31. // }
  32. // ctx := context.Background()
  33. // // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  34. // opts := []core.ClientOption{
  35. // option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
  36. // }
  37. // client, err := core.NewClient(ctx, opts...)
  38. // if err != nil {
  39. // log.Printf("new wechat pay client err:%s", err)
  40. // return nil, nil, err
  41. // }
  42. // return ctx, client, nil
  43. //}
  44. //func init() {
  45. // GetQueryOrderByOutTradeNo("OD202404081631545174")
  46. //}
  47. // 根据订单号查询订单状态失败
  48. func GetQueryOrderByOutTradeNo(outTradeNo string) (tradeState string, statusCode int, itemResp *Transaction) {
  49. var err error
  50. defer func() {
  51. if err != nil {
  52. fmt.Println("err", err)
  53. go utils.SendAlarmMsg(fmt.Sprint("根据订单号查询订单状态失败 GetQueryOrderByOutTradeNo, err:", err.Error(), "outTradeNo", outTradeNo), 2)
  54. }
  55. }()
  56. //ctx, client, err := getWechatClient()
  57. //if err != nil {
  58. // log.Printf("getWechatClientt err:%s", err)
  59. // return
  60. //}
  61. ctx := context.Background()
  62. svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
  63. resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
  64. jsapi.QueryOrderByOutTradeNoRequest{
  65. OutTradeNo: core.String(outTradeNo),
  66. Mchid: core.String(utils.Mchid),
  67. },
  68. )
  69. statusCode = result.Response.StatusCode
  70. //fmt.Println(statusCode)
  71. //订单状态码不存在直接返回
  72. if statusCode == 404 {
  73. err = nil
  74. return
  75. }
  76. tradeState = *resp.TradeState
  77. data, err := json.Marshal(resp)
  78. if err != nil {
  79. return
  80. }
  81. jsonstr := string(data)
  82. //item := new(Transaction)
  83. if err = json.Unmarshal([]byte(jsonstr), &itemResp); err != nil {
  84. return
  85. }
  86. //itemResp = item
  87. //fmt.Println(itemResp)
  88. //fmt.Println(tradeState)
  89. //fmt.Println(itemResp.TransactionId)
  90. return
  91. }
  92. // Transaction
  93. type Transaction struct {
  94. Amount TransactionAmount `json:"amount,omitempty"`
  95. Appid string `json:"appid,omitempty"`
  96. Attach string `json:"attach,omitempty"`
  97. BankType string `json:"bank_type,omitempty"`
  98. Mchid string `json:"mchid,omitempty"`
  99. OutTradeNo string `json:"out_trade_no,omitempty"`
  100. Payer TransactionPayer `json:"payer,omitempty"`
  101. PromotionDetail []PromotionDetail `json:"promotion_detail,omitempty"`
  102. SuccessTime time.Time `json:"success_time,omitempty"`
  103. TradeState string `json:"trade_state,omitempty"`
  104. TradeStateDesc string `json:"trade_state_desc,omitempty"`
  105. TradeType string `json:"trade_type,omitempty"`
  106. TransactionId string `json:"transaction_id,omitempty"`
  107. }
  108. // TransactionAmount
  109. type TransactionAmount struct {
  110. Currency string `json:"currency,omitempty"`
  111. PayerCurrency string `json:"payer_currency,omitempty"`
  112. PayerTotal int64 `json:"payer_total,omitempty"`
  113. Total int64 `json:"total,omitempty"`
  114. }
  115. // TransactionPayer
  116. type TransactionPayer struct {
  117. Openid string `json:"openid,omitempty"`
  118. }
  119. // PromotionDetail
  120. type PromotionDetail struct {
  121. // 券ID
  122. CouponId *string `json:"coupon_id,omitempty"`
  123. // 优惠名称
  124. Name *string `json:"name,omitempty"`
  125. // GLOBAL:全场代金券;SINGLE:单品优惠
  126. Scope *string `json:"scope,omitempty"`
  127. // CASH:充值;NOCASH:预充值。
  128. Type *string `json:"type,omitempty"`
  129. // 优惠券面额
  130. Amount *int64 `json:"amount,omitempty"`
  131. // 活动ID,批次ID
  132. StockId *string `json:"stock_id,omitempty"`
  133. // 单位为分
  134. WechatpayContribute *int64 `json:"wechatpay_contribute,omitempty"`
  135. // 单位为分
  136. MerchantContribute *int64 `json:"merchant_contribute,omitempty"`
  137. // 单位为分
  138. OtherContribute *int64 `json:"other_contribute,omitempty"`
  139. // CNY:人民币,境内商户号仅支持人民币。
  140. Currency *string `json:"currency,omitempty"`
  141. GoodsDetail []PromotionGoodsDetail `json:"goods_detail,omitempty"`
  142. }
  143. // PromotionGoodsDetail
  144. type PromotionGoodsDetail struct {
  145. // 商品编码
  146. GoodsId *string `json:"goods_id"`
  147. // 商品数量
  148. Quantity *int64 `json:"quantity"`
  149. // 商品价格
  150. UnitPrice *int64 `json:"unit_price"`
  151. // 商品优惠金额
  152. DiscountAmount *int64 `json:"discount_amount"`
  153. // 商品备注
  154. GoodsRemark *string `json:"goods_remark,omitempty"`
  155. }