Build Alipay Payments with Spring Boot: A Complete Guide
This article introduces a Spring Boot‑based open‑source project that demonstrates how to integrate Alipay's various payment modes—including QR code, PC, WAP, and app payments—by detailing the required tech stack, configuration files, deployment steps, and key code snippets.
Hello, I’m TJ, a programmer who loves sharing useful open‑source tools.
Spring‑Boot Alipay Payment Demo
The spring-boot-pay project is a plug‑and‑play example for learning Alipay payment services. It supports QR code, PC, WAP, and APP payment scenarios.
Prerequisites
Alipay testing normally requires enterprise certification; you can also use the service‑provider mode to obtain a personal merchant account for testing.
Technology Stack
JDK 1.8
Maven
IntelliJ IDEA
Spring Boot 2.2.6
Dubbo 2.7.3
Zookeeper 3.5.3
Demo Screenshots
Alipay Configuration (zfbinfo.properties)
open_api_domain = https://openapi.alipay.com/gateway.do
mcloud_api_domain = http://mcloudmonitor.com/gateway.do
pid =XXXXXXXXXXXXXX
appid =XXXXXXXXXXXXXX
private_key = XXXXXXXXXXXXXX
public_key = XXXXXXXXXXXXXX
alipay_public_key = XXXXXXXXXXXXXX
max_query_retry = 5
query_duration = 5000
max_cancel_retry = 3
cancel_duration = 2000
heartbeat_delay = 5
heartbeat_duration = 900Useful Links
https://openclub.alipay.com/read.php?tid=2190&fid=69
https://docs.open.alipay.com/54/103419/
Deployment Steps
Install and start Zookeeper as the service registry.
Initialize configuration via com.itstyle.common.cinfig.InitPay.
Configure Alipay account credentials and certificates.
Run the Spring Boot application.
Key Code Samples
/**
* PC payment
*/
@ApiOperation(value="电脑支付")
@PostMapping(value="pcPay")
public String pcPay(Product product, ModelMap map) {
logger.info("电脑支付");
String form = aliPayService.aliPayPc(product);
map.addAttribute("form", form);
return "aliPay/pay";
}
/**
* Mobile H5 payment
*/
@ApiOperation(value="手机H5支付")
@PostMapping(value="mobilePay")
public String mobilePay(Product product, ModelMap map) {
logger.info("手机H5支付");
String form = aliPayService.aliPayMobile(product);
map.addAttribute("form", form);
return "aliPay/pay";
}
/**
* QR code payment
*/
@ApiOperation(value="二维码支付")
@PostMapping(value="qcPay")
public String qcPay(Product product, ModelMap map) {
logger.info("二维码支付");
String message = aliPayService.aliPay(product);
if (!Constants.FAIL.equals(message)) {
map.addAttribute("img", message);
}
return "aliPay/qcpay";
}
/**
* App payment (server side)
*/
@ApiOperation(value="app支付服务端")
@PostMapping(value="appPay")
public String appPay(Product product, ModelMap map) {
logger.info("app支付服务端");
String orderString = aliPayService.appPay(product);
map.addAttribute("orderString", orderString);
return "aliPay/pay";
}The project also includes examples for WeChat and UnionPay, making it a comprehensive resource for anyone wanting to explore payment integrations in Java.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
