登录网站并注册
https://openhome.alipay.com/platform/banner.html
- 进入网站找到开发服务--沙箱
- 打开支付宝开发平台开发助手---密钥工具---密钥工具---生成密钥并上传---WINDOWS
- 下载完打开生成密钥
-
进入沙箱 把生成的公钥放进就去 就会自动生成支付宝公钥了
-
然后下载手机端沙箱(不支持苹果系统)
-
下载完成后需要登录沙箱支付宝,账号在沙箱环境中
idea实现如下
依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.49.ALL</version>
</dependency>
引入Controller
package com.jk.controller;
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.jk.dao.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@Controller
public class PayController {// 应用id
@Value("${alipay_test.APP_ID}")
private String APP_ID;
// 应用私钥
@Value("${alipay_test.APP_PRIVATE_KEY}")
private String APP_PRIVATE_KEY;// 编码
@Value("${alipay_test.CHARSET}")
private String CHARSET;// 支付宝公钥
@Value("${alipay_test.ALIPAY_PUBLIC_KEY}")
private String ALIPAY_PUBLIC_KEY;// 接口路径
@Value("${alipay_test.GATEWAY_URL}")
private String GATEWAY_URL;// 格式
@Value("${alipay_test.FORMAT}")
private String FORMAT;
//签名方式
@Value("${alipay_test.SIGN_TYPE}")
private String SIGN_TYPE;// 异步请求,需要使用域名
@Value("${alipay_test.NOTIFY_URL}")
private String NOTIFY_URL;// 同步请求,不需要域名
@Value("${alipay_test.RETURN_URL}")
private String RETURN_URL;
@RequestMapping(value = {"/toBuy","/"})
private String toBuy(){
return "buy";
}// @RequestMapping("/alipay_buy")
// public void alipay_buy(Book book, HttpServletResponse httpResponse) throws IOException {
//
// Random r = new Random();
// //实例化客户端,填入所需参数
// AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
// AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
// //在公共参数中设置回跳和通知地址
// request.setReturnUrl(RETURN_URL);
// request.setNotifyUrl(NOTIFY_URL);
//
// //商户订单号,商户网站订单系统中唯一订单号,必填
// //生成随机Id 商户订单号,商户网站订单系统中唯一订单号,必填
// String out_trade_no = UUID.randomUUID().toString();
// //付款金额,必填
String total_amount = Integer.toString(r.nextInt(9999999) + 1000000);
// //订单名称,必填
String subject = "奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
// //商品描述,可空
//
// // 可填,延时时间,如果填了在此时间段内没有付款则无效,这里的时间支付宝界面上减2分钟
// long etime1 = System.currentTimeMillis() + 3 * 60 * 1000;//延时函数,单位毫秒,这里是延时了3分钟
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
// String time_expire = df.format(new Date(etime1));
// String num = df.format(new Date());
String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
// request.setBizContent("{\"out_trade_no\":\"" + num + "\","
// + "\"total_amount\":\"" + book.getPrice() + "\","
// + "\"subject\":\"" + book.getBookName() + "\","
// + "\"body\":\"" + book.getDesc() + "\","
// + "\"time_expire\":\"" + time_expire + "\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// 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();
// }
@RequestMapping("/alipay_ajax_buy")
@ResponseBody
public String alipay_ajax_buy(@RequestParam("bookName") String bookName,@RequestParam("desc")String desc,@RequestParam("price")Double price, HttpServletResponse httpResponse,HttpServletRequest httpServletRequest) throws IOException {AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//在公共参数中设置回跳和通知地址
request.setReturnUrl(RETURN_URL);
request.setNotifyUrl(NOTIFY_URL);//商户订单号,商户网站订单系统中唯一订单号,必填
//付款金额,必填
//订单名称,必填
//商品描述,可空// 可填,延时时间,如果填了在此时间段内没有付款则无效,这里的时间支付宝界面上减2分钟
long etime1 = System.currentTimeMillis() + 3 * 60 * 1000;//延时函数,单位毫秒,这里是延时了3分钟
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
String time_expire = df.format(new Date(etime1));
String num = df1.format(new Date());
request.setBizContent("{\"out_trade_no\":\"" + num + "\","
+ "\"total_amount\":\"" + price + "\","
+ "\"subject\":\"" + bookName + "\","
+ "\"body\":\"" + desc + "\","
+ "\"time_expire\":\"" + time_expire + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
String form = "";
try {
form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
} catch (AlipayApiException e) {
e.printStackTrace();
}
return form;
}@RequestMapping(value = "/returnUrl", method = RequestMethod.GET)
public String returnUrl(HttpServletRequest request, HttpServletResponse response)
throws IOException, AlipayApiException {
System.out.println("=================================同步回调=====================================");// 获取支付宝GET过来反馈信息
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 = (String) iter.next();
String[] values = (String[]) 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("utf-8"), "utf-8");
params.put(name, valueStr);
}System.out.println(params);//查看参数都有哪些
boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 调用SDK验证签名
//验证签名通过
if (signVerified) {
// 商户订单号
String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");// 支付宝交易号
String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");// 付款金额
String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");System.out.println("商户订单号=" + out_trade_no);
System.out.println("支付宝交易号=" + trade_no);
System.out.println("付款金额=" + total_amount);//支付成功,修复支付状态
// payService.updateById(Integer.valueOf(out_trade_no));
return "success";//跳转付款成功页面
} else {
return "fail";//跳转付款失败页面
}}
}
yml配置
alipay_test:
ALIPAY_PUBLIC_KEY: 支付宝公钥
APP_ID: 支付宝APPID APP_PRIVATE_KEY: 应用私钥
CHARSET: UTF-8
FORMAT: JSON
GATEWAY_URL: https://openapi.alipaydev.com/gateway.do
NOTIFY_URL: http://localhost:8080/notifyUrl
RETURN_URL: http://localhost:8080/returnUrl
SIGN_TYPE: RSA2
实体类(后面换成自己的)
package com.jk.dao;
/**
* Created by dell on 2020/4/6.
*/
public class Book {
private String bookName;
private Double price;
private String desc;public String getBookName() {
return bookName;
}public void setBookName(String bookName) {
this.bookName = bookName;
}public Double getPrice() {
return price;
}public void setPrice(Double price) {
this.price = price;
}public String getDesc() {
return desc;
}public void setDesc(String desc) {
this.desc = desc;
}
}
购买页面(给的死值,后期自己换)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>买书</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"></script>
</head>
<body>
<div id="app"></div>
<table class="table">
<caption>书店</caption>
<thead>
<tr>
<th>名称</th>
<th>价格</th>
<th>简介</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><span na>java</span></td>
<td>110元</td>
<td>good book</td>
<td>
<button type="button" οnclick="buyItem('java','110','good book')">购买</button>
</td>
</tr>
<tr>
<td>Python</td>
<td>100元</td>
<td>好书</td>
<td>
<button type="button" οnclick="buyItem('python','100','好书')">购买</button>
</td>
</tr>
</tbody>
</table>
<script>
function buyItem(bookName,price,desc) {
var list={"bookName": bookName, "price": price, "desc": desc};
$.ajax({
//请求方式
type: "POST",
//请求地址
url: "/alipay_ajax_buy",
//数据,json字符串
data: list,
success: function (result) {
$("#app").append(result)
},
//请求失败,包含具体的错误信息
error: function (e) {
console.log(e.status);
console.log(e.responseText);
}
});
}
</script></body>
</html>
购买成功或失败跳转
error(失败)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购买失败</title>
</head>
<body>
<h1>购买失败</h1>
</body>
</html>
succes(成功)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>购买成功</title>
</head>
<body>
<h1>购买成功</h1>
</body>
</html>