wechat_client.go 9.8 KB

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