Integrating Alipay’s New Transfer API (alipay.fund.trans.uni.transfer) in a Java Spring Application
This guide explains how to integrate Alipay’s new secure transfer API (alipay.fund.trans.uni.transfer) into a Java Spring application, covering SDK upgrade, certificate placement, configuration files, Maven dependency, bean injection, and utility classes with full code examples for payment and transfer operations.
Alipay has launched a new transfer interface alipay.fund.trans.uni.transfer (more secure and powerful) that replaces the old alipay.fund.trans.toaccount.transfer, which will no longer be maintained; the new API uses certificate signature verification.
To use the new interface, upgrade the Alipay SDK to the latest version (the author used 4.10.97 at the time of writing) and then follow the integration steps.
1. Place the three certificates downloaded from the Alipay Open Platform into the resources directory
2. Create the Alipay configuration file
File:
alipay.properties 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 the Maven dependency
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.97.ALL</version>
</dependency>4. Inject the configuration into 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. Write the 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) {
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. Write the payment utility class
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: Alipay utility class
* @Date:2020-08-26
*/
@Slf4j
@Service
public class AliPayUtils {
@Autowired
@Qualifier("alipayClient")
private AlipayClient alipayClient;
/**
* Trade query interface
*/
public boolean isTradeQuery(AlipayTradeQueryModel model) throws AlipayApiException {
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizModel(model);
AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);
if (response.isSuccess()) {
return true;
} else {
return false;
}
}
/**
* App payment
*/
public String startAppPay(AlipayTradeAppPayModel model, String notifyUrl) throws AlipayApiException {
AlipayTradeAppPayRequest aliPayRequest = new AlipayTradeAppPayRequest();
model.setProductCode("QUICK_MSECURITY_PAY");
aliPayRequest.setNotifyUrl(notifyUrl);
aliPayRequest.setBizModel(model);
AlipayTradeAppPayResponse aliResponse = alipayClient.sdkExecute(aliPayRequest);
return aliResponse.getBody();
}
/**
* New transfer interface
*/
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;
}
}Tips: Classes used for the transfer operation
@Data
public class TransferParams {
/** Application ID */
private Long appId;
/** Creator ID */
private Long createdBy;
/** Business order number */
private String outBizNo;
/** Payee identification type */
private String payeeType;
/** Payee account (Alipay userId or loginId) */
private String payeeAccount;
/** Amount in cents */
private String amount;
/** Payer display name */
private String payerShowName;
/** Payee real name */
private String payeeRealName;
/** Remark */
private String remark;
/** Alipay transfer serial number */
private String orderId;
} import lombok.Data;
import java.math.BigDecimal;
/** Alipay transfer parameters */
@Data
public class BizContentForUniTransfer {
/** Business order number */
private String out_biz_no;
/** Transaction amount in yuan */
private BigDecimal trans_amount;
/** Product code, e.g., TRANS_ACCOUNT_NO_PWD */
private String product_code;
/** Business scene, e.g., DIRECT_TRANSFER */
private String biz_scene;
/** Order title shown on the user’s bill */
private String order_title;
/** Original Alipay order ID (optional) */
private String original_order_id;
/** Business remark */
private String remark;
/** Extension parameters */
private String business_params;
/** Payee information */
private Participant payee_info;
} @Data
public class Participant {
/** Unique identifier of the participant */
private String identity;
/** Identifier type, e.g., ALIPAY_USER_ID or ALIPAY_LOGON_ID */
private String identity_type;
/** Real name of the participant (required when identity_type is ALIPAY_LOGON_ID) */
private String name;
}点赞是最大的支持
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
