DownloadBillBusiness.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.tencent.business;
  2. import com.tencent.common.Configure;
  3. import com.tencent.common.Log;
  4. import com.tencent.common.Util;
  5. import com.tencent.common.report.ReporterFactory;
  6. import com.tencent.common.report.protocol.ReportReqData;
  7. import com.tencent.common.report.service.ReportService;
  8. import com.tencent.protocol.downloadbill_protocol.DownloadBillReqData;
  9. import com.tencent.protocol.downloadbill_protocol.DownloadBillResData;
  10. import com.tencent.service.DownloadBillService;
  11. import com.thoughtworks.xstream.io.StreamException;
  12. import org.slf4j.LoggerFactory;
  13. /**
  14. * User: rizenguo
  15. * Date: 2014/12/3
  16. * Time: 10:45
  17. */
  18. public class DownloadBillBusiness {
  19. public DownloadBillBusiness() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
  20. downloadBillService = new DownloadBillService();
  21. }
  22. public interface ResultListener{
  23. //API返回ReturnCode不合法,支付请求逻辑错误,请仔细检测传过去的每一个参数是否合法,或是看API能否被正常访问
  24. void onFailByReturnCodeError(DownloadBillResData downloadBillResData);
  25. //API返回ReturnCode为FAIL,支付API系统返回失败,请检测Post给API的数据是否规范合法
  26. void onFailByReturnCodeFail(DownloadBillResData downloadBillResData);
  27. //下载对账单失败
  28. void onDownloadBillFail(String response);
  29. //下载对账单成功
  30. void onDownloadBillSuccess(String response);
  31. }
  32. //打log用
  33. private static Log log = new Log(LoggerFactory.getLogger(DownloadBillBusiness.class));
  34. //执行结果
  35. private static String result = "";
  36. private DownloadBillService downloadBillService;
  37. /**
  38. * 请求对账单下载服务
  39. * @param downloadBillReqData 这个数据对象里面包含了API要求提交的各种数据字段
  40. * @param resultListener 商户需要自己监听被扫支付业务逻辑可能触发的各种分支事件,并做好合理的响应处理
  41. * @return API返回的XML数据
  42. * @throws Exception
  43. */
  44. public void run(DownloadBillReqData downloadBillReqData,ResultListener resultListener) throws Exception {
  45. //--------------------------------------------------------------------
  46. //构造请求“对账单API”所需要提交的数据
  47. //--------------------------------------------------------------------
  48. //API返回的数据
  49. String downloadBillServiceResponseString;
  50. long costTimeStart = System.currentTimeMillis();
  51. //支持加载本地测试数据进行调试
  52. log.i("对账单API返回的数据如下:");
  53. downloadBillServiceResponseString = downloadBillService.request(downloadBillReqData);
  54. long costTimeEnd = System.currentTimeMillis();
  55. long totalTimeCost = costTimeEnd - costTimeStart;
  56. log.i("api请求总耗时:" + totalTimeCost + "ms");
  57. log.i(downloadBillServiceResponseString);
  58. DownloadBillResData downloadBillResData;
  59. String returnCode = "";
  60. String returnMsg = "";
  61. try {
  62. //注意,这里失败的时候是返回xml数据,成功的时候反而返回非xml数据
  63. downloadBillResData = (DownloadBillResData) Util.getObjectFromXML(downloadBillServiceResponseString, DownloadBillResData.class);
  64. if (downloadBillResData == null || downloadBillResData.getReturn_code() == null) {
  65. setResult("Case1:对账单API请求逻辑错误,请仔细检测传过去的每一个参数是否合法,或是看API能否被正常访问",Log.LOG_TYPE_ERROR);
  66. resultListener.onFailByReturnCodeError(downloadBillResData);
  67. return;
  68. }
  69. if (downloadBillResData.getReturn_code().equals("FAIL")) {
  70. ///注意:一般这里返回FAIL是出现系统级参数错误,请检测Post给API的数据是否规范合法
  71. setResult("Case2:对账单API系统返回失败,请检测Post给API的数据是否规范合法",Log.LOG_TYPE_ERROR);
  72. resultListener.onFailByReturnCodeFail(downloadBillResData);
  73. returnCode = "FAIL";
  74. returnMsg = downloadBillResData.getReturn_msg();
  75. }
  76. } catch (StreamException e) {
  77. //注意,这里成功的时候是直接返回纯文本的对账单文本数据,非XML格式
  78. if (downloadBillServiceResponseString.equals(null) || downloadBillServiceResponseString.equals("")) {
  79. setResult("Case4:对账单API系统返回数据为空",Log.LOG_TYPE_ERROR);
  80. resultListener.onDownloadBillFail(downloadBillServiceResponseString);
  81. } else {
  82. setResult("Case3:对账单API系统成功返回数据",Log.LOG_TYPE_INFO);
  83. resultListener.onDownloadBillSuccess(downloadBillServiceResponseString);
  84. }
  85. returnCode = "SUCCESS";
  86. } finally {
  87. ReportReqData reportReqData = new ReportReqData(
  88. downloadBillReqData.getDevice_info(),
  89. Configure.DOWNLOAD_BILL_API,
  90. (int) (totalTimeCost),//本次请求耗时
  91. returnCode,
  92. returnMsg,
  93. "",
  94. "",
  95. "",
  96. "",
  97. Configure.getIP()
  98. );
  99. long timeAfterReport;
  100. if(Configure.isUseThreadToDoReport()){
  101. ReporterFactory.getReporter(reportReqData).run();
  102. timeAfterReport = System.currentTimeMillis();
  103. Util.log("pay+report总耗时(异步方式上报):"+(timeAfterReport-costTimeStart) + "ms");
  104. }else{
  105. ReportService.request(reportReqData);
  106. timeAfterReport = System.currentTimeMillis();
  107. Util.log("pay+report总耗时(同步方式上报):"+(timeAfterReport-costTimeStart) + "ms");
  108. }
  109. }
  110. }
  111. public void setDownloadBillService(DownloadBillService service) {
  112. downloadBillService = service;
  113. }
  114. public String getResult() {
  115. return result;
  116. }
  117. public void setResult(String result) {
  118. DownloadBillBusiness.result = result;
  119. }
  120. public void setResult(String result,String type){
  121. setResult(result);
  122. log.log(type,result);
  123. }
  124. }