支付宝提供了测试环境
支付宝沙箱:先登录开放平台 https://openhome.alipay.com/develop/manage
这边能拿到调用支付宝接口所需要的所有参数
代码部分:
首先引入依赖:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.35.37.ALL</version>
</dependency>
这边统一整理到main方法里面
package org.example;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
getAliCodeurl();
}
//--------------------------支付宝-----------------------
//需要的字段
//支付宝请求的地址:https://openapi-sandbox.dl.alipaydev.com/gateway.do
//(应用/小程序/公众号)appid:9021000140694217
//商户mchid:2088721045400254
//商户私钥:MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjVNfrCpJYS0h4aqsyVXth9q6IRa9sAPyPa5yBLZI7QtR57jfckjS01/NIIaCV6sW6JJw/sxqoHwRwl7D0mbaExSdHXgrYbO4FoshfSi07SVi3X2Yp8k7g1GZ5sfZhfy3bA835CiXbpjomQk+8DF30AdPsT8Sgdgz7dR0eqTuDmuEF/eLPG18088rKoSvUa1vhxnqjlW6Aermbob3Wl+akAGCsy2K/JYpDtZH2GUqY5RX16z/BDNhABnBFp/SOLHf1cHzvQWx8cLCqgFG7xzyipOjJzMVXYLCeFJSgknfsLdJ0tGmKzXzdriM3V5CtrJUozYz++83n+VWDyaY8rEonAgMBAAECggEB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mXUQOPrjfXihSi4WD7XG/9Q4ZS2M2vPIh9DT4qKXTwtY7Ji1JzwXBgAdkMcPGz6Ad1udveQi9QrXPAm21ykvEMapHW/q87oGg9vGJDPTujHbjazDBVwI1FGYp26xuqu5nxrwk
//支付宝公钥:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuw+rYJnRhkyGPr/VE9Znp8tgeVw5EFRQ3/38O6+fm31G9Yb3ewj2bRQ2WAk2uitX0tvMTqT33DUsSm2++Q0BjTT03Vst5atCjjI7Un4opMyIy0qgkNq6BaG0FqkXHZpCP0Pk7w6quQGG6qk75skH7ByqKZIXzhLiZyZy7/hv7+s52NVrwxwxuK9WpJmOzvH+QLDr5ZKdTyqDQH4IQa4hZGFfQhZ/ZCAGCixGQrV3/Ypx7Ig1eUG1kSvy4cn1JDvHbomdDKRdj8LNMXxBu6BvTtTklQfkDorilETXVv+AWuUfjExp7r0psAAOk+7hGZLklqfZ6l8rGhqky3YB/LfJswIDAQAB
//--------------------------支付宝获取二维码url-----------------------
//获取签证请求
public static AlipayClient createAlipayClient(){
String gataliUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
String appid = "9021000140694217";
String mchid = "2088721045400254";
String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjVNfrCpJYS0h4aqsyVXth9q6IRa9sAPyPa5yBLZI7QtR57jfckjS01/NIIaCV6sW6JJw/sxqoHwRwl7D0mbaExSdHXgrYbO4FoshfSi07SVi3X2Yp8k7g1GZ5sfZhfy3bA835CiXbpjomQk+8DF30AdPsT8Sgdgz7dR0eqTuDmuEF/eLPG18088rKoSvUa1vhxnqjlW6Aermbob3Wl+akAGCsy2K/JYpDtZH2GUqY5RX16z/BDNhABnBFp/SOLHf1cHzvQWx8cLCqgFG7xzyipOjJzMVXYLCeFJSgknfsLdJ0tGmKzXzdriM3V5CtrJUozYz++83n+VWDyaY8rEonAgMBAAECggEBAIaE4jAaz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fXihSi4WD7XG/9Q4ZS2M2vPIh9DT4qKXTwtY7Ji1JzwXBgAdkMcPGz6Ad1udveQi9QrXPAm21ykvEMapHW/q87oGg9vGJDPTujHbjazDBVwI1FGYp26xuqu5nxrwk";
String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuw+rYJnRhkyGPr/VE9Znp8tgeVw5EFRQ3/38O6+fm31G9Yb3ewj2bRQ2WAk2uitX0tvMTqT33DUsSm2++Q0BjTT03Vst5atCjjI7Un4opMyIy0qgkNq6BaG0FqkXHZpCP0Pk7w6quQGG6qk75skH7ByqKZIXzhLiZyZy7/hv7+s52NVrwxwxuK9WpJmOzvH+QLDr5ZKdTyqDQH4IQa4hZGFfQhZ/ZCAGCixGQrV3/Ypx7Ig1eUG1kSvy4cn1JDvHbomdDKRdj8LNMXxBu6BvTtTklQfkDorilETXVv+AWuUfjExp7r0psAAOk+7hGZLklqfZ6l8rGhqky3YB/LfJswIDAQAB";
AlipayClient alipayClient = new DefaultAlipayClient(gataliUrl, appid,
privateKey, "JSON", "UTF-8", publicKey
, "RSA2");
return alipayClient;
}
public static String getAliCodeurl(){
AlipayClient alipayClient = createAlipayClient();
//这段代码设置二维码url的失效时间,这边取的失效时间是 当前日期+2分钟
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime nowPlusTwoMinutes = now.plusMinutes(2);
//失效时间
String formattedNowPlusTwoMinutes = nowPlusTwoMinutes.format(formatter);
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//设置回调接口,可以填自己的接口地址,这边我传的空值(回调接口不太稳定,这边我用下面的主动请求ali接口,获取订单信息)
request.setNotifyUrl("");
//订单号 自己生成的 uuid
String out_trade_no = "qwertasdfg";
//金额
String total_amount = "10";
//subject body 商品描述之类的
String subject = "iphone";
String body = "iphone";
request.setBizContent("{\"out_trade_no\":\"" + out_trade_no+ "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"time_expire\":\"" + formattedNowPlusTwoMinutes + "\","
+ "\"product_code\":\"FACE_TO_FACE_PAYMENT\"}");
//获取的二维码连接
String qrCode = "";
// 执行请求,拿到响应的结果,返回给浏览器
try {
AlipayTradePrecreateResponse result = alipayClient.execute(request); // 调用SDK生成表单
if (result.isSuccess()) {
qrCode = result.getQrCode();
System.out.println("二维码连接:"+qrCode);
} else {
String resultdata = result.getBody();
System.out.println("错误原因:"+resultdata);
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return qrCode;
}
//--------------------------支付宝主动获取订单信息-----------------------
public static Boolean isPaySuccess() throws AlipayApiException {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//自己生成订单号
String out_trade_no = "qwertasdfg";
request.setBizContent("{" +
"\"out_trade_no\":\"" + out_trade_no + "\"" +
"}");
AlipayClient alipayClient = createAlipayClient();
AlipayTradeQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
System.out.println(response.getBody());
//返回结果是已经支付
if ("TRADE_SUCCESS".equals(response.getTradeStatus())) {
return true;
}else {
return false;
}
} else {
System.out.println("调用失败");
return false;
}
}
//--------------------------支付宝关闭订单,这边就是让上面生成的支付地址失效-----------------------
public static String colsePay() throws AlipayApiException {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//自己生成订单号
String out_trade_no = "qwertasdfg";
request.setBizContent("{" +
"\"out_trade_no\":\"" + out_trade_no + "\"" +
"}");
AlipayClient alipayClient = createAlipayClient();
AlipayTradeQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return "关闭订单成功";
} else {
return "关闭订单失败"+response.getSubCode();
}
}
//--------------------------退款-----------------------
public static String returnPay() throws AlipayApiException {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//自己生成订单号
String out_trade_no = "qwertasdfg";
request.setBizContent("{" +
"\"out_trade_no\":\"" + out_trade_no + "\"" +
"}");
AlipayClient alipayClient = createAlipayClient();
AlipayTradeRefundRequest alipay_request = new AlipayTradeRefundRequest();
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
//自己生成的订单号
model.setOutTradeNo("qwertasdfg");
//需要退款的金额
model.setRefundAmount("10");
model.setRefundReason("");
alipay_request.setBizModel(model);
AlipayTradeRefundResponse alipay_response = alipayClient.execute(alipay_request);
if ("Y".equals(alipay_response.getFundChange())) {
return "退款成功";
} else {
return "退款失败"+alipay_response.getSubMsg();
}
}
}
注意:主动获取订单状态的接口,只能获取到支付成功的状态,其他状态拿不到;我这边的getAliCodeurl的接口是获取了支付的链接,需要前端自己将这个链接转成二维码
正式环境对接注意!!!
1.申请商户号
2.申请appid
3.商户号和appid进行绑定
4.商户号开通当面付功能
5.设置对应的appid的密钥
如果是企业商户,登录开放平台的时候不要扫码登录,要不然拿不到权限