Appearance
获取到appid、公私钥后,我们就可以进行开发环境的配置了。
xml
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.22.0.ALL</version>
</dependency>
配置文件application.yml
yaml
ali:
appid: 20210*****24906
private-key: # 一般最长的是私钥
alipay-public-key: # 支付宝的应用公钥
notify-domain: https://xk857.mynatapp.cc # 你自己的回调地址,可以使用接口回调软件进行测试
gateway-host: https://openapi.alipay.com #支付宝统一网关
sign-type: RSA2 #加密方式
return-domain: https://www.xk857.com #H5或其他方式支付成功支付宝自动跳转地址
读取配置文件信息
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Data
@Configuration
@PropertySource("classpath:application.yml")
@ConfigurationProperties(prefix = "ali")
public class AliPayConfig {
private String appid;
private String privateKey;
private String alipayPublicKey;
/**
* 接收结果通知地址
*/
private String notifyDomain;
/**
* 支付宝统一网关
*/
private String gatewayHost;
/**
* 加密方式
*/
private String signType;
/**
* H5或其他方式支付成功支付宝自动跳转地址
*/
private String returnDomain;
}
将Client实例存入Bean中
java
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.xk857.alipay.config.AliPayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class AliPayClientBean {
@Autowired
private AliPayConfig aliPayConfig;
@Bean
public AlipayClient getAlipayClient() {
return new DefaultAlipayClient(aliPayConfig.getGatewayHost(), aliPayConfig.getAppid(), aliPayConfig.getPrivateKey(), "json", "UTF-8", aliPayConfig.getAlipayPublicKey(), aliPayConfig.getSignType());
}
}