准备
Jar包依赖
<!-- AliPay Old --> <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.12.4.ALL</version> </dependency>
|
方法
支付宝API,异步通知:官方文档 说到这一点,支付宝还是比较实在的,调用API整个过程不花一分钱,而且文档详细,人工服务很好联系,这点微信支付应该学习一下
@PostMapping(value = "aliPayCallBack") public String aliPayCallBack(HttpServletRequest request) throws Exception { Map<String, String[]> requestParameterMap = request.getParameterMap(); HashMap<String, Object> map = null; log.info("[支付_支付宝]开始异步验签"); map = AliPayUtil.asyncSignVerify(requestParameterMap); String notice = "fail"; if ((boolean) map.get("succ")) { log.info("[支付_支付宝]验签成功!"); LinkedHashMap<String, String> linkedHashMap = (LinkedHashMap<String, String>) map.get("data"); String trade_status = linkedHashMap.get("trade_status"); switch (trade_status) { case "WAIT_BUYER_PAY":
break; case "TRADE_CLOSED":
break; case "TRADE_FINISHED":
break; case "TRADE_SUCCESS":
notice = "success"; break; } } else { log.info("[支付_支付宝]验签失败!"); } return notice; }
|
工具类/方法
验签方法 AliPayUtil.asyncSignVerify() Jar包依赖,主要是StringUtils这个工具类的
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency>
|
public static HashMap<String, Object> asyncSignVerify(Map<String, String[]> requestParameterMap) throws AlipayApiException { String charset = "utf-8"; String alipaypublicKey = "支付宝公钥"; String sign_type = "RSA2"; StringBuilder builder = new StringBuilder(); for (Map.Entry<String, String[]> stringEntry : requestParameterMap.entrySet()) { builder.append(stringEntry.getKey()).append("="). append(StringUtils.removeEnd( StringUtils.removeStart(Arrays.toString( stringEntry.getValue()), "["), "]")) .append("&"); } String resultInfo = StringUtils.removeEnd(builder.toString(), "&"); log.info("[支付_支付宝]待验签内容:{}", resultInfo); String[] temp = resultInfo.split("&"); LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); for (int i = 0; i < temp.length; i++) { String[] arr = temp[i].split("=", 2); String[] tempAagin = new String[arr.length]; for (int j = 0; j < arr.length; j++) { tempAagin[j] = arr[j]; } map.put(tempAagin[0], tempAagin[1]); } boolean flg = AlipaySignature.rsaCheckV1(map, alipaypublicKey, charset, sign_type); HashMap<String, Object> objectHashMap = new HashMap<>(); objectHashMap.put("data", map); objectHashMap.put("succ", flg); return objectHashMap; }
|