1.沙箱准备
1.1 去支付宝开放平台找到沙箱测试,appid以及秘钥都是需要的,密钥生成工具支付宝有可以去下载 密钥生成工具.

2.依赖引入
<!--支付宝支付-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.4.49.ALL</version>
</dependency>
3.yml属性配置
### 支付宝支付
alipay:
config:
#支付宝预请求接口
url: https://openapi.alipaydev.com/gateway.do
#商家appid
appId: 2016121231231
#商家私钥
privateKey: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDAOnh1VIED11yqUKVTmvmITyybMDcSwAG0nN3NM/zkUSvUvO3BooqYxta8CFzlZNv3xv4VLJJfM5uuPMJTZS1xEf7vWhuAfzrTOR6Qg4aQr/hGZq1ROGJW/fqm6vIxZ4TVFpTb9UMtyW8bQsItm1/LyVEGYPVIqx5qM8UPZPkMpUg0/IpI8LP0Lb0JQ9eBCY3p4BCYRkwp5Te5xCIjoc4bqH/c+BD0lz2f1oOLrsq3HQJUyFI/MDUAW1WSmsUeKmQUZsUlEwVupNQ5SM1CAsXA1Ca4T+cdH8j5rUIAeaakMqVFy7hTzIqRliFCbS6xYlgVq5yXgWUuGKNqL++YN/bxAgMBAAECggEAGUlXwS9tjUDOOsVDCj1vp8OqfXuZMwk48DZWYwD8ulgHKQP1TqyACNrXMoLzHWXoHa76i3SCiK10R+KGIyQVAsyWkDRvZ6VfXUD3CRfHJEf5iiCMiUOv8TkCW+PQKBgD/DeGXr9C36jO4H82ohVm6mFiMExPLp+f2kxfIkhrcHUwmgQc2KnISXoNZn1Hg5Nv0LpXtNBKen6dbt+rCtI4BBm5ETg+1QMlHFaAVgiqVtyQn1qG8wIN8JS6V7Mapr01N+Q/5XHZXNRtvsHo0/DEyInRbL2HgHzaC5ifBTVM+TpzV8nEue4f2yHE8hkaEWgNCaJpJVC52k7LeEJp+tzPsnCVL655dvKie6qtyBsB+6XpG+E7QfidAoGAUuoekdJdI6AIBJKC4miks2tpCd7JF2cZfw+UkNIy0sbWpclXjx5Qqnc0EBR/gNYyGZC0OYivbL8dL1ZG4w7OS9r8XSdQtGLd58Yhr1Zn3bD6YXTPBRDApa29d5tr8hyRXnXogQ+DQVRaoQd8oIstea0rvJWXbPEN/+U3yODWoe0=
#支付宝公钥
pubKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkVl/RJ5vsQg9/UkRFDNgRFmEDnLIkpy/LzMv12jF3aBp9esAR809iGiqpogyMuCgs143cZIitBOiAYn70E3swptQi0uG2+IqhLcPqBpsVGf6xXJ6G69GAxHx/rhHFEwIDAQAB
charset: UTF-8
#签名类型
signType: RSA2
#异步回调内部接口
notifyUrl: https://www.baidu.com/
#同步回调
returnUrl: https://www.baidu.com/
format: json
回调接口都是要外网能访问的域名接口,不能是ip直接访问,这个可以在测试的时候用穿透工具ngrok做穿透来实现;
4.配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*@author liuxingying
*@description 支付宝配置文件
*@date 2020/9/24
*/
@Data
@Component
@ConfigurationProperties(prefix = "alipay.config")
public class AlipayAppConfig {
private String url;
private String appId;
private String privateKey;
private String pubKey;
private String charset;
private String signType;
// 异步回调地址
private String notifyUrl;
private String returnUrl;
// 返回格式
private String format;
}
5.接口实现
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.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.xx.core.exception.BusinessException;
im

本文详细介绍如何集成支付宝支付功能,包括沙箱环境配置、依赖引入、属性配置、接口实现及回调处理等步骤。提供了完整的示例代码,帮助开发者快速上手。
最低0.47元/天 解锁文章
907

被折叠的 条评论
为什么被折叠?



