01-支付宝支付

image-20200829141533196

官网地址:https://www.alipay.com/

沙箱环境:https://opendocs.alipay.com/open/200/105311

1. 封装二维码操作

QrCodeUtil-二维码生成与解析

2. 支付宝支付 Demo

2.1 依赖 jar

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.87.ALL</version>
</dependency>

2.2 封装工具类

  • AliPayUtil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;

public class AliPayUtil {
/**
* 支付宝相关配置
*/
public static final String ALIPAY_APPID = "(APPID)";
public static final String ALIPAY_PRIVATEKEY = "(私钥)";
public static final String ALIPAY_PUBLICKEY = "(公钥)";

private static final AlipayClient alipayClient;

static {
alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
ALIPAY_APPID,
ALIPAY_PRIVATEKEY,
"json", "UTF-8",
ALIPAY_PUBLICKEY, "RSA2");
}

/**
* 生成支付链接
*/
public static String createPayUrl(String payjson) {
//创建请求对象
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//设置请求参数 必选
request.setBizContent(payjson);
try {//发起请求 获取响应
AlipayTradePrecreateResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getQrCode();
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}

/**
* 撤消支付
*/
public static String cancelPay(String oid) {
AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
request.setBizContent("{\"trade_no\":" + oid + "}");
try {
AlipayTradeCancelResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getAction();
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}

/**
* 关闭支付
*/
public static boolean closePay(String oid) {
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
request.setBizContent("{\"trade_no\":" + oid + "}");
try {
AlipayTradeCloseResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return true;
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}

/**
* 查询支付状态
* 交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、TRADE_SUCCESS(交易支付成功)、TRADE_FINISHED(交易结束,不可退款)
*/
public static String queryPay(String oid) {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent("{\"out_trade_no\":" + oid + "}");
try {
AlipayTradeQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getTradeStatus();
}
System.err.println(response.getMsg());
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}

/**
* 退款
*/
public static boolean refundPay(String payjson) {
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent(payjson);
try {
AlipayTradeRefundResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return true;
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}

/**
* 查询退款进度
*/
public static boolean refundQueryPay(String payjson) {
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
request.setBizContent(payjson);
try {
AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return true;
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}
}

2.3 封装接口实现

  • AliPayController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// API
@RestController
@RequestMapping("/api/alipay")
public class AliPayController {
@Autowired
private AliPayService service;

@PostMapping("/createpay.do")
public R createPay(@RequestBody AliPayDto dto) {
return service.createPay(dto);
}

@GetMapping("/querypay.do")
public R queryPay(@RequestParam String oid) {
return service.queryPay(oid);
}

@PostMapping("/refundpay.do")
public R refundPay(@RequestBody AliPayRefundDto dto) {
return service.refundPay(dto);
}
}
  • AliPayService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface AliPayService {
/**
* 生成支付二维码
*/
R<String> createPay(AliPayDto payDto);

/**
* 查询支付状态
*/
R<String> queryPay(String oid);

/**
* 退款
*/
R refundPay(AliPayRefundDto refundDto);
}
  • AliPayServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Base64;

@Service
public class AliPayServiceImpl implements AliPayService {
@Override
public R<String> createPay(AliPayDto payDto) {
String qrcode = AliPayUtil.createPayUrl(JSON.toJSONString(payDto));
if (!StringUtils.isEmpty(qrcode)) {
// 生成二维码(依赖二维码工具类)
return R.ok("http://localhost:8085/api/qrcode/payqrcode/" + Base64.getUrlEncoder().encodeToString(qrcode.getBytes()));
}
return R.error();
}

@Override
public R<String> queryPay(String oid) {
return R.ok(AliPayUtil.queryPay(oid));
}

@Override
public R refundPay(AliPayRefundDto refundDto) {
if (AliPayUtil.refundPay(JSON.toJSONString(refundDto))) {
return R.ok();
} else {
return R.error();
}
}
}

01-支付宝支付
https://janycode.github.io/2020/03/02/14_微服务/10_支付服务/01-支付宝支付/
作者
Jerry(姜源)
发布于
2020年3月2日
许可协议