Java Springboot整合支付宝接口的教程详解
介绍:
Java Springboot是当前广泛使用的Java开发框架之一,兼容了Spring框架的优势并整合了大量解决方案,易用易扩展,本文将详细讲解如何在Java Springboot应用中整合支付宝接口。
准备工作:
1. 开通支付宝开放平台账号:
首先访问 支付宝开放平台官方网站,进行开发者注册,获取AppID、公钥和私钥。
2. 将支付宝SDK导入项目:
对于Java Springboot应用,可以通过以下方式引入支付宝SDK:
maven:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
gradle:
dependencies {
compile 'com.alipay.sdk:alipay-sdk-java:4.0.0.RELEASE'
}
整合过程:
1. 配置支付宝SDK参数:
使用IDE打开项目中的application.properties文件,配置以下参数:
# 支付宝APPID
alipay.appid=APPID
# 支付宝公钥
alipay.publicKey=PUBLIC_KEY
# 支付宝私钥
alipay.privateKey=PRIVATE_KEY
# 支付宝网关
alipay.gatewayUrl=https://openapi.alipay.com/gateway.do
# 与支付宝交互时使用的字符集
alipay.charset=utf-8
# 支付宝签名类型
alipay.signType=RSA2
2. 实现支付宝接口调用:
在Springboot应用中使用支付宝SDK主要通过如下步骤完成:
创建AlipayClient实例:
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
AlipayClient alipayClient = new DefaultAlipayClient(alipayCard.getGatewayUrl(), alipayCard.getAppid(), alipayCard.getPrivateKey(), "json", "UTF-8", alipayCard.getPublicKey(), "RSA2");
创建AlipayRequest实例:
import com.alipay.api.request.AlipayTradePagePayRequest;
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(returnUrl);
alipayRequest.setNotifyUrl(notifyUrl);
alipayRequest.setBizContent("{" +
"\"out_trade_no\":\"" + orderId + "\"," +
"\"total_amount\":\"" + orderAmount + "\"," +
"\"subject\":\"" + orderName + "\"," +
"\"body\":\"" + orderDesc + "\"," +
"\"product_code\":\"FAST_INSTANT_TRADE_PAY\"" +
"}");
调用AlipayClient的execute方法完成接口调用:
String form = alipayClient.pageExecute(alipayRequest).getBody();
3. 实现支付宝接口验签:
支付宝接口返回的数据包括了大量签名信息,需要对其进行验签操作,检验数据的完整性及防篡改。在Springboot应用中使用支付宝SDK可以根据以下步骤完成验签操作:
import com.alipay.api.AlipayApiException;
import com.alipay.api.SignItem;
import com.alipay.api.SignUtils;
// 返回结果验签
Map<String, Object> resultMap = new HashMap<>();
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
resultMap.put(entry.getKey(), entry.getValue()[0]);
}
boolean signVerified = SignUtils.verify(getSignItems(resultMap), alipayCard.getPublicKey(), alipayCard.getCharset(), alipayCard.getSignType());
private List<SignItem> getSignItems(Map<String, Object> dataMap) {
List<SignItem> signItemList = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
if (entry.getValue() != null && StringUtils.isNotBlank(entry.getValue().toString())) {
signItemList.add(new SignItem(entry.getKey(), entry.getValue().toString()));
}
}
return signItemList;
}
示例:
示例1:支付宝接口调用
@GetMapping("/pay")
public void pay(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException {
// 构造支付宝支付请求
AlipayClient alipayClient = new DefaultAlipayClient(alipayCard.getGatewayUrl(), alipayCard.getAppid(), alipayCard.getPrivateKey(), "json", "UTF-8", alipayCard.getPublicKey(), "RSA2");
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(returnUrl);
alipayRequest.setNotifyUrl(notifyUrl);
alipayRequest.setBizContent("{" +
"\"out_trade_no\":\"" + orderId + "\"," +
"\"total_amount\":\"" + orderAmount + "\"," +
"\"subject\":\"" + orderName + "\"," +
"\"body\":\"" + orderDesc + "\"," +
"\"product_code\":\"FAST_INSTANT_TRADE_PAY\"" +
"}");
// 提交发起支付请求
String form = alipayClient.pageExecute(alipayRequest).getBody();
PrintWriter out = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
out.write(form);
out.flush();
out.close();
}
示例2:支付宝接口验签
@PostMapping("/notify")
public void notify(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException {
// 验签操作
Map<String, Object> resultMap = new HashMap<>();
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
resultMap.put(entry.getKey(), entry.getValue()[0]);
}
boolean signVerified = SignUtils.verify(getSignItems(resultMap), alipayCard.getPublicKey(), alipayCard.getCharset(), alipayCard.getSignType());
if (signVerified) {
// 验签成功
String trade_status = request.getParameter("trade_status");
if (StringUtils.isNotBlank(trade_status) && trade_status.equals("TRADE_SUCCESS")) {
// 订单支付成功处理
// TODO
}
PrintWriter out = response.getWriter();
out.write("success");
out.flush();
out.close();
} else {
// 验签失败
PrintWriter out = response.getWriter();
out.write("fail");
out.flush();
out.close();
}
}
private List<SignItem> getSignItems(Map<String, Object> dataMap) {
List<SignItem> signItemList = new ArrayList<>();
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
if (entry.getValue() != null && StringUtils.isNotBlank(entry.getValue().toString())) {
signItemList.add(new SignItem(entry.getKey(), entry.getValue().toString()));
}
}
return signItemList;
}
以上是Java Springboot整合支付宝接口的教程详解,如果还有问题,可以参考支付宝开放平台提供的详细接口文档进行解决。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java Springboot整合支付宝接口的教程详解 - Python技术站