wechat_client.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package gopay
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "encoding/xml"
  6. "github.com/parnurzeal/gorequest"
  7. "io/ioutil"
  8. )
  9. type weChatClient struct {
  10. AppId string
  11. MchId string
  12. secretKey string
  13. isProd bool
  14. }
  15. //初始化微信客户端 ok
  16. // appId:应用ID
  17. // mchID:商户ID
  18. // secretKey:Key值
  19. // isProd:是否是正式环境
  20. func NewWeChatClient(appId, mchId, secretKey string, isProd bool) *weChatClient {
  21. client := new(weChatClient)
  22. client.AppId = appId
  23. client.MchId = mchId
  24. client.secretKey = secretKey
  25. client.isProd = isProd
  26. return client
  27. }
  28. //提交付款码支付 ok
  29. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
  30. func (this *weChatClient) Micropay(body BodyMap) (wxRsp *WeChatMicropayResponse, err error) {
  31. var bytes []byte
  32. if this.isProd {
  33. //正式环境
  34. bytes, err = this.doWeChat(body, wxURL_Micropay)
  35. if err != nil {
  36. return nil, err
  37. }
  38. } else {
  39. bytes, err = this.doWeChat(body, wxURL_SanBox_Micropay)
  40. if err != nil {
  41. return nil, err
  42. }
  43. }
  44. wxRsp = new(WeChatMicropayResponse)
  45. err = xml.Unmarshal(bytes, wxRsp)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return wxRsp, nil
  50. }
  51. //统一下单 ok
  52. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
  53. func (this *weChatClient) UnifiedOrder(body BodyMap) (wxRsp *WeChatUnifiedOrderResponse, err error) {
  54. var bytes []byte
  55. if this.isProd {
  56. //正式环境
  57. bytes, err = this.doWeChat(body, wxURL_UnifiedOrder)
  58. if err != nil {
  59. return nil, err
  60. }
  61. } else {
  62. body.Set("total_fee", 101)
  63. bytes, err = this.doWeChat(body, wxURL_SanBox_UnifiedOrder)
  64. if err != nil {
  65. return nil, err
  66. }
  67. }
  68. wxRsp = new(WeChatUnifiedOrderResponse)
  69. err = xml.Unmarshal(bytes, wxRsp)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return wxRsp, nil
  74. }
  75. //查询订单 ok
  76. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
  77. func (this *weChatClient) QueryOrder(body BodyMap) (wxRsp *WeChatQueryOrderResponse, err error) {
  78. var bytes []byte
  79. if this.isProd {
  80. //正式环境
  81. bytes, err = this.doWeChat(body, wxURL_OrderQuery)
  82. if err != nil {
  83. return nil, err
  84. }
  85. } else {
  86. bytes, err = this.doWeChat(body, wxURL_SanBox_OrderQuery)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. wxRsp = new(WeChatQueryOrderResponse)
  92. err = xml.Unmarshal(bytes, wxRsp)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return wxRsp, nil
  97. }
  98. //关闭订单 ok
  99. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3
  100. func (this *weChatClient) CloseOrder(body BodyMap) (wxRsp *WeChatCloseOrderResponse, err error) {
  101. var bytes []byte
  102. if this.isProd {
  103. //正式环境
  104. bytes, err = this.doWeChat(body, wxURL_CloseOrder)
  105. if err != nil {
  106. return nil, err
  107. }
  108. } else {
  109. bytes, err = this.doWeChat(body, wxURL_SanBox_CloseOrder)
  110. if err != nil {
  111. return nil, err
  112. }
  113. }
  114. wxRsp = new(WeChatCloseOrderResponse)
  115. err = xml.Unmarshal(bytes, wxRsp)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return wxRsp, nil
  120. }
  121. //撤销订单 ok
  122. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
  123. func (this *weChatClient) Reverse(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp *WeChatReverseResponse, err error) {
  124. var bytes []byte
  125. if this.isProd {
  126. //正式环境
  127. pkcsPool := x509.NewCertPool()
  128. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  129. if err != nil {
  130. return nil, err
  131. }
  132. pkcsPool.AppendCertsFromPEM(pkcs)
  133. certificate, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
  134. if err != nil {
  135. return nil, err
  136. }
  137. tlsConfig := new(tls.Config)
  138. tlsConfig.Certificates = []tls.Certificate{certificate}
  139. tlsConfig.RootCAs = pkcsPool
  140. tlsConfig.InsecureSkipVerify = true
  141. bytes, err = this.doWeChat(body, wxURL_Reverse, tlsConfig)
  142. if err != nil {
  143. return nil, err
  144. }
  145. } else {
  146. bytes, err = this.doWeChat(body, wxURL_SanBox_Reverse)
  147. if err != nil {
  148. return nil, err
  149. }
  150. }
  151. wxRsp = new(WeChatReverseResponse)
  152. err = xml.Unmarshal(bytes, wxRsp)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return wxRsp, nil
  157. }
  158. //申请退款 ok
  159. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
  160. func (this *weChatClient) Refund(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp *WeChatRefundResponse, err error) {
  161. var bytes []byte
  162. if this.isProd {
  163. //正式环境
  164. pkcsPool := x509.NewCertPool()
  165. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  166. if err != nil {
  167. return nil, err
  168. }
  169. pkcsPool.AppendCertsFromPEM(pkcs)
  170. certificate, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
  171. if err != nil {
  172. return nil, err
  173. }
  174. tlsConfig := new(tls.Config)
  175. tlsConfig.Certificates = []tls.Certificate{certificate}
  176. tlsConfig.RootCAs = pkcsPool
  177. tlsConfig.InsecureSkipVerify = true
  178. bytes, err = this.doWeChat(body, wxURL_Refund, tlsConfig)
  179. if err != nil {
  180. return nil, err
  181. }
  182. } else {
  183. bytes, err = this.doWeChat(body, wxURL_SanBox_Refund)
  184. if err != nil {
  185. return nil, err
  186. }
  187. }
  188. wxRsp = new(WeChatRefundResponse)
  189. err = xml.Unmarshal(bytes, wxRsp)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return wxRsp, nil
  194. }
  195. //查询退款 ok
  196. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5
  197. func (this *weChatClient) QueryRefund(body BodyMap) (wxRsp *WeChatQueryRefundResponse, err error) {
  198. var bytes []byte
  199. if this.isProd {
  200. //正式环境
  201. bytes, err = this.doWeChat(body, wxURL_RefundQuery)
  202. if err != nil {
  203. return nil, err
  204. }
  205. } else {
  206. bytes, err = this.doWeChat(body, wxURL_SanBox_RefundQuery)
  207. if err != nil {
  208. return nil, err
  209. }
  210. }
  211. wxRsp = new(WeChatQueryRefundResponse)
  212. err = xml.Unmarshal(bytes, wxRsp)
  213. if err != nil {
  214. return nil, err
  215. }
  216. return wxRsp, nil
  217. }
  218. //下载对账单 ok
  219. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6
  220. func (this *weChatClient) DownloadBill(body BodyMap) (wxRsp string, err error) {
  221. var bytes []byte
  222. if this.isProd {
  223. //正式环境
  224. bytes, err = this.doWeChat(body, wxURL_DownloadBill)
  225. } else {
  226. bytes, err = this.doWeChat(body, wxURL_SanBox_DownloadBill)
  227. }
  228. wxRsp = string(bytes)
  229. if err != nil {
  230. return wxRsp, err
  231. }
  232. return wxRsp, nil
  233. }
  234. //下载资金账单 ok
  235. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18&index=7
  236. // 好像不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
  237. func (this *weChatClient) DownloadFundFlow(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp string, err error) {
  238. var bytes []byte
  239. if this.isProd {
  240. //正式环境
  241. pkcsPool := x509.NewCertPool()
  242. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  243. if err != nil {
  244. return "", err
  245. }
  246. pkcsPool.AppendCertsFromPEM(pkcs)
  247. certificate, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
  248. if err != nil {
  249. return "", err
  250. }
  251. tlsConfig := new(tls.Config)
  252. tlsConfig.Certificates = []tls.Certificate{certificate}
  253. tlsConfig.RootCAs = pkcsPool
  254. tlsConfig.InsecureSkipVerify = true
  255. bytes, err = this.doWeChat(body, wxURL_DownloadFundFlow, tlsConfig)
  256. } else {
  257. bytes, err = this.doWeChat(body, wxURL_SanBox_DownloadFundFlow)
  258. }
  259. if err != nil {
  260. return "", err
  261. }
  262. wxRsp = string(bytes)
  263. return wxRsp, nil
  264. }
  265. //拉取订单评价数据
  266. // 文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_17&index=11
  267. // 好像不支持沙箱环境,因为沙箱环境默认需要用MD5签名,但是此接口仅支持HMAC-SHA256签名
  268. func (this *weChatClient) BatchQueryComment(body BodyMap, certFilePath, keyFilePath, pkcs12FilePath string) (wxRsp string, err error) {
  269. var bytes []byte
  270. if this.isProd {
  271. //正式环境
  272. body.Set("sign_type", SignType_HMAC_SHA256)
  273. pkcsPool := x509.NewCertPool()
  274. pkcs, err := ioutil.ReadFile(pkcs12FilePath)
  275. if err != nil {
  276. return "", err
  277. }
  278. pkcsPool.AppendCertsFromPEM(pkcs)
  279. certificate, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
  280. if err != nil {
  281. return "", err
  282. }
  283. tlsConfig := new(tls.Config)
  284. tlsConfig.Certificates = []tls.Certificate{certificate}
  285. tlsConfig.RootCAs = pkcsPool
  286. tlsConfig.InsecureSkipVerify = true
  287. bytes, err = this.doWeChat(body, wxURL_BatchQueryComment, tlsConfig)
  288. } else {
  289. bytes, err = this.doWeChat(body, wxURL_SanBox_BatchQueryComment)
  290. }
  291. if err != nil {
  292. return "", err
  293. }
  294. wxRsp = string(bytes)
  295. return wxRsp, nil
  296. }
  297. //向微信发送请求 ok
  298. func (this *weChatClient) doWeChat(body BodyMap, url string, tlsConfig ...*tls.Config) (bytes []byte, err error) {
  299. var sign string
  300. body.Set("appid", this.AppId)
  301. body.Set("mch_id", this.MchId)
  302. //===============生成参数===================
  303. if !this.isProd {
  304. //沙箱环境
  305. body.Set("sign_type", SignType_MD5)
  306. //从微信接口获取SanBoxSignKey
  307. key, err := getSanBoxSign(this.MchId, body.Get("nonce_str"), this.secretKey, body.Get("sign_type"))
  308. if err != nil {
  309. return nil, err
  310. }
  311. sign = getLocalSign(key, body.Get("sign_type"), body)
  312. } else {
  313. //正式环境
  314. //本地计算Sign
  315. sign = getLocalSign(this.secretKey, body.Get("sign_type"), body)
  316. }
  317. body.Set("sign", sign)
  318. reqXML := generateXml(body)
  319. //===============发起请求===================
  320. agent := gorequest.New()
  321. if this.isProd && tlsConfig != nil {
  322. agent.TLSClientConfig(tlsConfig[0])
  323. }
  324. agent.Post(url)
  325. agent.Type("xml")
  326. agent.SendString(reqXML)
  327. _, bytes, errs := agent.EndBytes()
  328. if len(errs) > 0 {
  329. return nil, errs[0]
  330. }
  331. return bytes, nil
  332. }