alipay.properties
alipay.appId=你的应用id
alipay.serverUrl=https://openapi.alipay.com/gateway.do
alipay.privateKey=你的应用私钥
alipay.format=json
alipay.charset=UTF-8
alipay.signType=RSA2
alipay.appCertPath=/cert/appCertPublicKey_2021001164652941.crt
alipay.alipayCertPath=/cert/alipayCertPublicKey_RSA2.crt
alipay.alipayRootCertPath=/cert/alipayRootCert.crt
3.引入pom依赖
com.alipay.sdk
alipay-sdk-java
4.10.97.ALL
4.将配置信息注入AliPayBean
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(“classpath:/production/alipay.properties”)
@ConfigurationProperties(prefix = “alipay”)
@Data
public class AliPayBean {
private String appId;
private String privateKey;
private String publicKey;
private String serverUrl;
private String domain;
private String format;
private String charset;
private String signType;
private String appCertPath;
private String alipayCertPath;
private String alipayRootCertPath;
}
5.写配置类
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.FileCopyUtils;
import java.io.InputStream;
@Configuration
public class AliConfig {
@Value(“${custom.http.proxyHost}”)
private String proxyHost;
@Value(“${custom.http.proxyPort}”)
private int proxyPort;
@Value(“${spring.profiles.active}”)
private String activeEnv;
@Autowired
private AliPayBean aliPayBean;
@Bean(name = {“alipayClient”})
public AlipayClient alipayClientService() throws Exception{
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//设置网关地址
certAlipayRequest.setServerUrl(aliPayBean.getServerUrl());
//设置应用Id
certAlipayRequest.setAppId(aliPayBean.getAppId());
//设置应用私钥
certAlipayRequest.setPrivateKey(aliPayBean.getPrivateKey());
//设置请求格式,固定值json
certAlipayRequest.setFormat(aliPayBean.getFormat());
//设置字符集
certAlipayRequest.setCharset(aliPayBean.getCharset());
//设置签名类型
certAlipayRequest.setSignType(aliPayBean.getSignType());
//如果是生产环境或者预演环境,则使用代理模式
if (“prod”.equals(activeEnv) || “stage”.equals(activeEnv) || “test”.equals(activeEnv)) {
//设置应用公钥证书路径
certAlipayRequest.setCertContent(getCertContentByPath(aliPayBean.getAppCertPath()));
//设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertContent(getCertContentByPath(aliPayBean.getAlipayCertPath()));
//设置支付宝根证书路径
certAlipayRequest.setRootCertContent(getCertContentByPath(aliPayBean.getAlipayRootCertPath()));
certAlipayRequest.setProxyHost(proxyHost);
certAlipayRequest.setProxyPort(proxyPort);
}else {
//local
String serverPath = this.getClass().getResource(“/”).getPath();
//设置应用公钥证书路径
certAlipayRequest.setCertPath(serverPath+aliPayBean.getAppCertPath());
//设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertPath(serverPath+aliPayBean.getAlipayCertPath());
//设置支付宝根证书路径
certAlipayRequest.setRootCertPath(serverPath+aliPayBean.getAlipayRootCertPath());
}
return new DefaultAlipayClient(certAlipayRequest);
}
public String getCertContentByPath(String name){
InputStream inputStream = null;
String content = null;
try{
inputStream = this.getClass().getClassLoader().getResourceAsStream(name);
content = new String(FileCopyUtils.copyToByteArray(inputStream));
}catch (Exception e){
e.printStackTrace();
}
return content;
}
}
6.写支付工具类
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* @description:支付宝工具类
* @Date:2020-08-26
*/
@Slf4j
@Service
public class AliPayUtils {
@Autowired
@Qualifier(“alipayClient”)
private AlipayClient alipayClient;
/**
* 交易查询接口
* @param request
* @return
* @throws Exception
*/
public boolean isTradeQuery(AlipayTradeQueryModel model) throws AlipayApiException {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizModel(model);
AlipayTradeQueryResponse alipayTradeQueryResponse = alipayClient.certificateExecute(request);
if(alipayTradeQueryResponse.isSuccess()){
return true;
} else {
return false;
}
}
/**
* app支付
* @param model
* @param notifyUrl
* @return
* @throws AlipayApiException
*/
public String startAppPay(AlipayTradeAppPayModel model, String notifyUrl) throws AlipayApiException {
AlipayTradeAppPayRequest aliPayRequest = new AlipayTradeAppPayRequest();
model.setProductCode(“QUICK_MSECURITY_PAY”);
aliPayRequest.setNotifyUrl(notifyUrl);
aliPayRequest.setBizModel(model);
// 这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse aliResponse = alipayClient.sdkExecute(aliPayRequest);
return aliResponse.getBody();
}
/**
* 转账接口
* @param transferParams
* @return AlipayFundTransToaccountTransferResponse
*/
public AlipayFundTransUniTransferResponse doTransferNew(TransferParams transferParams) throws Exception {
String title = (StringUtils.isNotBlank(transferParams.getRemark()) ? transferParams
.getRemark() : “转账”);
//转账请求入参
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
//转账参数
BizContentForUniTransfer bizContent = new BizContentForUniTransfer();
bizContent.setOut_biz_no(transferParams.getOutBizNo());
bizContent.setTrans_amount(MathUtil.changeF2Y(Math.abs(Integer.parseInt(transferParams.getAmount()))));
bizContent.setProduct_code(“TRANS_ACCOUNT_NO_PWD”);
bizContent.setBiz_scene(“DIRECT_TRANSFER”);
bizContent.setOrder_title(title);
Participant participant = new Participant();
participant.setIdentity(transferParams.getPayeeAccount());
participant.setIdentity_type(transferParams.getPayeeType());
participant.setName((StringUtils.isNotBlank(transferParams.getPayeeRealName()) ? transferParams
.getPayeeRealName() : StringUtils.EMPTY));
bizContent.setPayee_info(participant);
bizContent.setRemark(title);
request.setBizContent(JSON.toJSONString(bizContent));
//转账请求返回
AlipayFundTransUniTransferResponse response = null;
try {
response = alipayClient.certificateExecute(request);
} catch (Exception e) {
log.info(“doTransfer exception,异常信息:{}”, e.toString());
log.info(“doTransfer exception,支付宝返回信息:{}”, JSONObject.toJSONString(response));
}
log.info(“doTransfer,AlipayFundTransUniTransferResponse:{}”, JSONObject.toJSONString(response));
return response;
}
}
Spring Boot 最新教程推荐看这个:
https://github.com/javastacks/spring-boot-best-practice
最后
现在其实从大厂招聘需求可见,在招聘要求上有高并发经验优先,包括很多朋友之前都是做传统行业或者外包项目,一直在小公司,技术搞的比较简单,没有怎么搞过分布式系统,但是现在互联网公司一般都是做分布式系统。
所以说,如果你想进大厂,想脱离传统行业,这些技术知识都是你必备的,下面自己手打了一份Java并发体系思维导图,希望对你有所帮助。
ject.toJSONString(response));
}
log.info(“doTransfer,AlipayFundTransUniTransferResponse:{}”, JSONObject.toJSONString(response));
return response;
}
}
Spring Boot 最新教程推荐看这个:
https://github.com/javastacks/spring-boot-best-practice
最后
现在其实从大厂招聘需求可见,在招聘要求上有高并发经验优先,包括很多朋友之前都是做传统行业或者外包项目,一直在小公司,技术搞的比较简单,没有怎么搞过分布式系统,但是现在互联网公司一般都是做分布式系统。
所以说,如果你想进大厂,想脱离传统行业,这些技术知识都是你必备的,下面自己手打了一份Java并发体系思维导图,希望对你有所帮助。
[外链图片转存中…(img-jPCpn19H-1714450610451)]