1、登录支付宝开放平台,获取支付宝对接参数
参考地址:支付宝开放平台
1)进入控制台,创建应用,得到应用ID。
2)进入应用,开发设置,设置接口加密方式,得到应用私钥、支付宝公钥。
3)进入应用,产品绑定,绑定支付产品。
2、java代码的静态配置文件中配置支付宝对接参数以及回调地址,并读取到代码中
/**
* 支付宝配置参数
*/
@Configuration
public class AlipayConfig {
//应用id
public static String appId;
//应用私钥
public static String privateKey;
//支付宝公钥
public static String alipayPublicKey;
//请求网关地址
public static String alipayGateway;
// 异步接收地址
public static String notifyUrl;
// 同步跳转地址
public static String returnUrl;
// 编码格式
public static String input_charset_utf = "utf-8";
//编码格式
public static String input_charset_gbk = "GBK";
// 签名方式
public static String sign_type_md5 = "MD5";
//签名方式
public static String sign_type_rsa2 = "RSA2";
//字符串格式
public static String format="json";
@Value("${alipay.appId}")
public void setAppId(String appId) {
AlipayConfig.appId = appId;
}
@Value("${alipay.privateKey}")
public void setPrivateKey(String privateKey) {
AlipayConfig.privateKey = privateKey;
}
@Value("${alipay.alipayPublicKey}")
public void setAlipayPublicKey(String alipayPublicKey) {
AlipayConfig.alipayPublicKey = alipayPublicKey;
}
@Value("${alipay.alipayGateway}")
public void setAlipayGateway(String alipayGateway) {
AlipayConfig.alipayGateway = alipayGateway;
}
@Value("${alipay.notifyUrl}")
public void setNotifyUrl(String notifyUrl) {
AlipayConfig.notifyUrl = notifyUrl;
}
@Value("${alipay.returnUrl}")
public void setReturnUrl(String returnUrl) {
AlipayConfig.returnUrl = returnUrl;
}
}
3、查看文档小程序文档 - 支付宝文档中心,根据支付宝提供的api实现接口对接。
/**
* 支付宝接口对接
*/
public class AlipayPayUtil {
/**
* 发起支付请求
* 统一收单下单并支付页面接口
*
* @return
*/
public static AjaxResult alipayPay(String orderNum, BigDecimal price, String title){
try{
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGateway, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.input_charset_utf, AlipayConfig.alipayPublicKey, AlipayConfig.sign_type_rsa2);
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址,仅支持http/https,公网可访问
request.setNotifyUrl(AlipayConfig.notifyUrl);
//同步跳转地址,仅支持http/https
request.setReturnUrl(AlipayConfig.returnUrl);
/******必传参数******/
JSONObject bizContent = new JSONObject();
//商户订单号,商家自定义,保持唯一性
bizContent.put("out_trade_no",orderNum);
//支付金额,最小值0.01元
bizContent.put("total_amount", price);
//订单标题,不可使用特殊符号
bizContent.put("subject",title);
//订单描述
bizContent.put("body", title);
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(bizContent.toString());
AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
if (response.isSuccess()) {
return AjaxResult.success(response.getBody());
} else {
return AjaxResult.error("调用失败");
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("调用失败");
}
}
/**
* 统一收单线下交易查询接口
* @param tradeNo 支付宝交易号
*/
public AjaxResult alipayQuery(String tradeNo){
try{
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGateway, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.input_charset_utf, AlipayConfig.alipayPublicKey, AlipayConfig.sign_type_rsa2);
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", tradeNo);
request.setBizContent(bizContent.toString());
AlipayTradeQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return AjaxResult.success(response.getBody());
} else {
return AjaxResult.error("调用失败");
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("调用失败");
}
}
/**
* 统一收单交易关闭接口
* @param tradeNo 支付宝交易号
*/
public AjaxResult alipayClose(String tradeNo){
try{
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGateway, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.input_charset_utf, AlipayConfig.alipayPublicKey, AlipayConfig.sign_type_rsa2);
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", tradeNo);
request.setBizContent(bizContent.toString());
AlipayTradeCloseResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return AjaxResult.success(response.getBody());
} else {
return AjaxResult.error("调用失败");
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("调用失败");
}
}
/**
* 统一收单交易退款请求
* 全额退款,out_request_no 选填。
* 部分退款,out_request_no 必传。
* @param tradeNo 支付宝交易号
*/
public AjaxResult alipayRefund(String tradeNo,BigDecimal price){
try{
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGateway, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.input_charset_utf, AlipayConfig.alipayPublicKey, AlipayConfig.sign_type_rsa2);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", tradeNo);
bizContent.put("refund_amount", price);
bizContent.put("out_request_no", "T"+Seq.getOrderNum());
request.setBizContent(bizContent.toString());
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return AjaxResult.success(response.getBody());
} else {
return AjaxResult.error("调用失败");
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("调用失败");
}
}
/**
* 统一收单交易退款查询
* @param tradeNo 支付宝交易号
*/
public AjaxResult alipayRefundQuery(String tradeNo){
try{
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.alipayGateway, AlipayConfig.appId, AlipayConfig.privateKey, AlipayConfig.format, AlipayConfig.input_charset_utf, AlipayConfig.alipayPublicKey, AlipayConfig.sign_type_rsa2);
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", tradeNo);
bizContent.put("out_request_no","T"+ Seq.getOrderNum());
request.setBizContent(bizContent.toString());
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
return AjaxResult.success(response.getBody());
} else {
return AjaxResult.error("调用失败");
}
}catch (Exception e){
e.printStackTrace();
return AjaxResult.error("调用失败");
}
}
}
4、回调接口处理,支付宝回调接口分为同步回调、异步回调,同步接口,业务处理后,可直接跳转至配置的网页,并返回处理信息
/**
* 支付宝回调地址-同步通知页面路径
*
* @return
*/
@RequestMapping(value = "/returnUrl")
public void returnUrl(HttpServletRequest request,HttpServletResponse response) throws IOException {
try {
//获取支付宝GET过来反馈信息
Map<String, String> params = new HashMap<String, String>();
Map requestParams = request.getParameterMap();
for (Iterator 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] + ",";
}
params.put(name, valueStr);
}
// 获取支付宝的通知返回参数
String out_trade_no = request.getParameter("out_trade_no");// 商户订单号
String trade_no = request.getParameter("trade_no");// 支付宝交易号
String seller_id=request.getParameter("seller_id");// 收款支付宝账号对应的支付宝唯一用户号
String total_amount=request.getParameter("total_amount");// 交易金额
log.info("returnUrl支付宝订单交易:[商户订单号]"+out_trade_no+",[支付宝交易号trade_no]"+trade_no+",[收款支付宝账号seller_id]"+seller_id+",[交易金额total_amount]"+total_amount);
//调用SDK验证签名
boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipayPublicKey, AlipayConfig.input_charset_utf, AlipayConfig.sign_type_rsa2);
if(signVerified) {
//修改订单状态,根据商户订单号查询订单信息
response.sendRedirect(this.alerturl + "?type=ZF&status=300");
}else {
log.error("验签失败,请联系管理员解决");
response.sendRedirect(this.alerturl + "?type=ZF&status=303");
}
} catch (Exception e) {
e.printStackTrace();
log.error("订单修改失败,请联系管理员解决");
response.sendRedirect(this.alerturl + "?type=ZF&status=304");
}
}
/**
* 支付宝回调地址-异步通知页面路径
*
* @return
*/
@PostMapping(value = "/notifyUrl")
public String notifyUrl(HttpServletRequest request) {
try {
// 获取支付宝GET过来反馈信息
Map<String, String> params = new HashMap<String, String>();
Map requestParams = request.getParameterMap();
for (Iterator 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] + ",";
}
params.put(name, valueStr);
}
// 获取支付宝的通知返回参数
String out_trade_no = request.getParameter("out_trade_no");// 商户订单号
String trade_no = request.getParameter("trade_no");// 支付宝交易号
String seller_id=request.getParameter("seller_id");// 收款支付宝账号对应的支付宝唯一用户号
String total_amount=request.getParameter("total_amount");// 交易金额
String trade_status = request.getParameter("trade_status");// 交易状态
log.info("notifyUrl异步回调,支付宝订单交易:[商户订单号]"+out_trade_no+",[支付宝交易号trade_no]"+trade_no+",[收款支付宝账号seller_id]"+seller_id+",[交易金额total_amount]"+total_amount+",[交易状态]"+trade_status);
//调用SDK验证签名
boolean signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.alipayPublicKey, AlipayConfig.input_charset_utf, AlipayConfig.sign_type_rsa2); //调用SDK验证签名
if(signVerified) {
if(trade_status.equals("TRADE_FINISHED")||trade_status.equals("TRADE_SUCCESS")){
//根据商户订单号查询订单信息处理
return "success";
}else {
return "fail";
}
}else {
//验证失败
log.error("验签失败,请联系管理员解决");
return "fail";
}
} catch (Exception e) {
e.printStackTrace();
log.error("订单修改失败,请联系管理员解决");
return "fail";
}
}
注意:
如果框架接口访问有权限限制,需要放开支付宝异步回调地址,否则无法访问