杨杰 4 năm trước cách đây
mục cha
commit
0bc69a5201

+ 2 - 2
src/main/java/com/llisoft/pay/controller/AliPayController.java

@@ -35,7 +35,7 @@ public class AliPayController {
 	public String retur(HttpServletRequest request) throws Exception {
 		Map<String, String> resultMap = this.getParamMap(request);
 		logger.info("收到支付宝同步回调: {}", resultMap);
-		return "redirect:" + aliPayService.dispose(resultMap);
+		return "redirect:" + aliPayService.doReturn(resultMap);
 	}
 	
 	@ApiOperation(value="异步通知")
@@ -44,7 +44,7 @@ public class AliPayController {
 		Map<String, String> resultMap = this.getParamMap(request);
 		logger.info("收到支付宝异步通知: {}", resultMap);
 		try {
-			if (aliPayService.dispose(resultMap)) {
+			if (aliPayService.doNotify(resultMap)) {
 				logger.info("支付宝异步通知处理成功!");
 				return "success";
 			}

+ 29 - 10
src/main/java/com/llisoft/pay/service/AliPayService.java

@@ -53,6 +53,9 @@ public class AliPayService {
 	
 	@Autowired
 	private PayService payService;
+	@Autowired
+	private AppService appService;
+	
 	private AlipayClient alipayClient;
 	
 	
@@ -156,41 +159,57 @@ public class AliPayService {
 		}
 		// 验证支付宝交易状态
 		if(!this.checkStatus(map)) {
-			logger.error("支付宝处理: 交易状态异常: {}", map);
+			logger.error("支付宝查询: 交易状态异常: {}", map);
 			return false;
 		}
 		// 完成支付
 		String tradenum = map.get("trade_no"); // 支付宝交易号
 		int totalAmount = DecimalUtil.toInt(map.get("total_amount"));	// 实际支付金额
-		payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
-		return true;
+		return payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
 	}
 	
 	/**
-	 * 处理通知
+	 * 处理同步回调
+	 * @throws Exception 
+	 */
+	public String doReturn(Map<String, String> map) throws Exception {
+		// 验证签名
+		if(!this.checkSign(map)) {
+			logger.error("支付宝同步回调: 签名验证失败: {}", map);
+			return null;
+		}
+		// 通过接口查询订单 (同步回调不可靠)
+		String payNum = map.get("out_trade_no");
+		if(this.query(payNum)) {
+			return appService.returnUrl(payNum);
+		}
+		return null;
+	}
+	
+	/**
+	 * 处理异步通知
 	 * @param map
 	 * @return
 	 * @throws Exception 
 	 */
-	public boolean dispose(Map<String, String> map) throws Exception {
+	public boolean doNotify(Map<String, String> map) throws Exception {
 		// 验证签名
 		if(!this.checkSign(map)) {
-			logger.error("支付宝处理: 签名验证失败: {}", map);
+			logger.error("支付宝异步通知: 签名验证失败: {}", map);
 			return false;
 		}
 		// 验证支付宝交易状态
 		if(!this.checkStatus(map)) {
-			logger.error("支付宝处理: 交易状态异常: {}", map);
+			logger.error("支付宝异步通知: 交易状态异常: {}", map);
 			return false;
 		}
 		// 完成支付
 		String paynum = map.get("out_trade_no"); // paynum
 		String tradenum = map.get("trade_no"); // 支付宝交易号
 		int totalAmount = DecimalUtil.toInt(map.get("total_amount"));	// 实际支付金额
-		payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
-		return true;
+		return payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
 	}
-
+	
 	/**
 	 * 验证签名
 	 * @param map

+ 20 - 3
src/main/java/com/llisoft/pay/service/AppService.java

@@ -6,13 +6,20 @@ import org.springframework.beans.factory.annotation.Autowired;
 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;
 
 @Service
 public class AppService {
 	
 	@Autowired
 	private AppDao appDao;
+	@Autowired
+	private ItemDao itemDao;
+	@Autowired
+	private OrderDao orderDao;
 	
 	
 	/**
@@ -35,7 +42,7 @@ public class AppService {
 	 * @param orderNum
 	 * @return
 	 */
-	public String getNotifyUrl(int appId, String orderNum) {
+	public String notifyUrl(int appId, String orderNum) {
 		return packUrl(appDao.select(appId).getNotifyUrl(), orderNum);
 	}
 	
@@ -45,17 +52,27 @@ public class AppService {
 	 * @param orderNum
 	 * @return
 	 */
-	public String getReturnUrl(int appId, String orderNum) {
+	public String returnUrl(int appId, String orderNum) {
 		return packUrl(appDao.select(appId).getReturnUrl(), orderNum);
 	}
 	
 	/**
+	 * 获取同步通知地址
+	 * @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
 	 */
-	public String packUrl(String url, String orderNum) {
+	private String packUrl(String url, String orderNum) {
 		if (url.contains("{orderNum}")) { // rest地址
 			url = url.replace("{orderNum}", orderNum);
 		}else{ // param地址

+ 3 - 3
src/main/java/com/llisoft/pay/service/OrderService.java

@@ -109,19 +109,19 @@ public class OrderService {
 	 * @throws Exception 
 	 */
 	@Transactional
-	public String finish(int orderId) throws Exception{
+	public boolean finish(int orderId) throws Exception{
 		// 更新订单状态
 		orderDao.updatePay(orderId, STATUS_PAYED);
 		logger.info("订单处理成功, 状态更新为已支付: {}", orderId);
 		// 发送支付成功异步通知
 		Order order = orderDao.select(orderId);
-		String notifyUrl = appService.getNotifyUrl(order.getAppId(), order.getOrderNum());
+		String notifyUrl = appService.notifyUrl(order.getAppId(), order.getOrderNum());
 		if(Objects.nonNull(notifyUrl) && !notifyUrl.trim().isEmpty()) {
 			logger.info("订单完成: 异步通知业务地址: {}, 参数: {}", notifyUrl, JsonUtil.toJson(order));
 			String result = HttpUtil.postJson(notifyUrl, JsonUtil.toJson(order));
 			logger.info("订单完成: 业务端返回信息: {}", result);
 		}
-		return appService.getReturnUrl(order.getAppId(), order.getOrderNum());
+		return true;
 	}
 	
 }

+ 1 - 1
src/main/java/com/llisoft/pay/service/PayService.java

@@ -118,7 +118,7 @@ public class PayService {
 	 * @throws Exception 
 	 */
 	@Transactional
-	public String finish(String payNum, String tradeNum, int money, byte flag) throws Exception{
+	public boolean finish(String payNum, String tradeNum, int money, byte flag) throws Exception{
 		Item item = this.get(payNum);
 		if (item.getPayMoney() != money) { // 核对金额
 			throw new Exception("支付金额异常: " + money);