前言
提示:SpringBoot集成支付宝沙箱支付简单版 后续可根据实际情况结合其它功能
一、注册支付宝沙箱
直达链接:登录 - 支付宝
二、使用步骤
1.引入pom文件
<!-- 支付宝支付依赖 -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.31.56.ALL</version>
</dependency>
2.配置yml
代码如下(示例):
alipay:
app-id: APPID
private-key: 你的私有密钥(应用私钥)
alipay-public-key: 公有密钥(支付宝公钥)
server-url: https://openapi-sandbox.dl.alipaydev.com/gateway.do
charset: utf-8
sign-type: RSA2
notify-url: http://mkcaa9.natappfree.cc/alipay/notify(填写你的地址+/alipay/notify)
return-url: http://mkcaa9.natappfree.cc/alipay/returnUrl(支付成功回调的地址)
私有密钥和公有密钥在开发信息下公钥模式查看,地址不懂得先看3配置地址映射
3.配置地址映射 最好映射一下本地地址 可以公共访问
直达链接-》NATAPP-内网穿透 基于ngrok的国内高速内网映射工具
免费注册实名-》购买一个免费的
配置端口映射
下载 解压 配置
双击启动start.bat
三、Java类
1. AliPayController.Java
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xdong.shopping.dao.pojo.ShoppingDd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/alipay")
public class AliPayController {
@Value("${alipay.app-id}")
private String appId;
@Value("${alipay.private-key}")
private String privateKey;
@Value("${alipay.alipay-public-key}")
private String alipayPublicKey;
@Value("${alipay.server-url}")
private String serverUrl;
@Value("${alipay.charset}")
private String charset;
@Value("${alipay.sign-type}")
private String signType;
@Value("${alipay.notify-url}")
private String notifyUrl;
@Value("${alipay.return-url}")
private String returnUrl;
private static final String FORMAT = "JSON";
//vvvyyv9548@sandbox.com 支付邮箱
@GetMapping("/pay") //subject=xxx&traceNo=xxx&totalAmount=xxx
public void pay(AliPay aliPay, HttpServletResponse httpResponse) throws Exception {
// 1. 创建Client,通用SDK提供的Client,负责调用支付宝的API
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId,
privateKey, FORMAT, charset, alipayPublicKey, signType);
// 2. 创建 Request并设置Request参数
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 发送请求的 Request类
request.setNotifyUrl(notifyUrl);
request.setReturnUrl(returnUrl);
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", aliPay.getTraceNo()); // 我们自己生成的订单编号
bizContent.put("total_amount", aliPay.getTotalAmount()); // 订单的总金额
bizContent.put("subject", aliPay.getSubject()); // 支付的名称
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); // 固定配置
request.setBizContent(bizContent.toString());
// 执行请求,拿到响应的结果,返回给浏览器
String form = "";
try {
form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
httpResponse.setContentType("text/html;charset=" + charset);
httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
httpResponse.getWriter().flush();
httpResponse.getWriter().close();
}
@PostMapping("/notify") // 注意这里必须是POST接口
public String payNotify(HttpServletRequest request) throws Exception {
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
System.out.println("=========支付宝异步回调========");
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
for (String name : requestParams.keySet()) {
params.put(name, request.getParameter(name));
// System.out.println(name + " = " + request.getParameter(name));
}
String sign = params.get("sign");
String content = AlipaySignature.getSignCheckContentV1(params);
boolean checkSignature = AlipaySignature.rsa256CheckContent(content, sign, alipayPublicKey, "UTF-8"); // 验证签名
// 支付宝验签
if (checkSignature) {
// 验签通过 可做自己需要的操作
System.out.println("交易名称: " + params.get("subject"));
System.out.println("交易状态: " + params.get("trade_status"));
System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
System.out.println("商户订单号: " + params.get("out_trade_no"));
System.out.println("交易金额: " + params.get("total_amount"));
System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
System.out.println("买家付款时间: " + params.get("gmt_payment"));
System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
}
}
return "success";
}
@GetMapping("/returnUrl")
public String returnUrl() {
return "支付成功了";
}
}
1. AliPay.Java
import lombok.Data;
@Data
public class AliPay {
private String traceNo;// 我们自己生成的订单编号
private double totalAmount;// 订单的总金额
private String subject;// 支付的名称
private String alipayTraceNo;
}
总结
以上就是SpringBoot集成支付宝沙箱支付的极速版,不懂得可以在问下度娘,也可以留言。