支付寶今年推出了新的轉賬介面alipay.fund.trans.uni.transfer
(升級後安全性更高,功能更加強大) ,老轉賬介面alipay.fund.trans.toaccount.transfer
將不再維護,新老介面的一個區別就是新介面採用的證照驗籤方式。
使用新介面要將sdk版本升級到最新版本,博主升級時最新版本是4.10.97。
接下來看整合步驟。
1.將支付寶開放平臺裡下載的3個證照放在resources下面
2.寫支付寶支付的配置檔案
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依賴
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.97.ALL</version>
</dependency>
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;
//from www.fhadmin.cn
@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:支付寶工具類
* from www.fhadmin.cn
*/
@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();
}
/**
* 轉賬介面
*from www.fhadmin.cn
* @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;
}
}
Tips:轉賬用到的類
@Data
public class TransferParams {
/**
* 應用編號
*/
private Long appId;
/**
* 建立人id
*/
private Long createdBy;
/**
* 轉賬業務訂單號
*/
private String outBizNo;
/**
* 收款方識別方式
*/
private String payeeType;
/**
* 收款方賬號,可以是支付寶userId或者支付寶loginId
*/
private String payeeAccount;
/**
* 轉賬金額,單位分
*/
private String amount;
/**
* 付款方名稱
*/
private String payerShowName;
/**
* 收款方名稱
*/
private String payeeRealName;
/**
* 備註
*/
private String remark;
/**
* 支付寶轉賬流水號
*/
private String orderId;
}
import lombok.Data;
import java.math.BigDecimal;
/**
* 支付寶轉賬引數
*/
@Data
public class BizContentForUniTransfer {
/**
* 業務訂單號
*/
private String out_biz_no;
/**
* 訂單總金額,單位為元,精確到小數點後兩位,
*/
private BigDecimal trans_amount;
/**
* 業務產品碼,
* 單筆無密轉賬到支付寶賬戶固定為:TRANS_ACCOUNT_NO_PWD;
* 單筆無密轉賬到銀行卡固定為:TRANS_BANKCARD_NO_PWD;
* 收發現金紅包固定為:STD_RED_PACKET;
*/
private String product_code;
/**
* 描述特定的業務場景,可傳的引數如下:
* DIRECT_TRANSFER:單筆無密轉賬到支付寶/銀行卡, B2C現金紅包;
* PERSONAL_COLLECTION:C2C現金紅包-領紅包
*/
private String biz_scene;
/**
* 轉賬業務的標題,用於在支付寶使用者的賬單裡顯示
*/
private String order_title;
/**
* 原支付寶業務單號。C2C現金紅包-紅包領取時,傳紅包支付時返回的支付寶單號;
* B2C現金紅包、單筆無密轉賬到支付寶/銀行卡不需要該引數。
*/
private String original_order_id;
/**
* 業務備註
*/
private String remark;
/**
* 轉賬業務請求的擴充套件引數,支援傳入的擴充套件引數如下:
* 1、sub_biz_scene 子業務場景,紅包業務必傳,取值REDPACKET,C2C現金紅包、B2C現金紅包均需傳入;
* 2、withdraw_timeliness為轉賬到銀行卡的預期到賬時間,可選(不傳入則預設為T1),
* 取值T0表示預期T+0到賬,取值T1表示預期T+1到賬,因到賬時效受銀行機構處理影響,支付寶無法保證一定是T0或者T1到賬;
*/
private String business_params;
/**
* 支付收款物件
*/
private Participant payee_info;
}
@Data
public class Participant {
/**
* 參與方的唯一標識
*/
private String identity;
/**
* 參與方的標識型別,目前支援如下型別:
* 1、ALIPAY_USER_ID 支付寶的會員ID
* 2、ALIPAY_LOGON_ID:支付寶登入號,支援郵箱和手機號格式
*/
private String identity_type;
/**
* 參與方真實姓名,如果非空,將校驗收款支付寶賬號姓名一致性。
* 當identity_type=ALIPAY_LOGON_ID時,本欄位必填。
*/
private String name;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結