wx_pay.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // 订单超时手动关闭订单
  93. func ExampleJsapiApiService_CloseOrder(OutTradeNo string) {
  94. var err error
  95. defer func() {
  96. if err != nil {
  97. fmt.Println(err)
  98. go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleJsapiApiService_CloseOrder, err:", err.Error(), "OrderCode:", OutTradeNo), 2)
  99. }
  100. }()
  101. ctx := context.Background()
  102. svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
  103. _, err = svc.CloseOrder(ctx,
  104. jsapi.CloseOrderRequest{
  105. OutTradeNo: core.String(OutTradeNo),
  106. Mchid: core.String(utils.Mchid),
  107. },
  108. )
  109. return
  110. }
  111. // Transaction
  112. type Transaction struct {
  113. Amount TransactionAmount `json:"amount,omitempty"`
  114. Appid string `json:"appid,omitempty"`
  115. Attach string `json:"attach,omitempty"`
  116. BankType string `json:"bank_type,omitempty"`
  117. Mchid string `json:"mchid,omitempty"`
  118. OutTradeNo string `json:"out_trade_no,omitempty"`
  119. Payer TransactionPayer `json:"payer,omitempty"`
  120. PromotionDetail []PromotionDetail `json:"promotion_detail,omitempty"`
  121. SuccessTime time.Time `json:"success_time,omitempty"`
  122. TradeState string `json:"trade_state,omitempty"`
  123. TradeStateDesc string `json:"trade_state_desc,omitempty"`
  124. TradeType string `json:"trade_type,omitempty"`
  125. TransactionId string `json:"transaction_id,omitempty"`
  126. }
  127. // TransactionAmount
  128. type TransactionAmount struct {
  129. Currency string `json:"currency,omitempty"`
  130. PayerCurrency string `json:"payer_currency,omitempty"`
  131. PayerTotal int64 `json:"payer_total,omitempty"`
  132. Total int64 `json:"total,omitempty"`
  133. }
  134. // TransactionPayer
  135. type TransactionPayer struct {
  136. Openid string `json:"openid,omitempty"`
  137. }
  138. // PromotionDetail
  139. type PromotionDetail struct {
  140. // 券ID
  141. CouponId *string `json:"coupon_id,omitempty"`
  142. // 优惠名称
  143. Name *string `json:"name,omitempty"`
  144. // GLOBAL:全场代金券;SINGLE:单品优惠
  145. Scope *string `json:"scope,omitempty"`
  146. // CASH:充值;NOCASH:预充值。
  147. Type *string `json:"type,omitempty"`
  148. // 优惠券面额
  149. Amount *int64 `json:"amount,omitempty"`
  150. // 活动ID,批次ID
  151. StockId *string `json:"stock_id,omitempty"`
  152. // 单位为分
  153. WechatpayContribute *int64 `json:"wechatpay_contribute,omitempty"`
  154. // 单位为分
  155. MerchantContribute *int64 `json:"merchant_contribute,omitempty"`
  156. // 单位为分
  157. OtherContribute *int64 `json:"other_contribute,omitempty"`
  158. // CNY:人民币,境内商户号仅支持人民币。
  159. Currency *string `json:"currency,omitempty"`
  160. GoodsDetail []PromotionGoodsDetail `json:"goods_detail,omitempty"`
  161. }
  162. // PromotionGoodsDetail
  163. type PromotionGoodsDetail struct {
  164. // 商品编码
  165. GoodsId *string `json:"goods_id"`
  166. // 商品数量
  167. Quantity *int64 `json:"quantity"`
  168. // 商品价格
  169. UnitPrice *int64 `json:"unit_price"`
  170. // 商品优惠金额
  171. DiscountAmount *int64 `json:"discount_amount"`
  172. // 商品备注
  173. GoodsRemark *string `json:"goods_remark,omitempty"`
  174. }