WXPay.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. package com.izouma.weixin.wxpay;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class WXPay {
  5. private WXPayConfig config;
  6. private WXPayConstants.SignType signType;
  7. private boolean autoReport;
  8. private boolean useSandbox;
  9. private String notifyUrl;
  10. private WXPayRequest wxPayRequest;
  11. public WXPay(final WXPayConfig config) throws Exception {
  12. this(config, null, true, false);
  13. }
  14. // 新增的构造方法
  15. public WXPay(final WXPayConfig config, WXPayConstants.SignType signType) throws Exception {
  16. this(config, null, true, false, signType);
  17. }
  18. // 新增的构造方法
  19. public WXPay(final WXPayConfig config, final String notifyUrl,
  20. final boolean autoReport, final boolean useSandbox,
  21. WXPayConstants.SignType signType) throws Exception {
  22. this.config = config;
  23. this.notifyUrl = notifyUrl;
  24. this.autoReport = autoReport;
  25. this.useSandbox = useSandbox;
  26. this.signType = signType;
  27. this.wxPayRequest = new WXPayRequest(config);
  28. }
  29. public WXPay(final WXPayConfig config, final boolean autoReport) throws Exception {
  30. this(config, null, autoReport, false);
  31. }
  32. public WXPay(final WXPayConfig config, final boolean autoReport, final boolean useSandbox) throws Exception {
  33. this(config, null, autoReport, useSandbox);
  34. }
  35. public WXPay(final WXPayConfig config, final String notifyUrl) throws Exception {
  36. this(config, notifyUrl, true, false);
  37. }
  38. public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport) throws Exception {
  39. this(config, notifyUrl, autoReport, false);
  40. }
  41. public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
  42. this.config = config;
  43. this.notifyUrl = notifyUrl;
  44. this.autoReport = autoReport;
  45. this.useSandbox = useSandbox;
  46. if (useSandbox) {
  47. this.signType = WXPayConstants.SignType.MD5; // 沙箱环境
  48. } else {
  49. this.signType = WXPayConstants.SignType.HMACSHA256;
  50. }
  51. this.wxPayRequest = new WXPayRequest(config);
  52. }
  53. private void checkWXPayConfig() throws Exception {
  54. if (this.config == null) {
  55. throw new Exception("config is null");
  56. }
  57. if (this.config.getAppID() == null || this.config.getAppID().trim().length() == 0) {
  58. throw new Exception("appid in config is empty");
  59. }
  60. if (this.config.getMchID() == null || this.config.getMchID().trim().length() == 0) {
  61. throw new Exception("appid in config is empty");
  62. }
  63. if (this.config.getCertStream() == null) {
  64. throw new Exception("cert stream in config is empty");
  65. }
  66. if (this.config.getWXPayDomain() == null) {
  67. throw new Exception("config.getWXPayDomain() is null");
  68. }
  69. if (this.config.getHttpConnectTimeoutMs() < 10) {
  70. throw new Exception("http connect timeout is too small");
  71. }
  72. if (this.config.getHttpReadTimeoutMs() < 10) {
  73. throw new Exception("http read timeout is too small");
  74. }
  75. }
  76. /**
  77. * 向 Map 中添加 appid、mch_id、nonce_str、sign_type、sign <br>
  78. * 该函数适用于商户适用于统一下单等接口,不适用于红包、代金券接口
  79. *
  80. * @param reqData
  81. * @return
  82. * @throws Exception
  83. */
  84. public Map<String, String> fillRequestData(Map<String, String> reqData) throws Exception {
  85. reqData.put("appid", config.getAppID());
  86. reqData.put("mch_id", config.getMchID());
  87. reqData.put("nonce_str", WXPayUtil.generateNonceStr());
  88. if (WXPayConstants.SignType.MD5.equals(this.signType)) {
  89. reqData.put("sign_type", WXPayConstants.MD5);
  90. } else if (WXPayConstants.SignType.HMACSHA256.equals(this.signType)) {
  91. reqData.put("sign_type", WXPayConstants.HMACSHA256);
  92. }
  93. reqData.put("sign", WXPayUtil.generateSignature(reqData, config.getKey(), this.signType));
  94. return reqData;
  95. }
  96. /**
  97. * 判断xml数据的sign是否有效,必须包含sign字段,否则返回false。
  98. *
  99. * @param reqData 向wxpay post的请求数据
  100. * @return 签名是否有效
  101. * @throws Exception
  102. */
  103. public boolean isResponseSignatureValid(Map<String, String> reqData) throws Exception {
  104. // 返回数据的签名方式和请求中给定的签名方式是一致的
  105. return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), this.signType);
  106. }
  107. /**
  108. * 判断支付结果通知中的sign是否有效
  109. *
  110. * @param reqData 向wxpay post的请求数据
  111. * @return 签名是否有效
  112. * @throws Exception
  113. */
  114. public boolean isPayResultNotifySignatureValid(Map<String, String> reqData) throws Exception {
  115. String signTypeInData = reqData.get(WXPayConstants.FIELD_SIGN_TYPE);
  116. WXPayConstants.SignType signType;
  117. if (signTypeInData == null) {
  118. //signType = WXPayConstants.SignType.MD5;
  119. signType = this.signType;//修改为默认类型
  120. } else {
  121. signTypeInData = signTypeInData.trim();
  122. if (signTypeInData.length() == 0) {
  123. signType = WXPayConstants.SignType.MD5;
  124. } else if (WXPayConstants.MD5.equals(signTypeInData)) {
  125. signType = WXPayConstants.SignType.MD5;
  126. } else if (WXPayConstants.HMACSHA256.equals(signTypeInData)) {
  127. signType = WXPayConstants.SignType.HMACSHA256;
  128. } else {
  129. throw new Exception(String.format("Unsupported sign_type: %s", signTypeInData));
  130. }
  131. }
  132. return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), signType);
  133. }
  134. /**
  135. * 不需要证书的请求
  136. *
  137. * @param urlSuffix String
  138. * @param reqData 向wxpay post的请求数据
  139. * @param connectTimeoutMs 超时时间,单位是毫秒
  140. * @param readTimeoutMs 超时时间,单位是毫秒
  141. * @return API返回数据
  142. * @throws Exception
  143. */
  144. public String requestWithoutCert(String urlSuffix, Map<String, String> reqData,
  145. int connectTimeoutMs, int readTimeoutMs) throws Exception {
  146. String msgUUID = reqData.get("nonce_str");
  147. String reqBody = WXPayUtil.mapToXml(reqData);
  148. String resp = this.wxPayRequest.requestWithoutCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, autoReport);
  149. return resp;
  150. }
  151. /**
  152. * 需要证书的请求
  153. *
  154. * @param urlSuffix String
  155. * @param reqData 向wxpay post的请求数据 Map
  156. * @param connectTimeoutMs 超时时间,单位是毫秒
  157. * @param readTimeoutMs 超时时间,单位是毫秒
  158. * @return API返回数据
  159. * @throws Exception
  160. */
  161. public String requestWithCert(String urlSuffix, Map<String, String> reqData,
  162. int connectTimeoutMs, int readTimeoutMs) throws Exception {
  163. String msgUUID = reqData.get("nonce_str");
  164. String reqBody = WXPayUtil.mapToXml(reqData);
  165. String resp = this.wxPayRequest.requestWithCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, this.autoReport);
  166. return resp;
  167. }
  168. /**
  169. * 处理 HTTPS API返回数据,转换成Map对象。return_code为SUCCESS时,验证签名。
  170. *
  171. * @param xmlStr API返回的XML格式数据
  172. * @return Map类型数据
  173. * @throws Exception
  174. */
  175. public Map<String, String> processResponseXml(String xmlStr) throws Exception {
  176. String RETURN_CODE = "return_code";
  177. String return_code;
  178. Map<String, String> respData = WXPayUtil.xmlToMap(xmlStr);
  179. if (respData.containsKey(RETURN_CODE)) {
  180. return_code = respData.get(RETURN_CODE);
  181. } else {
  182. throw new Exception(String.format("No `return_code` in XML: %s", xmlStr));
  183. }
  184. if (return_code.equals(WXPayConstants.FAIL)) {
  185. return respData;
  186. } else if (return_code.equals(WXPayConstants.SUCCESS)) {
  187. if (this.isResponseSignatureValid(respData)) {
  188. return respData;
  189. } else {
  190. throw new Exception(String.format("Invalid sign value in XML: %s", xmlStr));
  191. }
  192. } else {
  193. throw new Exception(String.format("return_code value %s is invalid in XML: %s", return_code, xmlStr));
  194. }
  195. }
  196. /**
  197. * 作用:提交刷卡支付<br>
  198. * 场景:刷卡支付
  199. *
  200. * @param reqData 向wxpay post的请求数据
  201. * @return API返回数据
  202. * @throws Exception
  203. */
  204. public Map<String, String> microPay(Map<String, String> reqData) throws Exception {
  205. return this.microPay(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  206. }
  207. /**
  208. * 作用:提交刷卡支付<br>
  209. * 场景:刷卡支付
  210. *
  211. * @param reqData 向wxpay post的请求数据
  212. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  213. * @param readTimeoutMs 读超时时间,单位是毫秒
  214. * @return API返回数据
  215. * @throws Exception
  216. */
  217. public Map<String, String> microPay(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  218. String url;
  219. if (this.useSandbox) {
  220. url = WXPayConstants.SANDBOX_MICROPAY_URL_SUFFIX;
  221. } else {
  222. url = WXPayConstants.MICROPAY_URL_SUFFIX;
  223. }
  224. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  225. return this.processResponseXml(respXml);
  226. }
  227. /**
  228. * 提交刷卡支付,针对软POS,尽可能做成功
  229. * 内置重试机制,最多60s
  230. *
  231. * @param reqData
  232. * @return
  233. * @throws Exception
  234. */
  235. public Map<String, String> microPayWithPos(Map<String, String> reqData) throws Exception {
  236. return this.microPayWithPos(reqData, this.config.getHttpConnectTimeoutMs());
  237. }
  238. /**
  239. * 提交刷卡支付,针对软POS,尽可能做成功
  240. * 内置重试机制,最多60s
  241. *
  242. * @param reqData
  243. * @param connectTimeoutMs
  244. * @return
  245. * @throws Exception
  246. */
  247. public Map<String, String> microPayWithPos(Map<String, String> reqData, int connectTimeoutMs) throws Exception {
  248. int remainingTimeMs = 60 * 1000;
  249. long startTimestampMs = 0;
  250. Map<String, String> lastResult = null;
  251. Exception lastException = null;
  252. while (true) {
  253. startTimestampMs = WXPayUtil.getCurrentTimestampMs();
  254. int readTimeoutMs = remainingTimeMs - connectTimeoutMs;
  255. if (readTimeoutMs > 1000) {
  256. try {
  257. lastResult = this.microPay(reqData, connectTimeoutMs, readTimeoutMs);
  258. String returnCode = lastResult.get("return_code");
  259. if (returnCode.equals("SUCCESS")) {
  260. String resultCode = lastResult.get("result_code");
  261. String errCode = lastResult.get("err_code");
  262. if (resultCode.equals("SUCCESS")) {
  263. break;
  264. } else {
  265. // 看错误码,若支付结果未知,则重试提交刷卡支付
  266. if (errCode.equals("SYSTEMERROR") || errCode.equals("BANKERROR") || errCode.equals("USERPAYING")) {
  267. remainingTimeMs = remainingTimeMs - (int) (WXPayUtil.getCurrentTimestampMs() - startTimestampMs);
  268. if (remainingTimeMs <= 100) {
  269. break;
  270. } else {
  271. WXPayUtil.getLogger().info("microPayWithPos: try micropay again");
  272. if (remainingTimeMs > 5 * 1000) {
  273. Thread.sleep(5 * 1000);
  274. } else {
  275. Thread.sleep(1 * 1000);
  276. }
  277. continue;
  278. }
  279. } else {
  280. break;
  281. }
  282. }
  283. } else {
  284. break;
  285. }
  286. } catch (Exception ex) {
  287. lastResult = null;
  288. lastException = ex;
  289. }
  290. } else {
  291. break;
  292. }
  293. }
  294. if (lastResult == null) {
  295. throw lastException;
  296. } else {
  297. return lastResult;
  298. }
  299. }
  300. /**
  301. * 作用:统一下单<br>
  302. * 场景:公共号支付、扫码支付、APP支付
  303. *
  304. * @param reqData 向wxpay post的请求数据
  305. * @return API返回数据
  306. * @throws Exception
  307. */
  308. public Map<String, String> unifiedOrder(Map<String, String> reqData) throws Exception {
  309. return this.unifiedOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  310. }
  311. /**
  312. * 作用:统一下单<br>
  313. * 场景:公共号支付、扫码支付、APP支付
  314. *
  315. * @param reqData 向wxpay post的请求数据
  316. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  317. * @param readTimeoutMs 读超时时间,单位是毫秒
  318. * @return API返回数据
  319. * @throws Exception
  320. */
  321. public Map<String, String> unifiedOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  322. String url;
  323. if (this.useSandbox) {
  324. url = WXPayConstants.SANDBOX_UNIFIEDORDER_URL_SUFFIX;
  325. } else {
  326. url = WXPayConstants.UNIFIEDORDER_URL_SUFFIX;
  327. }
  328. if (this.notifyUrl != null) {
  329. reqData.put("notify_url", this.notifyUrl);
  330. }
  331. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  332. return this.processResponseXml(respXml);
  333. }
  334. /**
  335. * 作用:查询订单<br>
  336. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  337. *
  338. * @param reqData 向wxpay post的请求数据
  339. * @return API返回数据
  340. * @throws Exception
  341. */
  342. public Map<String, String> orderQuery(Map<String, String> reqData) throws Exception {
  343. return this.orderQuery(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  344. }
  345. /**
  346. * 作用:查询订单<br>
  347. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  348. *
  349. * @param reqData 向wxpay post的请求数据 int
  350. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  351. * @param readTimeoutMs 读超时时间,单位是毫秒
  352. * @return API返回数据
  353. * @throws Exception
  354. */
  355. public Map<String, String> orderQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  356. String url;
  357. if (this.useSandbox) {
  358. url = WXPayConstants.SANDBOX_ORDERQUERY_URL_SUFFIX;
  359. } else {
  360. url = WXPayConstants.ORDERQUERY_URL_SUFFIX;
  361. }
  362. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  363. return this.processResponseXml(respXml);
  364. }
  365. /**
  366. * 作用:撤销订单<br>
  367. * 场景:刷卡支付
  368. *
  369. * @param reqData 向wxpay post的请求数据
  370. * @return API返回数据
  371. * @throws Exception
  372. */
  373. public Map<String, String> reverse(Map<String, String> reqData) throws Exception {
  374. return this.reverse(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  375. }
  376. /**
  377. * 作用:撤销订单<br>
  378. * 场景:刷卡支付<br>
  379. * 其他:需要证书
  380. *
  381. * @param reqData 向wxpay post的请求数据
  382. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  383. * @param readTimeoutMs 读超时时间,单位是毫秒
  384. * @return API返回数据
  385. * @throws Exception
  386. */
  387. public Map<String, String> reverse(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  388. String url;
  389. if (this.useSandbox) {
  390. url = WXPayConstants.SANDBOX_REVERSE_URL_SUFFIX;
  391. } else {
  392. url = WXPayConstants.REVERSE_URL_SUFFIX;
  393. }
  394. String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  395. return this.processResponseXml(respXml);
  396. }
  397. /**
  398. * 作用:关闭订单<br>
  399. * 场景:公共号支付、扫码支付、APP支付
  400. *
  401. * @param reqData 向wxpay post的请求数据
  402. * @return API返回数据
  403. * @throws Exception
  404. */
  405. public Map<String, String> closeOrder(Map<String, String> reqData) throws Exception {
  406. return this.closeOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  407. }
  408. /**
  409. * 作用:关闭订单<br>
  410. * 场景:公共号支付、扫码支付、APP支付
  411. *
  412. * @param reqData 向wxpay post的请求数据
  413. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  414. * @param readTimeoutMs 读超时时间,单位是毫秒
  415. * @return API返回数据
  416. * @throws Exception
  417. */
  418. public Map<String, String> closeOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  419. String url;
  420. if (this.useSandbox) {
  421. url = WXPayConstants.SANDBOX_CLOSEORDER_URL_SUFFIX;
  422. } else {
  423. url = WXPayConstants.CLOSEORDER_URL_SUFFIX;
  424. }
  425. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  426. return this.processResponseXml(respXml);
  427. }
  428. /**
  429. * 作用:申请退款<br>
  430. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  431. *
  432. * @param reqData 向wxpay post的请求数据
  433. * @return API返回数据
  434. * @throws Exception
  435. */
  436. public Map<String, String> refund(Map<String, String> reqData) throws Exception {
  437. return this.refund(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  438. }
  439. /**
  440. * 作用:申请退款<br>
  441. * 场景:刷卡支付、公共号支付、扫码支付、APP支付<br>
  442. * 其他:需要证书
  443. *
  444. * @param reqData 向wxpay post的请求数据
  445. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  446. * @param readTimeoutMs 读超时时间,单位是毫秒
  447. * @return API返回数据
  448. * @throws Exception
  449. */
  450. public Map<String, String> refund(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  451. String url;
  452. if (this.useSandbox) {
  453. url = WXPayConstants.SANDBOX_REFUND_URL_SUFFIX;
  454. } else {
  455. url = WXPayConstants.REFUND_URL_SUFFIX;
  456. }
  457. String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  458. return this.processResponseXml(respXml);
  459. }
  460. /**
  461. * 作用:退款查询<br>
  462. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  463. *
  464. * @param reqData 向wxpay post的请求数据
  465. * @return API返回数据
  466. * @throws Exception
  467. */
  468. public Map<String, String> refundQuery(Map<String, String> reqData) throws Exception {
  469. return this.refundQuery(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  470. }
  471. /**
  472. * 作用:退款查询<br>
  473. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  474. *
  475. * @param reqData 向wxpay post的请求数据
  476. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  477. * @param readTimeoutMs 读超时时间,单位是毫秒
  478. * @return API返回数据
  479. * @throws Exception
  480. */
  481. public Map<String, String> refundQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  482. String url;
  483. if (this.useSandbox) {
  484. url = WXPayConstants.SANDBOX_REFUNDQUERY_URL_SUFFIX;
  485. } else {
  486. url = WXPayConstants.REFUNDQUERY_URL_SUFFIX;
  487. }
  488. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  489. return this.processResponseXml(respXml);
  490. }
  491. /**
  492. * 作用:对账单下载(成功时返回对账单数据,失败时返回XML格式数据)<br>
  493. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  494. *
  495. * @param reqData 向wxpay post的请求数据
  496. * @return API返回数据
  497. * @throws Exception
  498. */
  499. public Map<String, String> downloadBill(Map<String, String> reqData) throws Exception {
  500. return this.downloadBill(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  501. }
  502. /**
  503. * 作用:对账单下载<br>
  504. * 场景:刷卡支付、公共号支付、扫码支付、APP支付<br>
  505. * 其他:无论是否成功都返回Map。若成功,返回的Map中含有return_code、return_msg、data,
  506. * 其中return_code为`SUCCESS`,data为对账单数据。
  507. *
  508. * @param reqData 向wxpay post的请求数据
  509. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  510. * @param readTimeoutMs 读超时时间,单位是毫秒
  511. * @return 经过封装的API返回数据
  512. * @throws Exception
  513. */
  514. public Map<String, String> downloadBill(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  515. String url;
  516. if (this.useSandbox) {
  517. url = WXPayConstants.SANDBOX_DOWNLOADBILL_URL_SUFFIX;
  518. } else {
  519. url = WXPayConstants.DOWNLOADBILL_URL_SUFFIX;
  520. }
  521. String respStr = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs).trim();
  522. Map<String, String> ret;
  523. // 出现错误,返回XML数据
  524. if (respStr.indexOf("<") == 0) {
  525. ret = WXPayUtil.xmlToMap(respStr);
  526. } else {
  527. // 正常返回csv数据
  528. ret = new HashMap<String, String>();
  529. ret.put("return_code", WXPayConstants.SUCCESS);
  530. ret.put("return_msg", "ok");
  531. ret.put("data", respStr);
  532. }
  533. return ret;
  534. }
  535. /**
  536. * 作用:交易保障<br>
  537. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  538. *
  539. * @param reqData 向wxpay post的请求数据
  540. * @return API返回数据
  541. * @throws Exception
  542. */
  543. public Map<String, String> report(Map<String, String> reqData) throws Exception {
  544. return this.report(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  545. }
  546. /**
  547. * 作用:交易保障<br>
  548. * 场景:刷卡支付、公共号支付、扫码支付、APP支付
  549. *
  550. * @param reqData 向wxpay post的请求数据
  551. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  552. * @param readTimeoutMs 读超时时间,单位是毫秒
  553. * @return API返回数据
  554. * @throws Exception
  555. */
  556. public Map<String, String> report(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  557. String url;
  558. if (this.useSandbox) {
  559. url = WXPayConstants.SANDBOX_REPORT_URL_SUFFIX;
  560. } else {
  561. url = WXPayConstants.REPORT_URL_SUFFIX;
  562. }
  563. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  564. return WXPayUtil.xmlToMap(respXml);
  565. }
  566. /**
  567. * 作用:转换短链接<br>
  568. * 场景:刷卡支付、扫码支付
  569. *
  570. * @param reqData 向wxpay post的请求数据
  571. * @return API返回数据
  572. * @throws Exception
  573. */
  574. public Map<String, String> shortUrl(Map<String, String> reqData) throws Exception {
  575. return this.shortUrl(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  576. }
  577. /**
  578. * 作用:转换短链接<br>
  579. * 场景:刷卡支付、扫码支付
  580. *
  581. * @param reqData 向wxpay post的请求数据
  582. * @return API返回数据
  583. * @throws Exception
  584. */
  585. public Map<String, String> shortUrl(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  586. String url;
  587. if (this.useSandbox) {
  588. url = WXPayConstants.SANDBOX_SHORTURL_URL_SUFFIX;
  589. } else {
  590. url = WXPayConstants.SHORTURL_URL_SUFFIX;
  591. }
  592. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  593. return this.processResponseXml(respXml);
  594. }
  595. /**
  596. * 作用:授权码查询OPENID接口<br>
  597. * 场景:刷卡支付
  598. *
  599. * @param reqData 向wxpay post的请求数据
  600. * @return API返回数据
  601. * @throws Exception
  602. */
  603. public Map<String, String> authCodeToOpenid(Map<String, String> reqData) throws Exception {
  604. return this.authCodeToOpenid(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
  605. }
  606. /**
  607. * 作用:授权码查询OPENID接口<br>
  608. * 场景:刷卡支付
  609. *
  610. * @param reqData 向wxpay post的请求数据
  611. * @param connectTimeoutMs 连接超时时间,单位是毫秒
  612. * @param readTimeoutMs 读超时时间,单位是毫秒
  613. * @return API返回数据
  614. * @throws Exception
  615. */
  616. public Map<String, String> authCodeToOpenid(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
  617. String url;
  618. if (this.useSandbox) {
  619. url = WXPayConstants.SANDBOX_AUTHCODETOOPENID_URL_SUFFIX;
  620. } else {
  621. url = WXPayConstants.AUTHCODETOOPENID_URL_SUFFIX;
  622. }
  623. String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
  624. return this.processResponseXml(respXml);
  625. }
  626. } // end class