蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.3 KiB

  1. package wechat
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. "github.com/go-pay/gopay"
  7. "github.com/go-pay/util"
  8. "github.com/go-pay/xlog"
  9. )
  10. func TestClient_UnifiedOrder(t *testing.T) {
  11. number := util.RandomString(32)
  12. xlog.Info("out_trade_no:", number)
  13. // 初始化参数Map
  14. bm := make(gopay.BodyMap)
  15. bm.Set("nonce_str", util.RandomString(32)).
  16. Set("body", "H5支付").
  17. Set("out_trade_no", number).
  18. Set("total_fee", 1).
  19. Set("spbill_create_ip", "127.0.0.1").
  20. Set("notify_url", "https://www.fmm.ink").
  21. Set("trade_type", TradeType_H5).
  22. Set("device_info", "WEB").
  23. Set("sign_type", SignType_MD5).
  24. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  25. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  26. bm.Set("type", "Wap")
  27. bm.Set("wap_url", "https://www.fmm.ink")
  28. bm.Set("wap_name", "H5测试支付")
  29. })
  30. }) /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  31. // 请求支付下单,成功后得到结果
  32. wxRsp, err := client.UnifiedOrder(ctx, bm)
  33. if err != nil {
  34. xlog.Errorf("client.UnifiedOrder(%+v),error:%+v", bm, err)
  35. return
  36. }
  37. xlog.Info("wxRsp:", *wxRsp)
  38. //xlog.Info("wxRsp.MwebUrl:", wxRsp.MwebUrl)
  39. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  40. // 获取小程序支付需要的paySign
  41. //pac := "prepay_id=" + wxRsp.PrepayId
  42. //paySign := GetMiniPaySign(appId, wxRsp.NonceStr, pac, SignType_MD5, timeStamp, apiKey)
  43. //xlog.Info("paySign:", paySign)
  44. // 获取H5支付需要的paySign
  45. pac := "prepay_id=" + wxRsp.PrepayId
  46. paySign := GetJsapiPaySign(appId, wxRsp.NonceStr, pac, SignType_MD5, timeStamp, apiKey)
  47. xlog.Debug("paySign:", paySign)
  48. // 获取小程序需要的paySign
  49. //paySign := GetAppPaySign(appId,"partnerid", wxRsp.NonceStr, wxRsp.PrepayId, SignType_MD5, timeStamp, apiKey)
  50. //xlog.Info("paySign:", paySign)
  51. }
  52. func TestClient_Micropay(t *testing.T) {
  53. number := util.RandomString(32)
  54. xlog.Info("out_trade_no:", number)
  55. // 初始化参数Map
  56. bm := make(gopay.BodyMap)
  57. bm.Set("nonce_str", util.RandomString(32)).
  58. Set("body", "扫用户付款码支付").
  59. Set("out_trade_no", number).
  60. Set("total_fee", 1).
  61. Set("spbill_create_ip", "127.0.0.1").
  62. Set("auth_code", "134622817080551492").
  63. Set("sign_type", SignType_MD5)
  64. // 请求支付,成功后得到结果
  65. wxRsp, err := client.Micropay(ctx, bm)
  66. if err != nil {
  67. xlog.Errorf("client.Micropay(%+v),error:%+v", bm, err)
  68. return
  69. }
  70. xlog.Debug("Response:", *wxRsp)
  71. ok, err := VerifySign(apiKey, SignType_MD5, wxRsp)
  72. if err != nil {
  73. xlog.Error(err)
  74. }
  75. xlog.Debug("同步验签结果:", ok) // 沙箱环境验签失败请用正式环境测
  76. }
  77. func TestClient_QueryOrder(t *testing.T) {
  78. // 初始化参数结构体
  79. bm := make(gopay.BodyMap)
  80. bm.Set("out_trade_no", "MfZC2segKxh0bnJSELbvKNeH3d9oWvvQ").
  81. Set("nonce_str", util.RandomString(32)).
  82. Set("sign_type", SignType_MD5)
  83. // 请求订单查询,成功后得到结果
  84. wxRsp, resBm, err := client.QueryOrder(ctx, bm)
  85. if err != nil {
  86. xlog.Errorf("client.QueryOrder(%+v),error:%+v", bm, err)
  87. return
  88. }
  89. xlog.Debug("wxRsp:", *wxRsp)
  90. xlog.Debug("resBm:", resBm)
  91. }
  92. func TestClient_CloseOrder(t *testing.T) {
  93. // 初始化参数结构体
  94. bm := make(gopay.BodyMap)
  95. bm.Set("out_trade_no", "MfZC2segKxh0bnJSELbvKNeH3d9oWvvQ").
  96. Set("nonce_str", util.RandomString(32)).
  97. Set("sign_type", SignType_MD5)
  98. // 请求关闭订单,成功后得到结果
  99. wxRsp, err := client.CloseOrder(ctx, bm)
  100. if err != nil {
  101. xlog.Errorf("client.CloseOrder(%+v),error:%+v", bm, err)
  102. return
  103. }
  104. xlog.Debug("wxRsp:", *wxRsp)
  105. }
  106. func TestClient_Refund(t *testing.T) {
  107. // 初始化参数结构体
  108. s := util.RandomString(64)
  109. xlog.Info("out_refund_no:", s)
  110. bm := make(gopay.BodyMap)
  111. bm.Set("out_trade_no", "QRcTBTbJLoDrWSW9FtpSFlgWhft2QbaY").
  112. Set("nonce_str", util.RandomString(32)).
  113. Set("sign_type", SignType_MD5).
  114. Set("out_refund_no", s).
  115. Set("total_fee", 101).
  116. Set("refund_fee", 101).
  117. Set("notify_url", "https://www.fmm.ink")
  118. // 请求申请退款(沙箱环境下,证书路径参数可传空)
  119. // body:参数Body
  120. wxRsp, resBm, err := client.Refund(ctx, bm)
  121. if err != nil {
  122. xlog.Errorf("client.Refund(%+v),error:%+v", bm, err)
  123. return
  124. }
  125. xlog.Debug("wxRsp:", *wxRsp)
  126. xlog.Debug("resBm:", resBm)
  127. }
  128. func TestClient_QueryRefund(t *testing.T) {
  129. // 初始化参数结构体
  130. bm := make(gopay.BodyMap)
  131. bm.Set("out_trade_no", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk").
  132. Set("nonce_str", util.RandomString(32)).
  133. Set("sign_type", SignType_MD5) /*.
  134. Set("transaction_id", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk").
  135. Set("out_refund_no", "vk4264I1UQ3Hm3E4AKsavK8npylGSgQA092f9ckUxp8A2gXmnsLEdsupURVTcaC7").
  136. Set("refund_id", "97HiM5j6kGmM2fk7fYMc8MgKhPnEQ5Rk")*/
  137. // 请求申请退款
  138. wxRsp, resBm, err := client.QueryRefund(ctx, bm)
  139. if err != nil {
  140. xlog.Errorf("client.QueryRefund(%+v),error:%+v", bm, err)
  141. return
  142. }
  143. xlog.Debug("wxRsp:", *wxRsp)
  144. xlog.Debug("resBm:", resBm)
  145. }
  146. func TestClient_Reverse(t *testing.T) {
  147. // 初始化参数Map
  148. bm := make(gopay.BodyMap)
  149. bm.Set("nonce_str", util.RandomString(32)).
  150. Set("out_trade_no", "6aDCor1nUcAihrV5JBlI09tLvXbUp02B").
  151. Set("sign_type", SignType_MD5)
  152. // 请求撤销订单,成功后得到结果,沙箱环境下,证书路径参数可传nil
  153. wxRsp, err := client.Reverse(ctx, bm)
  154. if err != nil {
  155. xlog.Errorf("client.Reverse(%+v),error:%+v", bm, err)
  156. return
  157. }
  158. xlog.Debug("Response:", wxRsp)
  159. }