浏览代码

修改支付

杨杰 4 年之前
父节点
当前提交
973d663aad

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

@@ -35,16 +35,7 @@ public class AliPayController {
 	public String retur(HttpServletRequest request) throws Exception {
 		Map<String, String> resultMap = this.getParamMap(request);
 		logger.info("收到支付宝同步回调: {}", resultMap);
-		try {
-			if (aliPayService.doReturn(resultMap)) {
-				logger.info("支付宝同步回调处理成功!");
-				return "redirect:/pay/"+resultMap.get("out_trade_no")+"/ok";
-			}
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-		logger.error("支付宝同步回调处理失败!");
-		return null;
+		return aliPayService.doReturn(resultMap);
 	}
 	
 	@ApiOperation(value="异步通知")

+ 0 - 28
src/main/java/com/llisoft/pay/controller/WxPayController.java

@@ -1,7 +1,6 @@
 package com.llisoft.pay.controller;
 
 import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -11,7 +10,6 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.util.StreamUtils;
-import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -32,14 +30,6 @@ public class WxPayController {
 	@Autowired
 	private WxPayService wxPayService;
 	
-
-	@ApiOperation(value="同步回调")
-	@GetMapping("/return")
-	public String retur(HttpServletRequest request) throws Exception {
-		Map<String, String> resultMap = this.getParamMap(request);
-		logger.info("收到微信同步回调: {}", resultMap);
-		return wxPayService.dispose(resultMap) ? "success" : "";
-	}
 	
 	@ApiOperation(value="异步通知")
 	@PostMapping(value="/notify")
@@ -52,22 +42,4 @@ public class WxPayController {
 		return wxPayService.dispose(resultMap) ? "success" : "";
 	}
 	
-	
-	/**
-	 * 获取参数Map<String, String>
-	 * @param request
-	 * @return
-	 */
-	private Map<String, String> getParamMap(HttpServletRequest request){
-		Map<String, String> returnMap = new HashMap<String, String>();
-		Map<String, String[]> paramMap = request.getParameterMap();
-		for (String key: paramMap.keySet()) {
-			String[] values = paramMap.get(key);
-			if (values!=null && values.length>0) {
-				returnMap.put(key, values[0]);
-			}
-		}
-		return returnMap;
-	}
-
 }

+ 22 - 9
src/main/java/com/llisoft/pay/entity/Item.java

@@ -20,17 +20,22 @@ public class Item {
     /**
      * 支付状态(1未支付/2已支付)
      */
-    private Byte payStatus;
+    private int payStatus;
+
+    /**
+     * 支付金额(分)
+     */
+    private int payMoney;
 
     /**
      * 支付类型(1支付宝/2微信)
      */
-    private Byte payType;
+    private int payType;
 
     /**
      * 支付成功标记(1异步通知/2同步回调/3主动查询/4对账)
      */
-    private Byte payFlag;
+    private int payFlag;
 
     /**
      * 第三方交易号(由第三方通知返回)
@@ -73,27 +78,35 @@ public class Item {
         this.payNum = payNum;
     }
 
-    public Byte getPayStatus() {
+    public int getPayStatus() {
         return payStatus;
     }
 
-    public void setPayStatus(Byte payStatus) {
+    public void setPayStatus(int payStatus) {
         this.payStatus = payStatus;
     }
 
-    public Byte getPayType() {
+    public int getPayMoney() {
+		return payMoney;
+	}
+
+	public void setPayMoney(int payMoney) {
+		this.payMoney = payMoney;
+	}
+
+	public int getPayType() {
         return payType;
     }
 
-    public void setPayType(Byte payType) {
+    public void setPayType(int payType) {
         this.payType = payType;
     }
 
-    public Byte getPayFlag() {
+    public int getPayFlag() {
         return payFlag;
     }
 
-    public void setPayFlag(Byte payFlag) {
+    public void setPayFlag(int payFlag) {
         this.payFlag = payFlag;
     }
 

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

@@ -170,7 +170,8 @@ public class AliPayService {
 		String paynum = map.get("out_trade_no"); // paynum
 		String tradenum = map.get("trade_no"); // 支付宝交易号
 		int totalAmount = DecimalUtil.toInt(map.get("total_amount"));	// 实际支付金额
-		return payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
+		payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
+		return true;
 	}
 
 
@@ -179,27 +180,26 @@ public class AliPayService {
 	 * @throws Exception 
 	 */
 	@SuppressWarnings("unchecked")
-	public boolean doReturn(Map<String, String> map) throws Exception {
+	public String doReturn(Map<String, String> map) throws Exception {
 		// 验证签名
 		if(!this.checkSign(map)) {
 			logger.error("支付宝同步回调: 签名验证失败: {}", map);
-			return false;
+			return null;
 		}
 		// 查询订单 (同步回调不可靠)
 		String paynum = map.get("out_trade_no");
-		
 		String result = this.query(paynum);
 		Map<String, Object> resultMap = JsonUtil.toObject(result, HashMap.class);
 		// {"alipay_trade_query_response":{"code":"10000","msg":"Success","buyer_logon_id":"ipb***@sandbox.com","buyer_pay_amount":"0.00","buyer_user_id":"2088102170394382","buyer_user_type":"PRIVATE","invoice_amount":"0.00","out_trade_no":"1529637131201603","point_amount":"0.00","receipt_amount":"0.00","send_pay_date":"2018-06-22 11:12:23","total_amount":"12.00","trade_no":"2018062221001004380200826678","trade_status":"TRADE_SUCCESS"},"sign":"gnWtxKDDhkRmoWnfwLVs2RGtt4GbiY+xaVs5+G47D5e9SnQHqGnElKme4jlDdnqhAroX3aqyJLVexohBjzA+DlpRzDhIu5EZLXnDr/B2FZEVjyiU66ImVH6vSyWyOVEjbGIgmVhUslhXqXYK5KxAx3QAdLrJLQeMO/gUdjs0qWcGO9yTU/suDy0YcdLSLlFyUutwD2MBz4Ri0tBk+uHjpxXoTZeb9/lQ7e4BixwCy+wB3hcESWN1b/m77YLKztmbLy67auhxzP9TRwQQ+6WRrTdMe/9rouIt6AW9T+6XSFd/LwL73Qo+1MfQa+iEUx2Iq+AYekhSgVyLJMRgz/qoZA=="}
 		Map<String, String> responseMap = (Map<String, String>) resultMap.get("alipay_trade_query_response");
 		if(!"10000".equals(responseMap.get("code"))) {
 			logger.error("支付宝同步回调: 查询状态异常: {}", responseMap);
-			return false;
+			return null;
 		}
 		// 验证支付宝交易状态
 		if(!this.checkStatus(responseMap)) {
 			logger.error("支付宝同步回调: 交易状态异常: {}", responseMap);
-			return false;
+			return null;
 		}
 		// 完成支付
 		String tradenum = responseMap.get("trade_no"); // 支付宝交易号

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

@@ -6,12 +6,15 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import com.llisoft.pay.dao.OrderDao;
 import com.llisoft.pay.entity.App;
 import com.llisoft.pay.entity.Order;
 import com.llisoft.pay.util.BeanUtil;
 import com.llisoft.pay.util.CodeUtil;
+import com.llisoft.pay.util.HttpUtil;
+import com.llisoft.pay.util.JsonUtil;
 import com.llisoft.pay.vo.OrderInfoResponseVo;
 
 
@@ -94,4 +97,27 @@ public class OrderService {
 		return vo;
 	}
 	
+	/**
+	 * 完成订单
+	 * 更新订单状态 + 通知业务服务
+	 * @param orderId
+	 * @return 返回同步回调地址
+	 * @throws Exception 
+	 */
+	@Transactional
+	public String 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());
+		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());
+	}
+	
 }

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

@@ -13,8 +13,6 @@ import com.llisoft.pay.dao.OrderDao;
 import com.llisoft.pay.entity.Item;
 import com.llisoft.pay.entity.Order;
 import com.llisoft.pay.util.CodeUtil;
-import com.llisoft.pay.util.HttpUtil;
-import com.llisoft.pay.util.JsonUtil;
 
 @Service
 public class PayService {
@@ -50,7 +48,7 @@ public class PayService {
 	@Autowired
 	private WxPayService wxPayService;
 	@Autowired
-	private AppService appService;
+	private OrderService orderService;
 	
 	
 	/**
@@ -118,26 +116,15 @@ public class PayService {
 	 * @throws Exception 
 	 */
 	@Transactional
-	public boolean finish(String payNum, String tradeNum, int money, byte flag) throws Exception{
+	public String finish(String payNum, String tradeNum, int money, byte flag) throws Exception{
 		Item item = this.get(payNum);
-		Order order = orderDao.select(item.getOrderId());
-		if (order.getOrderMoney() != money) { // 核对金额
+		if (item.getPayMoney() != money) { // 核对金额
 			throw new Exception("支付金额异常: " + money);
 		}
 		// 更新支付状态
 		payDao.updatePay(item.getPayId(), tradeNum, STATUS_PAYED, flag);
 		logger.info("支付处理成功, 状态更新为已支付: {}", payNum);
-		// 更新订单状态
-		orderDao.updatePay(order.getOrderId(), STATUS_PAYED);
-		// 发送支付成功异步通知
-		String notifyUrl = appService.getNotifyUrl(order.getAppId(), order.getOrderNum());
-		if(Objects.nonNull(notifyUrl)) {
-			logger.info("订单完成: 异步通知业务地址: {}, 参数: {}", notifyUrl, JsonUtil.toJson(order));
-			String result = HttpUtil.postJson(notifyUrl, JsonUtil.toJson(order));
-			logger.info("订单完成: 业务端返回信息: {}", result);
-			return "success".equals(result);
-		}
-		return true;
+		return orderService.finish(item.getOrderId());
 	}
 	
 	/**

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

@@ -178,7 +178,8 @@ public class WxPayService {
 		String paynum = resultMap.get("out_trade_no"); // paynum
 		String tradenum = resultMap.get("trade_no"); // 支付宝交易号
 		int totalAmount = DecimalUtil.toInt(resultMap.get("total_amount"));	// 实际支付金额
-		return payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
+		payService.finish(paynum, tradenum, totalAmount, PayService.FLAG_NOTIFY);
+		return true;
 	}