alipay异步通知接口

This commit is contained in:
godmars 2017-09-08 21:55:50 +08:00
parent 71173ec19e
commit 3a141b85cb
3 changed files with 45 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package com.zheng.common.util;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/** /**
* request工具类 * request工具类
@ -78,4 +80,19 @@ public class RequestUtil {
return ip; return ip;
} }
/**
* 请求中参数转Map<String, String>,for支付宝异步回调,平时建议直接使用request.getParameterMap(),返回Map<String, String[]>
* @param request
* @return
*/
public static Map<String, String> getParameterMap(HttpServletRequest request) {
Map<String, String> result = new HashMap<>();
Enumeration parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = (String) parameterNames.nextElement();
result.put(parameterName, request.getParameter(parameterName));
}
return result;
}
} }

View File

@ -8,4 +8,8 @@ import com.zheng.common.base.BaseConstants;
*/ */
public class PayConstant extends BaseConstants { public class PayConstant extends BaseConstants {
public static final String SUCCESS = "success";
public static final String FAILED = "failed";
} }

View File

@ -3,14 +3,21 @@ package com.zheng.pay.web.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient; import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest; import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest; import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.zheng.common.base.BaseController; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/** /**
* 支付宝 * 支付宝
* Created by ZhangShuzheng on 2017/04/04. * Created by ZhangShuzheng on 2017/04/04.
@ -54,4 +61,21 @@ public class AlipayController extends BaseController {
return alipayClient.pageExecute(alipayTradeWapPayRequest).getBody(); return alipayClient.pageExecute(alipayTradeWapPayRequest).getBody();
} }
// 异步通知
@RequestMapping("/notifyUrl")
@ResponseBody
public Object notifyUrl(HttpServletRequest request) throws Exception {
Map<String, String> 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;
}
} }