AppService.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public class AppService {
  16. private static Logger logger = LoggerFactory.getLogger(AppService.class);
  17. @Autowired
  18. private AppDao appDao;
  19. @Autowired
  20. private ItemDao itemDao;
  21. @Autowired
  22. private OrderDao orderDao;
  23. /**
  24. * 获取
  25. * @param appKey
  26. * @return
  27. * @throws Exception
  28. */
  29. public App get(String appKey) throws Exception{
  30. App app = appDao.selectByAppKey(appKey);
  31. if (Objects.isNull(app)) {
  32. throw new Exception("APP不存在: " + appKey);
  33. }
  34. return app;
  35. }
  36. /**
  37. * 发送异步通知
  38. * @param appId
  39. * @param body
  40. */
  41. @Async
  42. public void notify(int appId, String orderNum, String body) {
  43. String notifyUrl = this.packUrl(appDao.select(appId).getNotifyUrl(), orderNum);
  44. logger.debug("异步通知: 请求地址: {}, 参数: {}", notifyUrl, body);
  45. String result = HttpUtil.postJson(notifyUrl, body);
  46. logger.debug("异步通知: 返回信息: {}", result);
  47. }
  48. /**
  49. * 获取同步通知地址
  50. * @param payNum
  51. * @return
  52. */
  53. public String returnUrl(String payNum) {
  54. Order order = orderDao.select(itemDao.selectByPayNum(payNum).getOrderId());
  55. return packUrl(appDao.select(order.getAppId()).getReturnUrl(), order.getOrderNum());
  56. }
  57. /**
  58. * 封装回调地址
  59. * @param url
  60. * @param orderNum
  61. * @return
  62. */
  63. private String packUrl(String url, String orderNum) {
  64. if (url.contains("{orderNum}")) { // rest地址
  65. url = url.replace("{orderNum}", orderNum);
  66. }else{ // param地址
  67. url = url + "?orderNum="+orderNum;
  68. }
  69. return url;
  70. }
  71. }