From 3a141b85cb17d430bab4cacb738e72519f1d0841 Mon Sep 17 00:00:00 2001 From: godmars Date: Fri, 8 Sep 2017 21:55:50 +0800 Subject: [PATCH] =?UTF-8?q?alipay=E5=BC=82=E6=AD=A5=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zheng/common/util/RequestUtil.java | 17 +++++++++++++ .../pay/common/constant/PayConstant.java | 4 ++++ .../pay/web/controller/AlipayController.java | 24 +++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/zheng-common/src/main/java/com/zheng/common/util/RequestUtil.java b/zheng-common/src/main/java/com/zheng/common/util/RequestUtil.java index 24ab2f8b9..0723426db 100644 --- a/zheng-common/src/main/java/com/zheng/common/util/RequestUtil.java +++ b/zheng-common/src/main/java/com/zheng/common/util/RequestUtil.java @@ -2,6 +2,8 @@ import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; /** * request工具类 @@ -78,4 +80,19 @@ public static String getIpAddr(HttpServletRequest request) { return ip; } + /** + * 请求中参数转Map,for支付宝异步回调,平时建议直接使用request.getParameterMap(),返回Map + * @param request + * @return + */ + public static Map getParameterMap(HttpServletRequest request) { + Map result = new HashMap<>(); + Enumeration parameterNames = request.getParameterNames(); + while (parameterNames.hasMoreElements()) { + String parameterName = (String) parameterNames.nextElement(); + result.put(parameterName, request.getParameter(parameterName)); + } + return result; + } + } diff --git a/zheng-pay/zheng-pay-common/src/main/java/com/zheng/pay/common/constant/PayConstant.java b/zheng-pay/zheng-pay-common/src/main/java/com/zheng/pay/common/constant/PayConstant.java index e620cb01a..c91821e79 100644 --- a/zheng-pay/zheng-pay-common/src/main/java/com/zheng/pay/common/constant/PayConstant.java +++ b/zheng-pay/zheng-pay-common/src/main/java/com/zheng/pay/common/constant/PayConstant.java @@ -8,4 +8,8 @@ */ public class PayConstant extends BaseConstants { + public static final String SUCCESS = "success"; + + public static final String FAILED = "failed"; + } diff --git a/zheng-pay/zheng-pay-web/src/main/java/com/zheng/pay/web/controller/AlipayController.java b/zheng-pay/zheng-pay-web/src/main/java/com/zheng/pay/web/controller/AlipayController.java index b7ffa2153..059eebcc9 100644 --- a/zheng-pay/zheng-pay-web/src/main/java/com/zheng/pay/web/controller/AlipayController.java +++ b/zheng-pay/zheng-pay-web/src/main/java/com/zheng/pay/web/controller/AlipayController.java @@ -3,14 +3,21 @@ import com.alibaba.fastjson.JSONObject; import com.alipay.api.AlipayClient; +import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradePagePayRequest; import com.alipay.api.request.AlipayTradeWapPayRequest; import com.zheng.common.base.BaseController; +import com.zheng.common.util.PropertiesFileUtil; +import com.zheng.common.util.RequestUtil; +import com.zheng.pay.common.constant.PayConstant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + /** * 支付宝 * Created by ZhangShuzheng on 2017/04/04. @@ -54,4 +61,21 @@ public Object wap() throws Exception { return alipayClient.pageExecute(alipayTradeWapPayRequest).getBody(); } + // 异步通知 + @RequestMapping("/notifyUrl") + @ResponseBody + public Object notifyUrl(HttpServletRequest request) throws Exception { + Map parameterMap = RequestUtil.getParameterMap(request); + // 验签 + boolean signVerified = AlipaySignature.rsaCheckV1( + parameterMap, + PropertiesFileUtil.getInstance().get("alipay.alipay_public_key"), + PropertiesFileUtil.getInstance().get("alipay.charset"), + PropertiesFileUtil.getInstance().get("alipay.sign_type")); + if (!signVerified) { + return PayConstant.FAILED; + } + return PayConstant.SUCCESS; + } + } \ No newline at end of file