wx_pay.go 5.3 KB

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