wx_UnifiedOrder.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package wechat
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/iGoogle-ink/gopay"
  7. "github.com/iGoogle-ink/gopay/wechat"
  8. )
  9. func UnifiedOrder() {
  10. //初始化微信客户端
  11. // appId:应用ID
  12. // mchId:商户ID
  13. // apiKey:API秘钥值
  14. // isProd:是否是正式环境
  15. client := wechat.NewClient("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", false)
  16. //设置国家
  17. client.SetCountry(wechat.China)
  18. number := gopay.GetRandomString(32)
  19. fmt.Println("out_trade_no:", number)
  20. //初始化参数Map
  21. bm := make(gopay.BodyMap)
  22. bm.Set("nonce_str", gopay.GetRandomString(32))
  23. bm.Set("body", "H5支付")
  24. bm.Set("out_trade_no", number)
  25. bm.Set("total_fee", 1)
  26. bm.Set("spbill_create_ip", "127.0.0.1")
  27. bm.Set("notify_url", "http://www.gopay.ink")
  28. bm.Set("trade_type", wechat.TradeType_H5)
  29. bm.Set("device_info", "WEB")
  30. bm.Set("sign_type", wechat.SignType_MD5)
  31. sceneInfo := make(map[string]map[string]string)
  32. h5Info := make(map[string]string)
  33. h5Info["type"] = "Wap"
  34. h5Info["wap_url"] = "http://www.gopay.ink"
  35. h5Info["wap_name"] = "H5测试支付"
  36. sceneInfo["h5_info"] = h5Info
  37. bm.Set("scene_info", sceneInfo)
  38. //body.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")
  39. // 正式
  40. //sign := gopay.GetWeChatParamSign("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", body)
  41. // 沙箱
  42. //sign, _ := gopay.GetWeChatSanBoxParamSign("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", body)
  43. //body.Set("sign", sign)
  44. //请求支付下单,成功后得到结果
  45. wxRsp, err := client.UnifiedOrder(bm)
  46. if err != nil {
  47. fmt.Println("Error:", err)
  48. return
  49. }
  50. fmt.Println("wxRsp:", *wxRsp)
  51. //fmt.Println("wxRsp.MwebUrl:", wxRsp.MwebUrl)
  52. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  53. //获取小程序支付需要的paySign
  54. //pac := "prepay_id=" + wxRsp.PrepayId
  55. //paySign := wechat.GetMiniPaySign("wxdaa2ab9ef87b5497", wxRsp.NonceStr, pac, wechat.SignType_MD5, timeStamp, "GFDS8j98rewnmgl45wHTt980jg543abc")
  56. //fmt.Println("paySign:", paySign)
  57. //获取H5支付需要的paySign
  58. pac := "prepay_id=" + wxRsp.PrepayId
  59. paySign := wechat.GetH5PaySign("wxdaa2ab9ef87b5497", wxRsp.NonceStr, pac, wechat.SignType_MD5, timeStamp, "GFDS8j98rewnmgl45wHTt980jg543abc")
  60. fmt.Println("paySign:", paySign)
  61. //获取小程序需要的paySign
  62. //paySign := wechat.GetAppPaySign("wxdaa2ab9ef87b5497","", wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, "GFDS8j98rewnmgl45wHTt980jg543abc")
  63. //fmt.Println("paySign:", paySign)
  64. }