wx_Micropay.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package wechat
  2. import (
  3. "fmt"
  4. "github.com/iGoogle-ink/gopay"
  5. "github.com/iGoogle-ink/gopay/wechat"
  6. )
  7. func Micropay() {
  8. //初始化微信客户端
  9. // appId:应用ID
  10. // MchID:商户ID
  11. // ApiKey:Key值
  12. // isProd:是否是正式环境
  13. client := wechat.NewClient("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", false)
  14. // 初始化参数Map
  15. bm := make(gopay.BodyMap)
  16. bm.Set("nonce_str", gopay.GetRandomString(32))
  17. bm.Set("body", "扫用户付款码支付")
  18. number := gopay.GetRandomString(32)
  19. fmt.Println("out_trade_no:", number)
  20. bm.Set("out_trade_no", number)
  21. bm.Set("total_fee", 1)
  22. bm.Set("spbill_create_ip", "127.0.0.1")
  23. bm.Set("auth_code", "134595229789828537")
  24. bm.Set("sign_type", wechat.SignType_MD5)
  25. sign := wechat.GetParamSign("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", bm)
  26. //sign, _ := gopay.GetSanBoxParamSign("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", body)
  27. bm.Set("sign", sign)
  28. //请求支付,成功后得到结果
  29. wxRsp, err := client.Micropay(bm)
  30. if err != nil {
  31. fmt.Println("Error:", err)
  32. return
  33. }
  34. fmt.Println("Response:", *wxRsp)
  35. ok, err := wechat.VerifySign("GFDS8j98rewnmgl45wHTt980jg543abc", wechat.SignType_MD5, wxRsp)
  36. if err != nil {
  37. fmt.Println("err:", err)
  38. }
  39. fmt.Println("同步验签结果:", ok)
  40. }