一、前期准备
1、支付宝开放平台沙箱账号,用于测试
应用公钥需要使用支付宝第三方工具生成,最好在本地备份好,公钥以及配对的密钥
2、javasdk的maven仓库
3、内网穿透,使内网能够被 支付宝的服务器访问
支付成功后支付宝需要回调咱们服务器的端口,必须保证自己的服务器
能被外网
访问
穿透教程
二、流程图
主要流程:
用户
下单请求商户系统
商户系统
构造支付请求,发送给支付宝
支付宝
展示支付信息,用户
支付- 支付结果由
支付宝
回调给商户
创建好的接口 商户
根据支付结果业务操作,并在最后将订单信息返回给用户
三、代码编写
1、依赖导入
springboot thymeleaf alipa-sdk-java
![在这里插入图片描述](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL2p3LXN0YXIvbXlGaWd1cmViZWQvbWFzdGVyL2ltZy8yMDE5MTIxMTE0NDc1OS5wbmc?x-oss-process=image/format,png)
2、创建请求公共参数的类 AlipayConfig
public class AlipayConfig {
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号,开发时使用沙箱提供的APPID,生产环境改成自己的APPID
public static String APP_ID = "2016101600700789"; //沙箱的
//public static String APP_ID = "2019120669659570";
// 商户私钥,您的PKCS8格式RSA2私钥
public static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCFOj7P1DzSVD2j/DxZrLg3i9siUn47m6xL2yaZZprDNb3UjyNEt31B/q4hdhZ7StJVBx9AEfSsceXzOizbRJgdAboR6XSz66bE9pI3k/Ef2TmSc4v+cEysCKF/Mg4t8Y+uCWpOak+r/K4ZNBla2u45gU1vPcvOzf/uvJiTXmSYbBndKUMCICkF0RbHLOjhMFN8a0Nu0IiCSuGFl8iMSnL9czKnGWT6k5luwHsvxh0ei+lW2V9NZyP56reSylhO8RmOihsDKep7Y4JdNRyGNGzjkiaqGKzbcGU2a/Zx9ztSmo7tzOLVK3eDZEiOX4nBqa0HqczpYEg7S+kYwkIAXT7nAgMBAAECggE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QDEawoukIcpMNFigTpmPp+R033xZAz9CyJCVuX8vfgfTMHsQNfngh5FNtCWMpIcrVei6cvCWD+cJqeVuV6LEbUxvqK+OY1bNoZnhnN1K6OjcyziYK//TSf0lHzBy7Uiy3l06youlRt/in3LualY3EDdtx/fUe4Lj244Nj2Cm3pQGQKBgQCiZFg1ZjMN0cZr/L8s+gGGPjlwdtWpBx33bmpncTqqWtxXkS91hiH/OvpFOKiC59ZZHLrmguKFFvNEQC/C0yNpgFsEDkM1pH6ZRDeb1RlBKvQUboBfLGN5PcFE1AQtV1LrLo22zPlPLQE7qx4KbojHsLlAsdlHu2fJeCtHTC8AwwKBgAWnBu38HseirgPlsCaNGs55haBSvsTxcxTxp0C0vtU3wkToQM5awkA/qZUI84d7vY/kpoZ7P6lgu2XlpcDey5YY5FoREe5UDV4hUmVG0ji78ci5+5afQ80YENcK47WSoXYp5lbc16O9+ozIVQVHoV5ADNzCYNVYVVWsyexeSRDRAoGBAJEa+g3rfVEKDzCXA+EgkTDphwEJyVH2csV9cGFLUnPv7fJgsTCj1QSE6PYHddCChKDBuJ5Yw1VhmqBch3Y4bcyV36hybO+vqzUP3AW1LVeu/NAhd3qRULoaiG5dhKJ28RWMZWr7IP1+xA58y/TbUpHdBhvOmvzopiXKe0SgWeSc";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
public static String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkE5K/L+l166zCpgLZ5EQRf9f32lNII8xtIJzTiBJaRgYAb2HnW3ZAEnpLNeWl8s5PxWsa6tNtqlrjSG8smNHDmbgF9pF1pLjhbCoe1tMizeuUvX6wypnPH3k2dM5Z4ffyLyvXw8rqO28CUegNLnDtc7U6dq5YhSlntTph3DB5EKL6iNSCs8PtblKwAA108I5beV8lvV75QkkXMU1jdpbA013swBAptyLxTsb/NoUB4eUmjr+uwrhavHoWbolFID2v9MuZ0GYb7TpiBEQSns+VaHi7pJpYXsL+bwSoteKAD+cG2vH8NI5HggMn3eXH6hOFtZ5jsm4NOZ0WgKOjo38UwIDAQAB";
//public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhvCnEvUMjsa7N1CybrtIdMY74o1JVyspZqv5Q5yj/8uyaE5LSG+7hFhQZpkJgHFNdKJsICfEKpK95MoKAm9uOV4+p7R7i7FMjVKh8yX6bf4auQXQLa3KeTJ/GSQpw1Z+5cSivyzBb2v+d4dt9MgJj04j6cCo2g9wjYsQ/wpRn/BMdDtA7nUaKfa15gJLz4q/1MYlIjALjnMeXIJXqZdDvBLuQ3JMEAgv6jfFxbU6U71L6ayA1OFdc1RFUrz4SXryOk9Ogoz77/DWCF4fcCZenx/zsBhTUXbl5GDy6/KFeMoU2WkVhFC9N/kG5aAiUTihjHQI3w3APAGAbJZkFJg/iwIDAQAB";
// 异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://gojw.xyz/notify";
// 同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问(其实就是支付成功后返回的页面)
public static String return_url = "http://gojw.xyz/return";
// 签名方式
public static String sign_type = "RSA2";
// 字符编码格式
public static String CHARSET = "utf-8";
// 支付宝网关,
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 日志输出地方
public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3、支付控制层PayController
@Controller
public class PayController {
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
@RequestMapping("/pay")
@ResponseBody
public String payController(String WIDout_trade_no,String WIDsubject,String WIDtotal_amount,String WIDbody) {//创建支付请求
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商户订单号,商户网站订单系统中唯一订单号,必填
String out_trade_no = WIDout_trade_no;
//付款金额,必填
String total_amount =WIDtotal_amount;
//订单名称,必填
String subject = WIDsubject;
//商品描述,可空
String body = WIDbody;
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//请求
String form = "";
try {
form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
return form;
}
@ResponseBody
@RequestMapping("/dingdanLook")
public String dingdanChaxun(){//查看订单信息
//设置请求参数
AlipayTradeQueryRequest alipayTradeQueryRequest = new AlipayTradeQueryRequest();
alipayTradeQueryRequest.setBizContent("{" +
" \"out_trade_no\":\"201503s240440010101002\" " +//商户订单号
"}" );
String body="";
try {
body = alipayClient.execute(alipayTradeQueryRequest).getBody();
} catch (AlipayApiException e) {
e.printStackTrace();
}
return body;
}
@ResponseBody
@PostMapping("/notify") //异步验签接口
public String notifyer(HttpServletRequest request) throws AlipayApiException {
boolean b = mysignVerified(request);
if (b){
System.out.println("异步验签成功");
//修改数据库订单状态
return "success";
}else {
System.out.println("异步验签失败");
return "failure";
}
}
@GetMapping(value = "/return") //同步验签接口
public String returner(HttpServletRequest request) throws AlipayApiException {
boolean b = mysignVerified(request);
if (b){
System.out.println("同步验签成功-跳转到成功后页面");
//修改数据库订单状态
return "/success";
}else {
System.out.println("同步验签失败-跳转到充值页面让用户重新充值");
return "/error";
}
}
/*
* 验签方法
* */
public boolean mysignVerified(HttpServletRequest request) throws AlipayApiException {
Map<String, String> params = new HashMap<String, String>();
Map<String, String[]> requestParams = request.getParameterMap();
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] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
params.put(name, valueStr);
}
//String out_trade_no = request.getParameter("out_trade_no");// 商户订单号
return AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type); //调用SDK验证签名
}
@RequestMapping("/")
public String toTest(){
return "index";
}
}
4、前台页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/pay" method="post">
订单号:<input type="text" name="WIDout_trade_no" required><br/>
订单名称:<input type="text" name="WIDsubject" required><br/>
付款金额:<input type="text" name="WIDtotal_amount" required><br/>
WIDbody:<input type="text" name="WIDbody"><br/>
<input type="submit" value="下单"> <input type="reset" value="重置">
</form>
</body>
</html>
来源:CSDN
作者:食之无味,弃之可惜
链接:https://blog.csdn.net/qq_39846820/article/details/103491718