一、准备支付平台对接材料:
1.首先进入支付宝开放平台的官网:https://open.alipay.com/
2.支付宝扫码登录注册成为开发者。
3.进入控制台→开发工具推荐→沙箱,创建网页&移动应用。
4.应用信息里面有APPID,接口加签方式启用公钥模式点击查看即可看到应用公钥和私钥(这一步如果没有需要自己下载密钥生成工具,具体参考官方文档:https://opendocs.alipay.com/open/291/105971)。
5.应用网关地址需要对应你项目的访问地址,在测试环境下一般是:http://127.0.0.1:8080
二、进入开发过程:
(1)新建一个SpringBoot工程,我用的版本是2.5.10。
(2)后端相关类和配置:
1、引入pom.xml配置:
Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<!--支付宝sdk-->
<!-- alipay -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.1.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、实体类:
Ali实体类
package com.alipay.entity;
import lombok.Data;
/**
* @Author zf
* @ClassName AliBean.java
* @ProjectName alipay
*/
@Data
public class AliBean {
/**
* 商户订单号,必填
*/
private String out_trade_no;
/**
* 订单名称,必填
*/
private String subject;
/**
* 付款金额,必填
* 根据支付宝接口协议,必须使用下划线
*/
private String total_amount;
/**
* 商品描述,可空
*/
private String body;
/**
* 超时时间参数
*/
private String timeout_express = "1h";
/**
* 产品编号
*/
private String product_code = "FAST_INSTANT_TRADE_PAY";
}
3、PayService:
PayService
package com.alipay.service;
import com.alipay.api.AlipayApiException;
import com.alipay.entity.AliBean;
/**
* @Author zf
* @ClassName PayService.java
* @ProjectName alipay
*/
public interface PayService {
String aliPay(AliBean alipayBean) throws AlipayApiException;
}
4、PayServiceImpl:
PayServiceImpl
package com.alipay.service.impl;
import com.alipay.api.AlipayApiException;
import com.alipay.entity.AliBean;
import com.alipay.pay.AliPay;
import com.alipay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author zf
* @ClassName PayServiceImpl.java
* @ProjectName alipay
*/
@Service
public class PayServiceImpl implements PayService {
@Autowired
private AliPay aliPay;
@Override
public String aliPay(AliBean alipayBean) throws AlipayApiException {
System.out.println(alipayBean.toString());
return aliPay.pay(alipayBean);
}
}
5、AliPay.yml:
AliPay.yml
# 应用ID
appId: 您的APPID,收款账号既是您的APPID对应支付宝账号。
# 商户私钥
privateKey: 您的PKCS8格式RSA2私钥。
# 支付宝公钥
publicKey: 查看地址:https://openhome.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
# 服务器异步通知页面路径需http://格式的完整路径,不能加?id=123这类自定义参数。
notifyUrl: http://localhost:9090/error.html
# 页面跳转同步通知页面路径,需http://格式的完整路径,不能加?id=123这类自定义参数。
returnUrl: http://127.0.0.1:8080/success.html
# 签名方式
signType: RSA2
# 字符编码格式
charset: utf-8
# 支付宝网关
gatewayUrl: https://openapi.alipaydev.com/gateway.do
# 支付宝日志
logPath: "D:\\log"
6、PropertiesListener:
PropertiesListener
package com.alipay.config;
import com.alipay.pay.AliPayProperties;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @Author zf
* @ClassName PropertiesListener.java
* @ProjectName alipay
*/
@Component
public class PropertiesListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
AliPayProperties.loadProperties();
}
}
7、AliPay:
AliPay
package com.alipay.pay;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.entity.AliBean;
import org.springframework.stereotype.Component;
/**
* @Author zf
* @ClassName AliPay.java
* @ProjectName alipay
*/
@Component
public class AliPay {
/**
* 支付接口
* @param alipayBean
* @return
* @throws AlipayApiException
*/
public String pay(AliBean alipayBean) throws AlipayApiException {
// 1、获得初始化的AlipayClient
String serverUrl = AliPayProperties.getGatewayUrl();
String appId = AliPayProperties.getAppId();
String privateKey = AliPayProperties.getPrivateKey();
String format = "json";
String charset = AliPayProperties.getCharset();
String alipayPublicKey = AliPayProperties.getPublicKey();
String signType = AliPayProperties.getSignType();
String returnUrl = AliPayProperties.getReturnUrl();
String notifyUrl = AliPayProperties.getNotifyUrl();
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
// 2、设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
// 页面跳转同步通知页面路径
alipayRequest.setReturnUrl(returnUrl);
// 服务器异步通知页面路径
alipayRequest.setNotifyUrl(notifyUrl);
// 封装参数
alipayRequest.setBizContent(JSON.toJSONString(alipayBean));
// 3、请求支付宝进行付款,并获取支付结果
String result = alipayClient.pageExecute(alipayRequest).getBody();
System.out.println(result);
// 返回付款信息
return result;
}
}
8、AliPayProperties:
AliPayProperties
package com.alipay.pay;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @Author zf
* @ClassName AliPayProperties.java
* @ProjectName alipay
*/
@Component
public class AliPayProperties {
public static final String APP_ID = "appId";
public static final String PRIVARY_KEY = "privateKey";
public static final String PUBLIC_KEY = "publicKey";
public static final String NOTIFY_URL = "notifyUrl";
public static final String RETURN_URL = "returnUrl";
public static final String SIGN_TYPE = "signType";
public static final String CHARSET = "charset";
public static final String GATEWAY_URL = "gatewayUrl";
public static final String LOG_PATH = "logPath";
/**
* 保存加载配置参数
*/
private static Map<String, String> propertiesMap = new HashMap<String, String>();
/**
* 加载属性
*/
public static void loadProperties() {
// 获得PathMatchingResourcePatternResolver对象
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
// 加载resource文件(也可以加载resources)
Resource resources = resolver.getResource("classpath:AliPay.yml");
PropertiesFactoryBean config = new PropertiesFactoryBean();
config.setLocation(resources);
config.afterPropertiesSet();
Properties prop = config.getObject();
// 循环遍历所有得键值对并且存入集合
for (String key : prop.stringPropertyNames()) {
propertiesMap.put(key, (String) prop.get(key));
}
} catch (Exception e) {
new Exception("配置文件加载失败");
}
}
/**
* 获取配置参数值
* @param key
* @return
*/
public static String getKey(String key) {
return propertiesMap.get(key);
}
public static String getAppId() {
return propertiesMap.get(APP_ID);
}
public static String getPrivateKey() {
return propertiesMap.get(PRIVARY_KEY);
}
public static String getPublicKey() {
return propertiesMap.get(PUBLIC_KEY);
}
public static String getNotifyUrl() {
return propertiesMap.get(NOTIFY_URL);
}
public static String getReturnUrl() {
return propertiesMap.get(RETURN_URL);
}
public static String getSignType() {
return propertiesMap.get(SIGN_TYPE);
}
public static String getCharset() {
return propertiesMap.get(CHARSET);
}
public static String getGatewayUrl() {
return propertiesMap.get(GATEWAY_URL);
}
public static String getLogPath() {
return propertiesMap.get(LOG_PATH);
}
}
9、OrderController:
OrderController
package com.alipay.controller;
import com.alipay.api.AlipayApiException;
import com.alipay.entity.AliBean;
import com.alipay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author zf
* @ClassName OrderController.java
* @ProjectName alipay
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private PayService payService;
@PostMapping(value = "alipay")
public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
AliBean alipayBean = new AliBean();
alipayBean.setOut_trade_no(outTradeNo);
alipayBean.setSubject(subject);
alipayBean.setTotal_amount(totalAmount);
alipayBean.setBody(body);
return payService.aliPay(alipayBean);
}
}
(3)前端相关页面:
1、首页:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下单页面</title>
</head>
<body>
<form id="form" action="order/alipay" method="post">
商户订单:
<input type="text" name="outTradeNo" value="zfb10001"></br>
订单名称:
<input type="text" name="subject" value="小米13"></br>
付款金额:
<input type="text" name="totalAmount" value="1999"></br>
商品描述:
<input type="text" name="body" value="为发烧而生!"></br>
<input type="submit" value="提交订单">
</form>
<!-- 页面可以自定义,但是要注意的是,里面的name值需要和controller接口里面的所需参数一一对应。 -->
</body>
</html>
2、支付成功后要跳转的页面:
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1>
恭喜你,支付成功了。
</h1>
</body>
</html>
三、开始测试环节:
1.进入控制台→开发工具推荐→沙箱,沙箱工具下载支付宝客户端沙箱版。
2.安装好以后选择邮箱方式登录,账号密码对应沙箱账号里面的买家信息。
3.然后提交订单用沙箱工具扫码支付就可以了。配置文件里面的回调地址需要将项目部署在公网方能生效,因为支付宝平台无法请求到你本地的localhost。
如果运行测试项目报错需要在启动类加
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
注解,原因是:DataSourceAutoConfiguration.class 会自动查找 application.yml 或者 properties 文件里的 spring.datasource.* 相关属性并自动配置单数据源。如果你没有配置数据库连接信息那肯定报错!上面的注解的用途是禁止SpringBoot自动注入数据源配置。