// 沙箱应用私钥(private key)
private String shaxiang_app_private_Key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDX0zMqoLHjndDPohURqt/xbQasYcFgJaSyb/kE54eUK0CeDnf3UyYKzESrgCsw2lg6UKnOVMCgjRQvmu+wUJVvtxy9Sne9fro9K2fHH8tqSevGHTDeSOdZRDrk4JBQIAJ01qoTMVJtivyWXngFMrAGz+eTegWeCKzIIKUea55ebzR6ltYL1OCAxBOxowvVKUkczljFt9wNQ3D3usG5HdKIlrDCyUIjfAf/LhNAnicOlu0XrhJST6phOZI4zzlty8NSsBC9/x8eSMevLkOCJFc78eSGJLCLX7BGT6nCXvkOSoe/70jaha5v05TJa0nRRc9/j3v/DHi06690CUTrwSPZAgMBAAECggEBAMwXZm1LJK1QEaQna7EXW0ochZEBqDHZpxMuG9ftciJFfId6uytgr7rKS626OB2sfH44xTrenoG0p4AVDdA3g/juufiusOi7LDaoEy0T/fH3EUR5Wjr5yfx6Lw885HtAqdH/JTmdDIuWQSGZF//BU5Pe7pvxnBh1a3s31iRovZvjIaCo5B1p9gIhU0lVXwHOI0QSBuiNqqZQ8ny2MXRldgOCAs9JWSFoPVEH7J4rhqqukOC4FMXg4jMkncSKFoZ71C2+Gqop7ipRxqcocdBXnlR34LX1G55OcUJTuuIBCJ/fCPgECgYEA7bPigHafk/hI11XFO1R682AE/oFV2138Kn6vT90XmJ4FrCY5LbHy5bCrn7sXVDcFIXim9cNLnbQ8HIqSE8YTMDjm5PVZnp7Fsj7bX4yOUpPuXPftXKaMJ/RYcRc4xz/4kAWurfonEgBqGFH95o3J/yDnTNe5luqP0jtsb/JB6HkCgYEA6HAy1TgXIVJ1mQ9A/R7exO/5lM6HLcwmq8CHlgLbJ6wWfjcKpuV0s1QxcqP7TxLPUxjoJKryfOhs57Da8xYYxUCOhyFNYhca8vrSxcQrjXdbIOzLRWErbPuhKy7tFLsz3MYQ5gX7ulWo7Ph1DNuuMep3FzWmUpizoPMBq9Ug/mECgYEAoVq1iyx64I/uqjWvv7l2CJUKbtTOui1PSm1aheT36oKCKTx9HdaoomBN8AypJmJ+2hyzgSqDvtak417xD/kp91b47TzbDACNBJUEhxS7W7TNXlBx0K3BgDGug34W7bIwAVvKGpEvSln7quylZIalMslcGV5qvdYVsMjjsA4LzUkCgYEA35fGnmsVfP14mGMfDh7bBrEd8jJ1wO1oqcFL7FcVAGW/QWyrN370sukjo/bwcdFTVMlZzqQPHW7sDDiJdltkOqNuN7kmWlyFlQUJKisaBjyxkcuXgkcF4hFwqTy73TxzmEWDzfIlg8qQ9FvRJSxRtjyokNAQDdZMvV/l1Kjx0yECgYARUHHCBysvQLmPQQ3vNOxDtWbieWozaA6j1CWDn/JTU1Hj3hsR+ySFjwvBNh7xUJ2fGoYuEh0GVq0lVIAGFRSkVRG2vGF00ZjLRkwHpkCfJhaVlaRMxC3R1lksFSnY6I0x7fEj5ccztXbA8uL+f2ajwDkPskAk2pz1HgG7bwBm6w==";
// 沙箱应用公钥(public key)
private String shaxiang_app_public_Key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA19MzKqCx453Qz6IVEarf8W0GrGHBYCWksm//Laknrxh0w3kjnWUQ65OCQUCACdNaqEzFSbYr8ll54BTKwBs/nk3oFngisyCClHmueXm80epbWC9TggMQTsaML1SlJHM5YxbfcDUNw97rBuR3SiJawwslCI3wH/y4TQJ4nDpbtF64SUk+qYTmSOM85bcvDUrAQvf8fHkjHry5DgiRXO/HkhiSwi1+wRk+pwl75DkqHv+9I2oWub9OUyWtJ0UXPf497/wx4tOuvdAlE68Ej2QIDAQAB";
// 沙箱支付宝公钥
private String shaxiang_alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmNGvU4bQrIGPHWNQBXZgxWDiPcrbA66Lm3jhBvKR9tdICrDVYO1Me04cQkwmiQBLfohGTzQ3b9Vb220vhDSPvyXpWz+e/jowwWXXpx0TYmJkjzgLYNpXME8x6wtIj42pMMBhOa3TtmU9bCtaWeRtkUCmJtMbMhtbVWyAGEEAe52rUx0lb73opQyJWkf1K03mRAPAsThnR9vsR3yMs8xOgSsdv/yxOsa3XywuGQ3tydqsDDXA3KgBE7/8AknzeFHRTkJChe9kLyqHpiY2LdYCC9H9YYQ8wnmBwHTRuuoqk7porX7KtIB8M6byVoKc/jGVklTKyYLISnXl4slo052lDQIDAQAB";
// 沙箱APPID
private String shaxiang_app_id = "2021000118636593";
// 沙箱应用名称
private String shaxiang_app_name = "sandbox应用:2088621956695242";
// 沙箱绑定的商家账号(PID)
private String shaxiang_app_pid = "2088621956695242";
// 沙箱URL
private String shaxiang_url = "https://openapi.alipaydev.com/gateway.do";
// 沙箱回调URL{同步}
private String shaxiang_return_url = "http://localhost:10086/api/Alipay/alipayCallBackURl";
// 沙箱字符集
private String shaxiang_char_set = "UTF-8";
// 沙箱验签方式
private String shaxiang_sign_type = "RSA2";
/**
* Description: 沙箱测试支付宝接口
*
* @author: GuoTong
* @date: 2022-12-22 21:21:04
* @return:com.alipay.api.AlipayClient
*/
@Bean
public AlipayClient getAlipayClientShaXiang() {
AlipayConfig alipayConfig = new AlipayConfig();
// 支付宝网关(固定)https://openapi.alipay.com/gateway.do
alipayConfig.setServerUrl(shaxiang_url);
// APPID 即创建应用后生成
alipayConfig.setAppId(shaxiang_app_id);
// 开发者私钥,由开发者自己生成。
alipayConfig.setPrivateKey(shaxiang_app_private_Key);
// 参数返回格式,只支持 JSON(固定)。
alipayConfig.setFormat("json");
// 编码集,支持 GBK/UTF-8
alipayConfig.setCharset(shaxiang_char_set);
// 支付宝公钥,由支付宝生成。
alipayConfig.setAlipayPublicKey(shaxiang_alipay_public_key);
// 生成签名字符串所使用的签名算法类型,目前支持 RSA2。
alipayConfig.setSignType(shaxiang_sign_type);
//构造client
try {
return new DefaultAlipayClient(alipayConfig);
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}
@RequestMapping(value = "/getAlipayPage", method = RequestMethod.GET)
@ApiOperation("获取支付页面")
public void getPayPage(HttpServletResponse resp, HttpServletRequest req) {
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
// 支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
request.setNotifyUrl("");
// 支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
request.setReturnUrl(alipayByAPPConfig.getShaxiang_return_url());
JSONObject bizContent = new JSONObject();
String out_trade_no = req.getParameter("out_trade_no");
// 东八区时间搓,精确到秒级别
long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
// 设置商家订单号
bizContent.put("out_trade_no", StringUtils.defaultIfEmpty(out_trade_no, String.valueOf(currentTime)));
bizContent.put("total_amount", 200);
bizContent.put("subject", "测试商品");
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = null;
try {
response = alipayClient.pageExecute(request);
resp.setHeader("Content-Type", "text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
String body = response.getBody();
out.print(body);
} catch (AlipayApiException | IOException e) {
e.printStackTrace();
}
assert response != null;
if (response.isSuccess()) {
log.info("调用成功");
} else {
log.error("调用失败==>{}", response);
}
}
@RequestMapping(value = "/alipayCallBackURl", method = RequestMethod.GET)
@ApiOperation("支付完成回调地址")
public ModelAndView alipayCallBackURl(HttpServletRequest req) throws UnsupportedEncodingException, AlipayApiException {
Map<String, String[]> requestParams = req.getParameterMap();
// 获取支付宝GET过来反馈信息
Map<String, String> params = new HashMap<>();
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = iter.next();
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] + ",";
}
// 乱码解决,这段代码在出现乱码时使用
valueStr = new String(valueStr.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
params.put(name, valueStr);
}
log.info("支付宝回调参数。。。。。。{}", params);
//验证签名(支付宝公钥)
boolean signVerified = AlipaySignature.rsaCheckV1(params,
alipayByAPPConfig.getShaxiang_alipay_public_key(),
alipayByAPPConfig.getShaxiang_char_set(),
alipayByAPPConfig.getShaxiang_sign_type()); // 调用SDK验证签名
ModelAndView modelAndView = new ModelAndView("/Alipay");
//验证签名通过
if (signVerified) {
// 商户订单号
String out_trade_no = new String(req.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
// 支付宝交易流水号
String trade_no = new String(req.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
// 付款金额
float money = Float.parseFloat(new String(req.getParameter("total_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
modelAndView.addObject("tradeNo", trade_no);
modelAndView.addObject("tradeStatus", "回调成功");
modelAndView.addObject("totalAmount", money);
modelAndView.addObject("outTradeNo", out_trade_no);
return modelAndView;
} else {
modelAndView.addObject("tradeNo", "验证签名未通过");
modelAndView.addObject("tradeStatus", "验证签名未通过");
modelAndView.addObject("totalAmount", "验证签名未通过");
modelAndView.addObject("outTradeNo", "验证签名未通过");
}
return modelAndView;
}
/**
* Description: 商家订单号查看订单
*
* @param out_trade_no
* @author: GuoTong
* @date: 2022-12-22 20:43:42
* @return:org.springframework.web.servlet.ModelAndView
*/
@RequestMapping(value = "/queryInfobyOutTradeNo", method = RequestMethod.GET)
@ApiOperation("商家订单号查看订单")
public ModelAndView queryAlipayInfo(@RequestParam("out_trade_no") String out_trade_no) {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", out_trade_no);
request.setBizContent(bizContent.toString());
AlipayTradeQueryResponse response = null;
ModelAndView modelAndView = new ModelAndView("/Alipay");
try {
response = alipayClient.execute(request);
// 支付宝交易号
String tradeNo = response.getTradeNo();
// 交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、TRADE_SUCCESS(交易支付成功)、TRADE_FINISHED(交易结束,不可退款)
String tradeStatus = response.getTradeStatus();
//交易的订单金额
String totalAmount = response.getTotalAmount();
// 商家订单号
String outTradeNo = response.getOutTradeNo();
modelAndView.addObject("tradeNo", tradeNo);
modelAndView.addObject("tradeStatus", tradeStatus);
modelAndView.addObject("totalAmount", totalAmount);
modelAndView.addObject("outTradeNo", outTradeNo);
} catch (AlipayApiException e) {
e.printStackTrace();
}
assert response != null;
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return modelAndView;
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<script th:src="@{/bootstrap-3.4.1-dist/js/bootstrap.js}"></script>
<link rel="stylesheet" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.css}" crossorigin="anonymous">
<meta charset="UTF-8">
<title>支付宝</title>
</head>
<body>
<div>
<table class="table table-hover">
<tr>
<td class="active">支付宝交易号</td>
<td class="success">交易状态</td>
<td class="warning">交易的订单金额</td>
<td class="danger">商家订单号</td>
</tr>
<tr>
<td class="active" th:text="${tradeNo}">支付宝交易号</td>
<td class="success" th:text="${tradeStatus}">交易状态</td>
<td class="warning" th:text="${totalAmount}">交易的订单金额</td>
<td class="danger" th:text="${outTradeNo}">商家订单号</td>
</tr>
</table>
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="100"
aria-valuemin="0" aria-valuemax="100" style="width: 100%">
<span class="sr-only">100% Complete (success)</span>
</div>
<h1 align="center">支付反馈结果</h1>
</div>
</body>
</html>
halo,大家好很开心又和大家见面了在第一篇 楠姐技术漫画:图计算的那些事 发布之后,楠姐收到了很多建议、鼓励和支持,非常感谢大家的喜欢,所以楠姐尽自己所能马不停蹄开始第二篇的创作,虽迟但到~