官网地址:https://www.alipay.com/
沙箱环境:https://opendocs.alipay.com/open/200/105311
1. 封装二维码操作
QrCodeUtil-二维码生成与解析
2. 支付宝支付 Demo
2.1 依赖 jar
1 2 3 4 5 6
| <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.10.87.ALL</version> </dependency>
|
2.2 封装工具类
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; }
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 封装接口实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @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); } }
|
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); }
|
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(); } } }
|