wx_ServiceApi.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package wechat
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/iGoogle-ink/gopay"
  6. "github.com/iGoogle-ink/gopay/wechat"
  7. )
  8. func Code2Session() {
  9. //获取微信用户的OpenId、SessionKey、UnionId
  10. // appId:APPID
  11. // appSecret:AppSecret
  12. // wxCode:小程序调用wx.login 获取的code
  13. userIdRsp, err := wechat.Code2Session("AppID", "APPSecret", "011EZg6p0VO47n1p2W4p0mle6p0EZg6u")
  14. if err != nil {
  15. fmt.Println("err:", err)
  16. return
  17. }
  18. fmt.Println("SessionKey:", userIdRsp.SessionKey)
  19. fmt.Println("OpenID:", userIdRsp.Openid)
  20. fmt.Println("UnionID:", userIdRsp.Unionid)
  21. fmt.Println("Errcode:", userIdRsp.Errcode)
  22. fmt.Println("Errmsg:", userIdRsp.Errmsg)
  23. }
  24. func GetAppWeChatLoginAccessToken() {
  25. accessToken, err := wechat.GetAppLoginAccessToken("AppID", "AppSecret", "code")
  26. if err != nil {
  27. fmt.Println("err:", err)
  28. return
  29. }
  30. fmt.Println("AccessToken:", accessToken.AccessToken)
  31. fmt.Println("Openid:", accessToken.Openid)
  32. fmt.Println("Unionid:", accessToken.Unionid)
  33. fmt.Println("Scope:", accessToken.Scope)
  34. fmt.Println("ExpiresIn:", accessToken.ExpiresIn)
  35. fmt.Println("Errcode:", accessToken.Errcode)
  36. fmt.Println("Errmsg:", accessToken.Errmsg)
  37. }
  38. func RefreshAppWeChatLoginAccessToken() {
  39. accessToken, err := wechat.RefreshAppLoginAccessToken("AppID", "refreshToken")
  40. if err != nil {
  41. fmt.Println("err:", err)
  42. return
  43. }
  44. fmt.Println("AccessToken:", accessToken.AccessToken)
  45. fmt.Println("Openid:", accessToken.Openid)
  46. fmt.Println("Scope:", accessToken.Scope)
  47. fmt.Println("ExpiresIn:", accessToken.ExpiresIn)
  48. fmt.Println("Errcode:", accessToken.Errcode)
  49. fmt.Println("Errmsg:", accessToken.Errmsg)
  50. }
  51. func GetWeChatAppletAccessToken() {
  52. //获取小程序全局唯一后台接口调用凭据(AccessToken:157字符)
  53. // appId:APPID
  54. // appSecret:AppSecret
  55. accessToken, err := wechat.GetAppletAccessToken("AppID", "AppSecret")
  56. if err != nil {
  57. fmt.Println("err:", err)
  58. return
  59. }
  60. fmt.Println("AccessToken:", accessToken.AccessToken)
  61. fmt.Println("ExpiresIn:", accessToken.ExpiresIn)
  62. fmt.Println("Errcode:", accessToken.Errcode)
  63. fmt.Println("Errmsg:", accessToken.Errmsg)
  64. }
  65. func GetWeChatAppletPaidUnionId() {
  66. accessToken := "21_3puo3mxoK6Ry2bR7Dh-qdn41wUP1wClwke8Zhf9a_i39jfWRq9rhNJZZZHaOt_Yad-Gp6u9_46dGW0RbIMz3nANInRI3m-1glvCnGW47v63sjYWV1iyTKOHGwDVxEv78kY-0OfkmkiIveVqAZCZaAAAQTQ"
  67. //用户支付完成后,获取该用户的 UnionId,无需用户授权。
  68. // accessToken:接口调用凭据
  69. // openId:用户的OpenID
  70. // transactionId:微信支付订单号
  71. rsp, err := wechat.GetAppletPaidUnionId(accessToken, "o0Df70MSI4Ygv2KQ2cLnoMN5CXI8", "4200000326201905256499385970")
  72. if err != nil {
  73. fmt.Println("err:", err)
  74. return
  75. }
  76. fmt.Println("Unionid:", rsp.Unionid)
  77. fmt.Println("Errcode:", rsp.Errcode)
  78. fmt.Println("Errmsg:", rsp.Errmsg)
  79. }
  80. func GetWeChatUserInfo() {
  81. accessToken := "21_3puo3mxoK6Ry2bR7Dh-qdn41wUP1wClwke8Zhf9a_i39jfWRq9rhNJZZZHaOt_Yad-Gp6u9_46dGW0RbIMz3nANInRI3m-1glvCnGW47v63sjYWV1iyTKOHGwDVxEv78kY-0OfkmkiIveVqAZCZaAAAQTQ"
  82. //获取用户基本信息(UnionID机制)
  83. // accessToken:接口调用凭据
  84. // openId:用户的OpenID
  85. // lang:默认为 zh_CN ,可选填 zh_CN 简体,zh_TW 繁体,en 英语
  86. userInfo, err := wechat.GetUserInfo(accessToken, "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")
  87. if err != nil {
  88. fmt.Println("err:", err)
  89. return
  90. }
  91. fmt.Println("userInfo:", *userInfo)
  92. }
  93. func DecryptWeChatOpenDataToStruct() {
  94. data := "Kf3TdPbzEmhWMuPKtlKxIWDkijhn402w1bxoHL4kLdcKr6jT1jNcIhvDJfjXmJcgDWLjmBiIGJ5acUuSvxLws3WgAkERmtTuiCG10CKLsJiR+AXVk7B2TUQzsq88YVilDz/YAN3647REE7glGmeBPfvUmdbfDzhL9BzvEiuRhABuCYyTMz4iaM8hFjbLB1caaeoOlykYAFMWC5pZi9P8uw=="
  95. iv := "Cds8j3VYoGvnTp1BrjXdJg=="
  96. session := "lyY4HPQbaOYzZdG+JcYK9w=="
  97. //微信小程序,手机号
  98. phone := new(wechat.UserPhone)
  99. //解密开放数据
  100. // encryptedData:包括敏感数据在内的完整用户信息的加密数据
  101. // iv:加密算法的初始向量
  102. // sessionKey:会话密钥
  103. // beanPtr:需要解析到的结构体指针
  104. err := wechat.DecryptOpenDataToStruct(data, iv, session, phone)
  105. if err != nil {
  106. fmt.Println("err:", err)
  107. return
  108. }
  109. fmt.Println("PhoneNumber:", phone.PhoneNumber)
  110. fmt.Println("PurePhoneNumber:", phone.PurePhoneNumber)
  111. fmt.Println("CountryCode:", phone.CountryCode)
  112. fmt.Println("Watermark:", phone.Watermark)
  113. sessionKey := "tiihtNczf5v6AKRyjwEUhQ=="
  114. encryptedData := "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew=="
  115. iv2 := "r7BXXKkLb8qrSNn05n0qiA=="
  116. //微信小程序 用户信息
  117. userInfo := new(wechat.AppletUserInfo)
  118. err = wechat.DecryptOpenDataToStruct(encryptedData, iv2, sessionKey, userInfo)
  119. if err != nil {
  120. fmt.Println("err:", err)
  121. return
  122. }
  123. fmt.Println("NickName:", userInfo.NickName)
  124. fmt.Println("AvatarUrl:", userInfo.AvatarUrl)
  125. fmt.Println("Country:", userInfo.Country)
  126. fmt.Println("Province:", userInfo.Province)
  127. fmt.Println("City:", userInfo.City)
  128. fmt.Println("Gender:", userInfo.Gender)
  129. fmt.Println("OpenId:", userInfo.OpenId)
  130. fmt.Println("UnionId:", userInfo.UnionId)
  131. fmt.Println("Watermark:", userInfo.Watermark)
  132. }
  133. func DecryptWeChatOpenDataToBodyMap() {
  134. data := "Kf3TdPbzEmhWMuPKtlKxIWDkijhn402w1bxoHL4kLdcKr6jT1jNcIhvDJfjXmJcgDWLjmBiIGJ5acUuSvxLws3WgAkERmtTuiCG10CKLsJiR+AXVk7B2TUQzsq88YVilDz/YAN3647REE7glGmeBPfvUmdbfDzhL9BzvEiuRhABuCYyTMz4iaM8hFjbLB1caaeoOlykYAFMWC5pZi9P8uw=="
  135. iv := "Cds8j3VYoGvnTp1BrjXdJg=="
  136. session := "lyY4HPQbaOYzZdG+JcYK9w=="
  137. //解密开放数据
  138. // encryptedData:包括敏感数据在内的完整用户信息的加密数据
  139. // iv:加密算法的初始向量
  140. // sessionKey:会话密钥
  141. bm, err := wechat.DecryptOpenDataToBodyMap(data, iv, session)
  142. if err != nil {
  143. fmt.Println("err:", err)
  144. return
  145. }
  146. fmt.Println("WeChatUserPhone:", bm)
  147. }
  148. func GetOpenIdByAuthCode() {
  149. //授权码查询openid
  150. // appId:APPID
  151. // mchId:商户号
  152. // apiKey:ApiKey
  153. // authCode:用户授权码
  154. // nonceStr:随即字符串
  155. openIdRsp, err := wechat.GetOpenIdByAuthCode("wxdaa2ab9ef87b5497", "1368139502", "GFDS8j98rewnmgl45wHTt980jg543abc", "135127679952609396", gopay.GetRandomString(32))
  156. if err != nil {
  157. fmt.Println("err:", err)
  158. return
  159. }
  160. fmt.Println("ReturnCode:", openIdRsp.ReturnCode)
  161. fmt.Println("ReturnMsg:", openIdRsp.ReturnMsg)
  162. fmt.Println("ResultCode:", openIdRsp.ResultCode)
  163. fmt.Println("Appid:", openIdRsp.Appid)
  164. fmt.Println("MchId:", openIdRsp.MchId)
  165. fmt.Println("NonceStr:", openIdRsp.NonceStr)
  166. fmt.Println("err_code:", openIdRsp.ErrCode)
  167. fmt.Println("Openid:", openIdRsp.Openid)
  168. }
  169. //解析notify参数、验签、返回数据到微信
  170. func ParseWeChatNotifyAndVerifyWeChatSign(req *http.Request) string {
  171. rsp := new(wechat.NotifyResponse)
  172. //解析参数
  173. notifyReq, err := wechat.ParseNotify(req)
  174. if err != nil {
  175. fmt.Println("err:", err)
  176. }
  177. fmt.Println("notifyReq:", *notifyReq)
  178. //验签
  179. ok, err := wechat.VerifySign("GFDS8j98rewnmgl45wHTt980jg543abc", wechat.SignType_MD5, notifyReq)
  180. if err != nil {
  181. fmt.Println("err:", err)
  182. }
  183. fmt.Println("微信验签是否通过:", ok)
  184. //或者
  185. bodyMap, err := wechat.ParseNotifyToBodyMap(req)
  186. if err != nil {
  187. fmt.Println("err:", err)
  188. }
  189. fmt.Println("bodyMap:", bodyMap)
  190. ok, err = wechat.VerifySign("GFDS8j98rewnmgl45wHTt980jg543abc", wechat.SignType_MD5, bodyMap)
  191. if err != nil {
  192. fmt.Println("err:", err)
  193. }
  194. fmt.Println("微信验签是否通过:", ok)
  195. rsp.ReturnCode = gopay.SUCCESS
  196. rsp.ReturnMsg = "OK"
  197. return rsp.ToXmlString()
  198. }
  199. // 解析微信退款异步通知的参数,解析出来的 req_info 是加密数据,需解密
  200. func ParseWeChatRefundNotify(req *http.Request) string {
  201. rsp := new(wechat.NotifyResponse)
  202. // 解析参数
  203. notifyReq, err := wechat.ParseRefundNotify(req)
  204. if err != nil {
  205. fmt.Println("err:", err)
  206. }
  207. fmt.Println("notifyReq:", *notifyReq)
  208. // 退款通知无sign,不用验签
  209. // 解密退款异步通知的加密数据
  210. refundNotify, err := wechat.DecryptRefundNotifyReqInfo(notifyReq.ReqInfo, "GFDS8j98rewnmgl45wHTt980jg543abc")
  211. if err != nil {
  212. fmt.Println("err:", err)
  213. }
  214. fmt.Println("refundNotify:", *refundNotify)
  215. // 返回微信
  216. rsp.ReturnCode = gopay.SUCCESS
  217. rsp.ReturnMsg = "OK"
  218. return rsp.ToXmlString()
  219. }