Integrating Alipay’s New Transfer API with Spring Boot (Java)
This guide explains how to replace the old Alipay transfer interface with the new certificate‑based alipay.fund.trans.uni.transfer API, upgrade the SDK, configure certificates, add Maven dependencies, and implement Spring‑Boot beans and utility classes for secure payment and transfer operations.
Alipay has launched a new transfer interface alipay.fund.trans.uni.transfer that uses certificate verification for higher security, deprecating the old alipay.fund.trans.toaccount.transfer API.
To use the new interface, upgrade the Alipay SDK to the latest version (at the time of writing 4.10.97) and follow the integration steps below.
1. Place certificates
Download the three certificates from the Alipay Open Platform and put them under the resources directory of your project.
2. Create configuration file
alipay.appId=YOUR_APP_ID
alipay.serverUrl=https://openapi.alipay.com/gateway.do
alipay.privateKey=YOUR_PRIVATE_KEY
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.crt3. Add Maven dependency
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.97.ALL</version>
</dependency>4. Inject configuration into a bean
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. Create a configuration class
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());
certAlipayRequest.setAppId(aliPayBean.getAppId());
certAlipayRequest.setPrivateKey(aliPayBean.getPrivateKey());
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 {
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) {
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name)) {
return new String(FileCopyUtils.copyToByteArray(inputStream));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}6. Implement a utility class for payment and transfer
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class AliPayUtils {
@Autowired
@Qualifier("alipayClient")
private AlipayClient alipayClient;
public String startAppPay(AlipayTradeAppPayModel model, String notifyUrl) throws AlipayApiException {
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
model.setProductCode("QUICK_MSECURITY_PAY");
request.setNotifyUrl(notifyUrl);
request.setBizModel(model);
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
return response.getBody();
}
// Transfer method omitted for brevity – see original source for full implementation
}Additional data‑transfer classes ( TransferParams, BizContentForUniTransfer, Participant) are defined in the source and should be copied unchanged into your project.
After completing these steps, the new Alipay transfer API can be called securely from a Spring‑Boot application.
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
