AppService.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.llisoft.pay.service;
  2. import java.util.Objects;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.scheduling.annotation.Async;
  7. import org.springframework.stereotype.Service;
  8. import com.llisoft.pay.dao.AppDao;
  9. import com.llisoft.pay.dao.ItemDao;
  10. import com.llisoft.pay.dao.OrderDao;
  11. import com.llisoft.pay.entity.App;
  12. import com.llisoft.pay.entity.Order;
  13. import com.llisoft.pay.util.HttpUtil;
  14. @Service
  15. @Deprecated // 升级服务,取消app设计
  16. public class AppService {
  17. private static Logger logger = LoggerFactory.getLogger(AppService.class);
  18. @Autowired
  19. private AppDao appDao;
  20. @Autowired
  21. private ItemDao itemDao;
  22. @Autowired
  23. private OrderDao orderDao;
  24. /**
  25. * 获取
  26. * @param appKey
  27. * @return
  28. * @throws Exception
  29. */
  30. public App get(String appKey) throws Exception{
  31. App app = appDao.selectByAppKey(appKey);
  32. if (Objects.isNull(app)) {
  33. throw new Exception("APP不存在: " + appKey);
  34. }
  35. return app;
  36. }
  37. /**
  38. * 发送异步通知
  39. * @param appId
  40. * @param body
  41. */
  42. @Async
  43. public void notify(int appId, String orderNum, String body) {
  44. App app = appDao.select(appId);
  45. String notifyUrl = this.packUrl(app.getNotifyUrl(), orderNum);
  46. if(Objects.isNull(notifyUrl) || notifyUrl.trim().isEmpty()) {
  47. logger.debug("未配置异步通知地址:{} : {}", app.getAppKey(), app.getAppName());
  48. return;
  49. }
  50. logger.debug("异步通知: 请求地址: {}, 参数: {}", notifyUrl, body);
  51. String result = HttpUtil.postJson(notifyUrl, body);
  52. logger.debug("异步通知: 返回信息: {}", result);
  53. }
  54. /**
  55. * 获取同步通知地址
  56. * @param payNum
  57. * @return
  58. */
  59. public String returnUrl(String payNum) {
  60. Order order = orderDao.select(itemDao.selectByPayNum(payNum).getOrderId());
  61. return packUrl(appDao.select(order.getAppId()).getReturnUrl(), order.getOrderNum());
  62. }
  63. /**
  64. * 封装回调地址
  65. * @param url
  66. * @param orderNum
  67. * @return
  68. */
  69. private String packUrl(String url, String orderNum) {
  70. if (url.contains("{orderNum}")) { // rest地址
  71. url = url.replace("{orderNum}", orderNum);
  72. }else{ // param地址
  73. url = url + "?orderNum="+orderNum;
  74. }
  75. return url;
  76. }
  77. }