RefundBusiness.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.tencent.business;
  2. import com.tencent.common.Configure;
  3. import com.tencent.common.Log;
  4. import com.tencent.common.Signature;
  5. import com.tencent.common.Util;
  6. import com.tencent.common.report.ReporterFactory;
  7. import com.tencent.common.report.protocol.ReportReqData;
  8. import com.tencent.common.report.service.ReportService;
  9. import com.tencent.protocol.refund_protocol.RefundReqData;
  10. import com.tencent.protocol.refund_protocol.RefundResData;
  11. import com.tencent.service.RefundService;
  12. import org.slf4j.LoggerFactory;
  13. /**
  14. * User: rizenguo
  15. * Date: 2014/12/2
  16. * Time: 17:51
  17. */
  18. public class RefundBusiness {
  19. public RefundBusiness() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
  20. refundService = new RefundService();
  21. }
  22. public interface ResultListener{
  23. //API返回ReturnCode不合法,支付请求逻辑错误,请仔细检测传过去的每一个参数是否合法,或是看API能否被正常访问
  24. void onFailByReturnCodeError(RefundResData refundResData);
  25. //API返回ReturnCode为FAIL,支付API系统返回失败,请检测Post给API的数据是否规范合法
  26. void onFailByReturnCodeFail(RefundResData refundResData);
  27. //支付请求API返回的数据签名验证失败,有可能数据被篡改了
  28. void onFailBySignInvalid(RefundResData refundResData);
  29. //退款失败
  30. void onRefundFail(RefundResData refundResData);
  31. //退款成功
  32. void onRefundSuccess(RefundResData refundResData);
  33. }
  34. //打log用
  35. private static Log log = new Log(LoggerFactory.getLogger(RefundBusiness.class));
  36. //执行结果
  37. private static String result = "";
  38. private RefundService refundService;
  39. /**
  40. * 调用退款业务逻辑
  41. * @param refundReqData 这个数据对象里面包含了API要求提交的各种数据字段
  42. * @param resultListener 业务逻辑可能走到的结果分支,需要商户处理
  43. * @throws Exception
  44. */
  45. public void run(RefundReqData refundReqData,ResultListener resultListener) throws Exception {
  46. //--------------------------------------------------------------------
  47. //构造请求“退款API”所需要提交的数据
  48. //--------------------------------------------------------------------
  49. //API返回的数据
  50. String refundServiceResponseString;
  51. long costTimeStart = System.currentTimeMillis();
  52. log.i("退款查询API返回的数据如下:");
  53. refundServiceResponseString = refundService.request(refundReqData);
  54. long costTimeEnd = System.currentTimeMillis();
  55. long totalTimeCost = costTimeEnd - costTimeStart;
  56. log.i("api请求总耗时:" + totalTimeCost + "ms");
  57. log.i(refundServiceResponseString);
  58. //将从API返回的XML数据映射到Java对象
  59. RefundResData refundResData = (RefundResData) Util.getObjectFromXML(refundServiceResponseString, RefundResData.class);
  60. ReportReqData reportReqData = new ReportReqData(
  61. refundResData.getDevice_info(),
  62. Configure.REFUND_API,
  63. (int) (totalTimeCost),//本次请求耗时
  64. refundResData.getReturn_code(),
  65. refundResData.getReturn_msg(),
  66. refundResData.getResult_code(),
  67. refundResData.getErr_code(),
  68. refundResData.getErr_code_des(),
  69. refundResData.getOut_trade_no(),
  70. Configure.getIP()
  71. );
  72. long timeAfterReport;
  73. if(Configure.isUseThreadToDoReport()){
  74. ReporterFactory.getReporter(reportReqData).run();
  75. timeAfterReport = System.currentTimeMillis();
  76. Util.log("pay+report总耗时(异步方式上报):"+(timeAfterReport-costTimeStart) + "ms");
  77. }else{
  78. ReportService.request(reportReqData);
  79. timeAfterReport = System.currentTimeMillis();
  80. Util.log("pay+report总耗时(同步方式上报):"+(timeAfterReport-costTimeStart) + "ms");
  81. }
  82. if (refundResData == null || refundResData.getReturn_code() == null) {
  83. setResult("Case1:退款API请求逻辑错误,请仔细检测传过去的每一个参数是否合法,或是看API能否被正常访问",Log.LOG_TYPE_ERROR);
  84. resultListener.onFailByReturnCodeError(refundResData);
  85. return;
  86. }
  87. //Debug:查看数据是否正常被填充到scanPayResponseData这个对象中
  88. //Util.reflect(refundResData);
  89. if (refundResData.getReturn_code().equals("FAIL")) {
  90. ///注意:一般这里返回FAIL是出现系统级参数错误,请检测Post给API的数据是否规范合法
  91. setResult("Case2:退款API系统返回失败,请检测Post给API的数据是否规范合法",Log.LOG_TYPE_ERROR);
  92. resultListener.onFailByReturnCodeFail(refundResData);
  93. } else {
  94. log.i("退款API系统成功返回数据");
  95. //--------------------------------------------------------------------
  96. //收到API的返回数据的时候得先验证一下数据有没有被第三方篡改,确保安全
  97. //--------------------------------------------------------------------
  98. if (!Signature.checkIsSignValidFromResponseString(refundServiceResponseString)) {
  99. setResult("Case3:退款请求API返回的数据签名验证失败,有可能数据被篡改了",Log.LOG_TYPE_ERROR);
  100. resultListener.onFailBySignInvalid(refundResData);
  101. return;
  102. }
  103. if (refundResData.getResult_code().equals("FAIL")) {
  104. log.i("出错,错误码:" + refundResData.getErr_code() + " 错误信息:" + refundResData.getErr_code_des());
  105. setResult("Case4:【退款失败】",Log.LOG_TYPE_ERROR);
  106. //退款失败时再怎么延时查询退款状态都没有意义,这个时间建议要么再手动重试一次,依然失败的话请走投诉渠道进行投诉
  107. resultListener.onRefundFail(refundResData);
  108. } else {
  109. //退款成功
  110. setResult("Case5:【退款成功】",Log.LOG_TYPE_INFO);
  111. resultListener.onRefundSuccess(refundResData);
  112. }
  113. }
  114. }
  115. public void setRefundService(RefundService service) {
  116. refundService = service;
  117. }
  118. public String getResult() {
  119. return result;
  120. }
  121. public void setResult(String result) {
  122. RefundBusiness.result = result;
  123. }
  124. public void setResult(String result,String type){
  125. setResult(result);
  126. log.log(type,result);
  127. }
  128. }