package com.llisoft.pay.service; import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import com.llisoft.pay.dao.AppDao; import com.llisoft.pay.dao.ItemDao; import com.llisoft.pay.dao.OrderDao; import com.llisoft.pay.entity.App; import com.llisoft.pay.entity.Order; import com.llisoft.pay.util.HttpUtil; @Service @Deprecated // 升级服务,取消app设计 public class AppService { private static Logger logger = LoggerFactory.getLogger(AppService.class); @Autowired private AppDao appDao; @Autowired private ItemDao itemDao; @Autowired private OrderDao orderDao; /** * 获取 * @param appKey * @return * @throws Exception */ public App get(String appKey) throws Exception{ App app = appDao.selectByAppKey(appKey); if (Objects.isNull(app)) { throw new Exception("APP不存在: " + appKey); } return app; } /** * 发送异步通知 * @param appId * @param body */ @Async public void notify(int appId, String orderNum, String body) { App app = appDao.select(appId); String notifyUrl = this.packUrl(app.getNotifyUrl(), orderNum); if(Objects.isNull(notifyUrl) || notifyUrl.trim().isEmpty()) { logger.debug("未配置异步通知地址:{} : {}", app.getAppKey(), app.getAppName()); return; } logger.debug("异步通知: 请求地址: {}, 参数: {}", notifyUrl, body); String result = HttpUtil.postJson(notifyUrl, body); logger.debug("异步通知: 返回信息: {}", result); } /** * 获取同步通知地址 * @param payNum * @return */ public String returnUrl(String payNum) { Order order = orderDao.select(itemDao.selectByPayNum(payNum).getOrderId()); return packUrl(appDao.select(order.getAppId()).getReturnUrl(), order.getOrderNum()); } /** * 封装回调地址 * @param url * @param orderNum * @return */ private String packUrl(String url, String orderNum) { if (url.contains("{orderNum}")) { // rest地址 url = url.replace("{orderNum}", orderNum); }else{ // param地址 url = url + "?orderNum="+orderNum; } return url; } }