NotifyCallback.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.keao.edu.user.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.huifu.adapay.Adapay;
  5. import com.huifu.adapay.notify.MQTTCallbackHandler;
  6. import com.keao.edu.thirdparty.adapay.ConfigInit;
  7. import com.keao.edu.user.dao.SysConfigDao;
  8. import com.keao.edu.user.entity.ExamRegistrationPayment;
  9. import com.keao.edu.user.enums.TransStatusEnum;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import javax.annotation.PostConstruct;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. @Service
  16. public class NotifyCallback implements MQTTCallbackHandler {
  17. @Autowired
  18. private ExamRegistrationPaymentService examRegistrationPaymentService;
  19. @Autowired
  20. private SysConfigDao sysConfigDao;
  21. private static NotifyCallback notifyCallback;
  22. @PostConstruct
  23. public void init() {
  24. notifyCallback = this;
  25. String baseApiUrl = notifyCallback.sysConfigDao.findConfigValue("base_api_url");
  26. Adapay.setDeviceID(baseApiUrl);
  27. }
  28. /**
  29. * 用户接收并处理支付成功的异步消息
  30. *
  31. * @param payment 成功的支付对象
  32. * @throws Exception 异常
  33. */
  34. @Override
  35. public void paymentSuccessMessageArrived(String payment) throws Exception {
  36. System.out.println(String.format("receive paymentSuccess msg=%s", payment));
  37. JSONObject dataObj = JSON.parseObject(payment);
  38. if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
  39. return;
  40. }
  41. String orderNo = dataObj.getString("order_no");
  42. ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
  43. if (order == null || !order.getTransStatus().equals(TransStatusEnum.ING)) {
  44. return;
  45. }
  46. Map<String,String> rpMap = new HashMap<>();
  47. rpMap.put("channelType", dataObj.getString("pay_channel"));
  48. rpMap.put("transStatus","SUCCESS");
  49. rpMap.put("orderNo",orderNo);
  50. rpMap.put("transNo",order.getTransNo());
  51. notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
  52. }
  53. /**
  54. * 用户接收并处理支付失败的异步消息
  55. *
  56. * @param payment 失败的支付对象
  57. * @throws Exception 异常
  58. */
  59. @Override
  60. public void paymentFailedMessageArrived(String payment) throws Exception {
  61. System.out.println(String.format("receive paymentFailed msg=%s", payment));
  62. JSONObject dataObj = JSON.parseObject(payment);
  63. if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
  64. return;
  65. }
  66. String orderNo = dataObj.getString("order_no");
  67. ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
  68. if (order == null || !order.getTransStatus().equals("ING")) {
  69. return;
  70. }
  71. Map<String,String> rpMap = new HashMap<>();
  72. rpMap.put("channelType", dataObj.getString("pay_channel"));
  73. rpMap.put("transStatus","FAILED");
  74. rpMap.put("orderNo",orderNo);
  75. rpMap.put("transNo",order.getTransNo());
  76. notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
  77. }
  78. /**
  79. * 用户接收并处理关闭支付交易成功的异步消息
  80. *
  81. * @param payment 关闭成功的支付对象
  82. * @throws Exception 异常
  83. */
  84. @Override
  85. public void paymentCloseSuccessMessageArrived(String payment) throws Exception {
  86. System.out.println(String.format("receive paymentCloseSuccess msg=%s", payment));
  87. JSONObject dataObj = JSON.parseObject(payment);
  88. if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
  89. return;
  90. }
  91. String orderNo = dataObj.getString("order_no");
  92. ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
  93. if (order == null || !order.getTransStatus().equals(TransStatusEnum.ING)) {
  94. return;
  95. }
  96. Map<String,String> rpMap = new HashMap<>();
  97. rpMap.put("channelType", dataObj.getString("pay_channel"));
  98. rpMap.put("transStatus","FAILED");
  99. rpMap.put("orderNo",orderNo);
  100. rpMap.put("transNo",order.getTransNo());
  101. notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
  102. }
  103. /**
  104. * 用户接收并处理关闭支付交易失败的异步消息
  105. *
  106. * @param payment 关闭失败的支付对象
  107. * @throws Exception 异常
  108. */
  109. @Override
  110. public void paymentCloseFailedMessageArrived(String payment) throws Exception {
  111. System.out.println(String.format("receive paymentCloseFailed msg=%s", payment));
  112. }
  113. @Override
  114. public void paymentReverseFailedMessageArrived(String payment) throws Exception {
  115. System.out.println(String.format("receive payment ReverseFailed msg=%s", payment));
  116. }
  117. @Override
  118. public void paymentReverseSuccessMessageArrived(String payment) throws Exception {
  119. System.out.println(String.format("receive payment ReverseSuccess msg=%s", payment));
  120. }
  121. /**
  122. * 用户接收并处理退款成功的异步消息
  123. *
  124. * @param refund 成功的退款对象
  125. * @throws Exception 异常
  126. */
  127. @Override
  128. public void refundSuccessMessageArrived(String refund) throws Exception {
  129. System.out.println(String.format("receive refundSuccess msg=%s", refund));
  130. }
  131. /**
  132. * 用户接收并处理退款失败的异步消息
  133. *
  134. * @param refund 失败的退款对象
  135. * @throws Exception 异常
  136. */
  137. @Override
  138. public void refundFailedMessageArrived(String refund) throws Exception {
  139. System.out.println(String.format("receive refundFailed msg=%s", refund));
  140. }
  141. @Override
  142. public void corpMemberSuccessMessageArrived(String corpMember) throws Exception {
  143. System.out.println(String.format("receive corpMember msg=%s", corpMember));
  144. }
  145. @Override
  146. public void corpMemberFailedMessageArrived(String corpMember) throws Exception {
  147. System.out.println(String.format("receive corpMember msg=%s", corpMember));
  148. }
  149. @Override
  150. public void unknowMessageArrived(String message) throws Exception {
  151. System.out.println(String.format("receive unknow msg=%s", message));
  152. }
  153. @Override
  154. public void drawCashFailedMessageArrived(String drawCash) throws Exception {
  155. System.out.println(String.format("receive unknow msg=%s", drawCash));
  156. }
  157. @Override
  158. public void drawCashSuccessedMessageArrived(String drawCash) throws Exception {
  159. System.out.println(String.format("receive unknow msg=%s", drawCash));
  160. }
  161. @Override
  162. public void pagePaymentFailedMessageArrived(String arg0) throws Exception {
  163. // TODO Auto-generated method stub
  164. }
  165. @Override
  166. public void pagePaymentSuccessedMessageArrived(String arg0) throws Exception {
  167. // TODO Auto-generated method stub
  168. }
  169. @Override
  170. public void connectionLost(String message) throws Exception {
  171. // TODO Auto-generated method stub
  172. System.out.println("mqtt...........lost.............");
  173. }
  174. @Override
  175. public void connectSuccess() throws Exception {
  176. System.out.println("mqtt...........connect.............");
  177. }
  178. }