需要接入支付宝支付了
支付宝支付相对于微信支付接入更麻烦一些,要配置密钥啥的
需要支付宝开放平台账号,并开通网站支付相关权限,具体查看官方网站
上代码:
1 - 引入依赖
com.alipay.sdk
alipay-sdk-java
4.10.0.ALL
2 - 配置,具体参数自行设置
importcom.alipay.api.AlipayApiException;importcom.alipay.api.AlipayClient;importcom.alipay.api.DefaultAlipayClient;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;
@Configurationpublic classAlipayConfig {
@Value("${***}")privateString appId;
@Value("${***}")privateString gateway;
@Value("${***}")privateString privateKey;
@Value("${***}")privateString alipayPublicKey;
@Value("${***}")privateString charset;
@Value("${***}")privateString signType;
@Beanpublic AlipayClient alipayClient() throwsAlipayApiException {return new DefaultAlipayClient(gateway, appId, privateKey, "json", charset, alipayPublicKey, signType);
}
}
3 - 支付,参数自行写入,返回的是一个链接,前端直接打开就可以了,接口就不写了
public String aliPagePay(AlipayPara para) throwsAlipayApiException {
AlipayTradePagePayRequest alipayRequest= newAlipayTradePagePayRequest();
alipayRequest.setNotifyUrl(aliNotifyUrl);
JSONObject job= newJSONObject();job.put("out_trade_no", uuid());
job.put("product_code", para.getProductCode());
job.put("total_amount", para.getTotalAmount().toString());
job.put("subject", para.getSubject());
alipayRequest.setBizContent(job.toJSONString());
// 记录支付单相关操作,代码省略AlipayResponse response= alipayClient.pageExecute(alipayRequest, "GET");returnresponse.getBody();
}
4 - 异步通知,验签,同步没用到,就不写了
public void aliPagePayNotifyMethod(HttpServletRequest request) throwsAlipayApiException, SQLException {
// 验签if (!aliPagePayRsaCheck(request)) {throw new AlipayApiException("AliPagePay notify Rsa Check Failed");
}
String outTradeNo= getParameter(request, "out_trade_no");
// 由于我支付时创建了支付单,这里查询支付单if (order == null) {throw newSQLException();
}
String tradeStatus= getParameter(request, "trade_status");
// 支付成功,可能回调多次if ("TRADE_FINISHED".equals(tradeStatus) || "TRADE_SUCCESS".equals(tradeStatus)) {//查订单,已支付就不处理了
if (order.isPay()) {return;
}
String totalAmount= getParameter(request, "total_amount");
// 判断金额是否一致if (order.getAmount().compareTo(new BigDecimal(totalAmount)) != 0) {throw newAlipayApiException();
}
String tradeNo= getParameter(request, "trade_no");
// 记录交易号
}else{throw newAlipayApiException();
}
}
5 - 验签方法
private boolean aliPagePayRsaCheck(HttpServletRequest request) throwsAlipayApiException {
Map maps = new HashMap<>();
Map requestParams =request.getParameterMap();for(String name : requestParams.keySet()) {
String[] values=requestParams.get(name);
String valueStr= "";for (int i = 0; i < values.length; i++) {
valueStr= (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
maps.put(name, valueStr);
}returnAlipaySignature.rsaCheckV1(maps, alipayPublicKey, charset, signType);
}
6 - 获取参数方法 - 处理乱码问题 - 有可能 - 没具体测试,参考网上代码加的
privateString getParameter(HttpServletRequest request, String paramName) {returnhandleEncodeingError(request.getParameter(paramName));
}privateString handleEncodeingError(String str) {return newString(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
7 - 异步接口
// 注入
@RequestMapping(value = "***")public void aliPayNotify(HttpServletRequest request, HttpServletResponse response) throwsIOException {try{
payService.aliPagePayNotifyMethod(request);
response.getWriter().println("success");
}catch(Exception e) {
// 记录下日志
response.getWriter().println("fail");
}
}
至此支付宝支付接入完成,经过测试,注意参数配置.
原文:https://www.cnblogs.com/SamNicole1809/p/13564101.html