SettlementServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.crm.settlement.service.impl;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.TypeReference;
  4. import com.crm.rely.backend.core.dto.base.BaseResultDto;
  5. import com.crm.rely.backend.core.exception.PayValidatedException;
  6. import com.crm.rely.backend.core.exception.ServiceException;
  7. import com.crm.rely.backend.service.MqSendService;
  8. import com.crm.rely.backend.util.DateUtil;
  9. import com.crm.rely.backend.util.HttpUtil;
  10. import com.crm.rely.backend.util.MapUtil;
  11. import com.crm.rely.backend.model.constant.ConfigConstants;
  12. import com.crm.rely.backend.model.entity.settlement.SettlementDepositEntity;
  13. import com.crm.rely.backend.model.entity.settlement.SettlementWithdrawEntity;
  14. import com.crm.rely.backend.model.pojo.table.SettlementDepositRecordTable;
  15. import com.crm.rely.backend.model.pojo.table.SettlementWithdrawRecordTable;
  16. import com.crm.settlement.dao.mapper.SettlementDepositRecordMapper;
  17. import com.crm.settlement.dao.mapper.SettlementWithdrawRecordMapper;
  18. import com.crm.settlement.dao.repository.SettlementDepositRecordRepository;
  19. import com.crm.settlement.dao.repository.SettlementWithdrawRecordRepository;
  20. import com.crm.settlement.service.SettlementService;
  21. import com.crm.settlement.service.impl.base.BaseSettlementServiceImpl;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.jsoup.Connection;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.jms.annotation.JmsListener;
  27. import org.springframework.scheduling.annotation.Scheduled;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.util.CollectionUtils;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. @Slf4j
  36. @Service
  37. public class SettlementServiceImpl extends BaseSettlementServiceImpl implements SettlementService {
  38. @Autowired
  39. private SettlementDepositRecordRepository settlementDepositRecordRepository;
  40. @Autowired
  41. private SettlementWithdrawRecordRepository settlementWithdrawRecordRepository;
  42. @Autowired
  43. private SettlementDepositRecordMapper settlementDepositRecordMapper;
  44. @Autowired
  45. private SettlementWithdrawRecordMapper settlementWithdrawRecordMapper;
  46. @Autowired
  47. private MqSendService mqSendService;
  48. /**
  49. * 验证入金订单
  50. */
  51. @Override
  52. public BaseResultDto validateDepositOrder(SettlementDepositEntity entity) {
  53. try {
  54. //验证时间
  55. long time = DateUtil.getTimestampByZero() - entity.getRequestTime().getTime();
  56. if ((time > (10 * 1000)) || (time < 0)) {
  57. log.error("time out ,time:" + time);
  58. throw ServiceException.exception("Time out");
  59. }
  60. verifySign(entity.getSign(), getDepositSignString(entity), entity.getMerchantId());
  61. SettlementDepositRecordTable recordTable = settlementDepositRecordRepository.findFirstBySerialAndChannelCode(entity.getSerial(), entity.getChannelCode());
  62. if (recordTable == null) {
  63. return BaseResultDto.error();
  64. }
  65. if (recordTable.getCallbackAmount().compareTo(entity.getCallbackAmount()) != 0
  66. || !recordTable.getCallbackCurrency().equals(entity.getCallbackCurrency())
  67. || !recordTable.getCallbackSerial().equals(entity.getCallbackSerial())
  68. || !recordTable.getStatus().equals(entity.getStatus())){
  69. return BaseResultDto.error();
  70. }
  71. } catch (Exception e){
  72. return BaseResultDto.error();
  73. }
  74. return BaseResultDto.success();
  75. }
  76. /**
  77. * 验证出金订单
  78. */
  79. @Override
  80. public BaseResultDto validateWithdrawOrder(SettlementWithdrawEntity entity) throws PayValidatedException {
  81. try {
  82. //验证时间
  83. long time = DateUtil.getTimestampByZero() - entity.getRequestTime().getTime();
  84. if ((time > (10 * 1000)) || (time < 0)) {
  85. log.error("time out ,time:" + time);
  86. throw ServiceException.exception("Time out");
  87. }
  88. verifySign(entity.getSign(), getWithdrawSignString(entity), entity.getMerchantId());
  89. SettlementWithdrawRecordTable recordTable = settlementWithdrawRecordRepository.findFirstBySerialAndChannelCode(entity.getSerial(), entity.getChannelCode());
  90. if (recordTable == null) {
  91. return BaseResultDto.error();
  92. }
  93. if (recordTable.getCallbackAmount().compareTo(entity.getCallbackAmount()) != 0
  94. || !recordTable.getCallbackCurrency().equals(entity.getCallbackCurrency())
  95. || !recordTable.getCallbackSerial().equals(entity.getCallbackSerial())
  96. || !recordTable.getStatus().equals(entity.getStatus())){
  97. return BaseResultDto.error();
  98. }
  99. } catch (Exception e){
  100. return BaseResultDto.error();
  101. }
  102. return BaseResultDto.success();
  103. }
  104. @Transactional(rollbackFor = Exception.class)
  105. @JmsListener(destination = ConfigConstants.DEPOSIT_CALLBACK)
  106. public void depositCallback(String content) throws Exception {
  107. log.info("DEPOSIT_CALLBACK: content:" + content);
  108. SettlementDepositRecordTable recordTable = JSON.parseObject(content, SettlementDepositRecordTable.class);
  109. SettlementDepositRecordTable table = settlementDepositRecordRepository.findFirstById(recordTable.getId());
  110. if (table == null){
  111. log.error("DEPOSIT_CALLBACK SettlementDepositRecordTable is null");
  112. return;
  113. }
  114. // 判断状态
  115. if (table.getCallbackMerchantStatus().equals(1)){
  116. log.info("DEPOSIT_CALLBACK: callbackMerchantStatus is Success ");
  117. return;
  118. }
  119. if (table.getCallbackMerchantCount() >= table.getCallbackMerchantMaxRetryCount()){
  120. log.info("DEPOSIT_CALLBACK: callbackMerchantCount is max retry count");
  121. return;
  122. }
  123. String callbackUrl = table.getCallbackUrl();
  124. Map<String, String> params = new HashMap<>();
  125. params.put("serial", table.getSerial());
  126. params.put("channelCode", table.getChannelCode());
  127. params.put("callbackAmount", table.getCallbackAmount().stripTrailingZeros().toPlainString());
  128. params.put("callbackCurrency", table.getCallbackCurrency());
  129. params.put("callbackSerial", table.getCallbackSerial());
  130. params.put("status", String.valueOf(table.getStatus()));
  131. String callbackMerchantBody = JSON.toJSONString(params);
  132. String sign = getSign(params, table.getMerchantId());
  133. params.put("sign", sign);
  134. String callbackContent = JSON.toJSONString(params);
  135. try {
  136. Connection.Response response = HttpUtil.post(callbackUrl, callbackContent);
  137. //打印请求日志
  138. log.info("DEPOSIT_CALLBACK Request URL: {},data:{},response body:{}", callbackUrl, callbackContent, response.body());
  139. BaseResultDto requestEntity = JSON.parseObject(response.body(), BaseResultDto.class);
  140. if (requestEntity == null || requestEntity.getCode() != 200) {
  141. table.setCallbackMerchantStatus(2);
  142. } else {
  143. table.setCallbackMerchantStatus(1);
  144. }
  145. } catch (Exception e) {
  146. log.error("DEPOSIT_CALLBACK, Request URL: {}, data:{}", callbackUrl, callbackContent, e);
  147. table.setCallbackMerchantStatus(2);
  148. }
  149. table.setCallbackMerchantBody(callbackMerchantBody);
  150. table.setCallbackMerchantCount(table.getCallbackMerchantCount() + 1);
  151. table.setCallbackMerchantTime(new Date());
  152. settlementDepositRecordRepository.save(table);
  153. if (table.getCallbackMerchantStatus().equals(2) && table.getCallbackMerchantCount() < table.getCallbackMerchantMaxRetryCount()){
  154. mqSendService.send(ConfigConstants.DEPOSIT_RETRY_CALLBACK, table , 3 * 60 * 1000);
  155. }
  156. }
  157. @Transactional(rollbackFor = Exception.class)
  158. @JmsListener(destination = ConfigConstants.WITHDRAW_CALLBACK)
  159. public void withdrawCallback(String content) throws Exception {
  160. log.info("WITHDRAW_CALLBACK: content:" + content);
  161. SettlementWithdrawRecordTable withdrawRecordTable = JSON.parseObject(content, SettlementWithdrawRecordTable.class);
  162. SettlementWithdrawRecordTable table = settlementWithdrawRecordRepository.findFirstById(withdrawRecordTable.getId());
  163. if (table == null){
  164. log.error("WITHDRAW_CALLBACK SettlementDepositRecordTable is null");
  165. return;
  166. }
  167. // 判断状态
  168. if (table.getCallbackMerchantStatus().equals(1)){
  169. log.info("WITHDRAW_CALLBACK: callbackMerchantStatus is Success ");
  170. return;
  171. }
  172. if (table.getCallbackMerchantCount() >= table.getCallbackMerchantMaxRetryCount()){
  173. log.info("WITHDRAW_CALLBACK: callbackMerchantCount is max retry count");
  174. return;
  175. }
  176. String callbackUrl = table.getCallbackUrl();
  177. Map<String, String> params = new HashMap<>();
  178. params.put("serial", table.getSerial());
  179. params.put("channelCode", table.getChannelCode());
  180. params.put("callbackAmount", table.getCallbackAmount().stripTrailingZeros().toPlainString());
  181. params.put("callbackCurrency", table.getCallbackCurrency());
  182. params.put("callbackSerial", table.getCallbackSerial());
  183. params.put("status", String.valueOf(table.getStatus()));
  184. String callbackMerchantBody = JSON.toJSONString(params);
  185. String sign = getSign(params, table.getMerchantId());
  186. params.put("sign", sign);
  187. String callbackContent = JSON.toJSONString(params);
  188. try {
  189. Connection.Response response = HttpUtil.post(callbackUrl, callbackContent);
  190. //打印请求日志
  191. log.info("WITHDRAW_CALLBACK Request URL: {},data:{},response body:{}", callbackUrl, callbackContent, response.body());
  192. BaseResultDto requestEntity = JSON.parseObject(response.body(), BaseResultDto.class);
  193. if (requestEntity == null || requestEntity.getCode() != 200) {
  194. table.setCallbackMerchantStatus(2);
  195. } else {
  196. table.setCallbackMerchantStatus(1);
  197. }
  198. } catch (Exception e) {
  199. log.error("WITHDRAW_CALLBACK, Request URL: {}, data:{}", callbackUrl, callbackContent, e);
  200. table.setCallbackMerchantStatus(2);
  201. }
  202. table.setCallbackMerchantBody(callbackMerchantBody);
  203. table.setCallbackMerchantCount(table.getCallbackMerchantCount() + 1);
  204. table.setCallbackMerchantTime(new Date());
  205. settlementWithdrawRecordRepository.save(table);
  206. if (table.getCallbackMerchantStatus().equals(2) && table.getCallbackMerchantCount() < table.getCallbackMerchantMaxRetryCount()){
  207. mqSendService.send(ConfigConstants.WITHDRAW_RETRY_CALLBACK, table , 3 * 60 * 1000);
  208. }
  209. }
  210. @Transactional(rollbackFor = Exception.class)
  211. @JmsListener(destination = ConfigConstants.DEPOSIT_RETRY_CALLBACK)
  212. public void depositRetryCallback(String content) throws Exception {
  213. log.info("DEPOSIT_RETRY_CALLBACK: content:" + content);
  214. SettlementDepositRecordTable table = JSON.parseObject(content, SettlementDepositRecordTable.class);
  215. if (table == null){
  216. log.error("DEPOSIT_RETRY_CALLBACK SettlementDepositRecordTable is null");
  217. return;
  218. }
  219. // 判断状态
  220. if (table.getCallbackMerchantStatus().equals(1)){
  221. log.info("DEPOSIT_RETRY_CALLBACK: callbackMerchantStatus is Success , serial:{}", table.getSerial());
  222. return;
  223. }
  224. if (table.getCallbackMerchantCount() >= table.getCallbackMerchantMaxRetryCount()){
  225. log.info("DEPOSIT_RETRY_CALLBACK: callbackMerchantCount is max retry count, serial:{}", table.getSerial());
  226. return;
  227. }
  228. String callbackUrl = table.getCallbackUrl();
  229. String body = table.getCallbackMerchantBody();
  230. if (StringUtils.isEmpty(callbackUrl) || StringUtils.isEmpty(body)){
  231. log.error("DEPOSIT_RETRY_CALLBACK: callbackUrl or callbackMerchantBody is null , serial:{}", table.getSerial());
  232. return;
  233. }
  234. Map<String, String> params = JSON.parseObject(body, new TypeReference<Map<String, String>>(){});
  235. String sign = getSign(params, table.getMerchantId());
  236. params.put("sign", sign);
  237. String callbackContent = JSON.toJSONString(params);
  238. try {
  239. Connection.Response response = HttpUtil.post(callbackUrl, callbackContent);
  240. //打印请求日志
  241. log.info("DEPOSIT_RETRY_CALLBACK Request URL: {},data:{},response body:{}", callbackUrl, callbackContent, response.body());
  242. BaseResultDto requestEntity = JSON.parseObject(response.body(), BaseResultDto.class);
  243. if (requestEntity == null || requestEntity.getCode() != 200) {
  244. table.setCallbackMerchantStatus(2);
  245. } else {
  246. table.setCallbackMerchantStatus(1);
  247. }
  248. } catch (Exception e) {
  249. log.error("DEPOSIT_RETRY_CALLBACK, Request URL: {}, data:{}", callbackUrl, callbackContent, e);
  250. table.setCallbackMerchantStatus(2);
  251. }
  252. table.setCallbackMerchantCount(table.getCallbackMerchantCount() + 1);
  253. settlementDepositRecordRepository.save(table);
  254. if (table.getCallbackMerchantStatus().equals(2) && table.getCallbackMerchantCount() < table.getCallbackMerchantMaxRetryCount()){
  255. mqSendService.send(ConfigConstants.DEPOSIT_RETRY_CALLBACK, table , 3 * 60 * 1000);
  256. }
  257. }
  258. @Transactional(rollbackFor = Exception.class)
  259. @JmsListener(destination = ConfigConstants.WITHDRAW_RETRY_CALLBACK)
  260. public void withdrawRetryCallback(String content) throws Exception {
  261. log.info("WITHDRAW_RETRY_CALLBACK: content:" + content);
  262. SettlementWithdrawRecordTable table = JSON.parseObject(content, SettlementWithdrawRecordTable.class);
  263. if (table == null){
  264. log.error("WITHDRAW_RETRY_CALLBACK SettlementDepositRecordTable is null");
  265. return;
  266. }
  267. // 判断状态
  268. if (table.getCallbackMerchantStatus().equals(1)){
  269. log.info("WITHDRAW_RETRY_CALLBACK: callbackMerchantStatus is Success ");
  270. return;
  271. }
  272. if (table.getCallbackMerchantCount() >= table.getCallbackMerchantMaxRetryCount()){
  273. log.info("WITHDRAW_RETRY_CALLBACK: callbackMerchantCount is max retry count");
  274. return;
  275. }
  276. String callbackUrl = table.getCallbackUrl();
  277. String body = table.getCallbackMerchantBody();
  278. if (StringUtils.isEmpty(callbackUrl) || StringUtils.isEmpty(body)){
  279. log.error("WITHDRAW_RETRY_CALLBACK: callbackUrl or callbackMerchantBody is null , serial:{}", table.getSerial());
  280. return;
  281. }
  282. Map<String, String> params = JSON.parseObject(body, new TypeReference<Map<String, String>>(){});
  283. String sign = getSign(params, table.getMerchantId());
  284. params.put("sign", sign);
  285. String callbackContent = JSON.toJSONString(params);
  286. try {
  287. Connection.Response response = HttpUtil.post(callbackUrl, callbackContent);
  288. //打印请求日志
  289. log.info("WITHDRAW_CALLBACK Request URL: {},data:{},response body:{}", callbackUrl, callbackContent, response.body());
  290. BaseResultDto requestEntity = JSON.parseObject(response.body(), BaseResultDto.class);
  291. if (requestEntity == null || requestEntity.getCode() != 200) {
  292. table.setCallbackMerchantStatus(2);
  293. } else {
  294. table.setCallbackMerchantStatus(1);
  295. }
  296. } catch (Exception e) {
  297. log.error("WITHDRAW_CALLBACK, Request URL: {}, data:{}", callbackUrl, callbackContent, e);
  298. table.setCallbackMerchantStatus(2);
  299. }
  300. table.setCallbackMerchantCount(table.getCallbackMerchantCount() + 1);
  301. settlementWithdrawRecordRepository.save(table);
  302. if (table.getCallbackMerchantStatus().equals(2) && table.getCallbackMerchantCount() < table.getCallbackMerchantMaxRetryCount()){
  303. mqSendService.send(ConfigConstants.WITHDRAW_RETRY_CALLBACK, table , 3 * 60 * 1000);
  304. }
  305. }
  306. /**
  307. * 订单通知失败重新发送通知
  308. * 查询入金和出金 回调失败的 未达到最大回调次数的 超过一天的数据 分别发送回调
  309. */
  310. @Scheduled(cron = "0 23 0 * * ?")
  311. @Transactional(rollbackFor = Exception.class)
  312. @Override
  313. public void scheduleCallback() throws ServiceException {
  314. log.info("scheduleCallback start");
  315. List<SettlementDepositRecordTable> depositRecordTables = settlementDepositRecordMapper.getAllCallback();
  316. List<SettlementWithdrawRecordTable> withdrawRecordTables = settlementWithdrawRecordMapper.getAllCallback();
  317. if (!CollectionUtils.isEmpty(depositRecordTables)){
  318. log.info("scheduleCallback: depositRecordTables size:{}", depositRecordTables.size());
  319. for (SettlementDepositRecordTable table : depositRecordTables){
  320. mqSendService.send(ConfigConstants.DEPOSIT_RETRY_CALLBACK, table);
  321. }
  322. }
  323. if (!CollectionUtils.isEmpty(withdrawRecordTables)){
  324. log.info("scheduleCallback: withdrawRecordTables size:{}", withdrawRecordTables.size());
  325. for (SettlementWithdrawRecordTable table : withdrawRecordTables){
  326. mqSendService.send(ConfigConstants.WITHDRAW_RETRY_CALLBACK, table);
  327. }
  328. }
  329. log.info("scheduleCallback end");
  330. }
  331. private String getDepositSignString(SettlementDepositEntity entity) {
  332. Map<String, String> map = new HashMap<>();
  333. map.put("merchantId", entity.getMerchantId());
  334. map.put("channelCode", entity.getChannelCode());
  335. map.put("serial", entity.getSerial());
  336. map.put("callbackAmount", entity.getCallbackAmount().stripTrailingZeros().toPlainString());
  337. map.put("callbackCurrency", entity.getCallbackCurrency());
  338. map.put("callbackSerial", entity.getCallbackSerial());
  339. map.put("status", String.valueOf(entity.getStatus()));
  340. map.put("requestTime", DateUtil.formatTime(entity.getRequestTime()));
  341. return MapUtil.getStringSortByKey(map);
  342. }
  343. private String getWithdrawSignString(SettlementWithdrawEntity entity) {
  344. Map<String, String> map = new HashMap<>();
  345. map.put("merchantId", entity.getMerchantId());
  346. map.put("channelCode", entity.getChannelCode());
  347. map.put("serial", entity.getSerial());
  348. map.put("callbackAmount", entity.getCallbackAmount().stripTrailingZeros().toPlainString());
  349. map.put("callbackCurrency", entity.getCallbackCurrency());
  350. map.put("callbackSerial", entity.getCallbackSerial());
  351. map.put("status", String.valueOf(entity.getStatus()));
  352. map.put("requestTime", DateUtil.formatTime(entity.getRequestTime()));
  353. return MapUtil.getStringSortByKey(map);
  354. }
  355. }