1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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
- 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) {
- String notifyUrl = this.packUrl(appDao.select(appId).getNotifyUrl(), orderNum);
- 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;
- }
-
- }
|