wechat_servier_api.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //==================================
  2. // * Name:Jerry
  3. // * DateTime:2019/5/6 13:16
  4. // * Desc:
  5. //==================================
  6. package gopay
  7. import (
  8. "bytes"
  9. "crypto/hmac"
  10. "crypto/md5"
  11. "crypto/sha256"
  12. "crypto/tls"
  13. "encoding/hex"
  14. "github.com/parnurzeal/gorequest"
  15. "strings"
  16. )
  17. func HttpAgent() (agent *gorequest.SuperAgent) {
  18. agent = gorequest.New()
  19. agent.TLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  20. return
  21. }
  22. //JSAPI支付,支付参数后,再次计算出小程序用的paySign
  23. func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
  24. buffer := new(bytes.Buffer)
  25. buffer.WriteString("appId=")
  26. buffer.WriteString(appId)
  27. buffer.WriteString("&nonceStr=")
  28. buffer.WriteString(nonceStr)
  29. buffer.WriteString("&package=")
  30. buffer.WriteString(prepayId)
  31. buffer.WriteString("&signType=")
  32. buffer.WriteString(signType)
  33. buffer.WriteString("&timeStamp=")
  34. buffer.WriteString(timeStamp)
  35. buffer.WriteString("&key=")
  36. buffer.WriteString(secretKey)
  37. signStr := buffer.String()
  38. var hashSign []byte
  39. if signType == SignType_MD5 {
  40. hash := md5.New()
  41. hash.Write([]byte(signStr))
  42. hashSign = hash.Sum(nil)
  43. } else {
  44. hash := hmac.New(sha256.New, []byte(secretKey))
  45. hash.Write([]byte(signStr))
  46. hashSign = hash.Sum(nil)
  47. }
  48. paySign = strings.ToUpper(hex.EncodeToString(hashSign))
  49. return
  50. }
  51. //JSAPI支付,支付参数后,再次计算出微信内H5支付需要用的paySign
  52. func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
  53. buffer := new(bytes.Buffer)
  54. buffer.WriteString("appId=")
  55. buffer.WriteString(appId)
  56. buffer.WriteString("&nonceStr=")
  57. buffer.WriteString(nonceStr)
  58. buffer.WriteString("&package=")
  59. buffer.WriteString(prepayId)
  60. buffer.WriteString("&signType=")
  61. buffer.WriteString(signType)
  62. buffer.WriteString("&timeStamp=")
  63. buffer.WriteString(timeStamp)
  64. buffer.WriteString("&key=")
  65. buffer.WriteString(secretKey)
  66. signStr := buffer.String()
  67. var hashSign []byte
  68. if signType == SignType_MD5 {
  69. hash := md5.New()
  70. hash.Write([]byte(signStr))
  71. hashSign = hash.Sum(nil)
  72. } else {
  73. hash := hmac.New(sha256.New, []byte(secretKey))
  74. hash.Write([]byte(signStr))
  75. hashSign = hash.Sum(nil)
  76. }
  77. paySign = strings.ToUpper(hex.EncodeToString(hashSign))
  78. return
  79. }
  80. //获取微信用户的OpenId、SessionKey、UnionId
  81. // appId:APPID
  82. // appSecret:AppSecret
  83. // wxCode:小程序调用wx.login 获取的code
  84. func Code2Session(appId, appSecret, wxCode string) (userRsp *Code2SessionRsp, err error) {
  85. userRsp = new(Code2SessionRsp)
  86. url := "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + wxCode + "&grant_type=authorization_code"
  87. agent := HttpAgent()
  88. _, _, errs := agent.Get(url).EndStruct(userRsp)
  89. if len(errs) > 0 {
  90. return nil, errs[0]
  91. } else {
  92. return userRsp, nil
  93. }
  94. }
  95. //获取小程序全局唯一后台接口调用凭据(AccessToken:157字符)
  96. // appId:APPID
  97. // appSecret:AppSecret
  98. func GetAccessToken(appId, appSecret string) (rsp *GetAccessTokenRsp, err error) {
  99. rsp = new(GetAccessTokenRsp)
  100. url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
  101. agent := HttpAgent()
  102. _, _, errs := agent.Get(url).EndStruct(rsp)
  103. if len(errs) > 0 {
  104. return nil, errs[0]
  105. } else {
  106. return rsp, nil
  107. }
  108. }
  109. //用户支付完成后,获取该用户的 UnionId,无需用户授权。
  110. // accessToken:接口调用凭据
  111. // openId:用户的OpenID
  112. // transactionId:微信支付订单号
  113. func GetPaidUnionId(accessToken, openId, transactionId string) (rsp *GetPaidUnionIdRsp, err error) {
  114. rsp = new(GetPaidUnionIdRsp)
  115. url := "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=" + accessToken + "&openid=" + openId + "&transaction_id=" + transactionId
  116. agent := HttpAgent()
  117. _, _, errs := agent.Get(url).EndStruct(rsp)
  118. if len(errs) > 0 {
  119. return nil, errs[0]
  120. } else {
  121. return rsp, nil
  122. }
  123. }