需要准備的東西如下:
應用私鑰,支付寶公鑰,應用公鑰證書、支付寶公鑰證書、支付寶根證書, 應用的appId,每個應用的appId都有對應的秘鑰證書的
通過支付寶開放平台開發助手app獲取秘鑰(公鑰、私鑰),然后拿公鑰去支付寶開放平台獲取支付寶公鑰
如果還需要加簽成證書,那么就得到應用公鑰證書和支付寶公鑰證書 應用證書是可以解析應用公鑰的,
注意: 應用私鑰請妥善保管,忘記了只能重新提取,是解析不出來的 ,支付寶公鑰證書可以解析支付寶公鑰的
支付寶開放平台地址: https://open.alipay.com
開發官方文檔: https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
支付寶sdk: https://opendocs.alipay.com/open/54/103419 這里有各環境下的示例
測試過程中如遇到問題請前往這里: https://opendocs.alipay.com/support/01ravi
生成以及配置證書: https://opendocs.alipay.com/support/01rauw
依賴如下:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
</dependency>
支付寶轉賬到支付寶
以下都是使用證書的方式進行調用的
方式一:
新接口: alipay.fund.trans.uni.transfer
public Map<String, Object> aliPayTransfer(AliPayTransferParam aliPayTransfer) { try { CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); certAlipayRequest.setServerUrl(aliPayConfig.getSERVER_URL()); certAlipayRequest.setAppId(aliPayConfig.getAPPID()); certAlipayRequest.setPrivateKey(aliPayConfig.getPAY_PRIVATE_KEY()); certAlipayRequest.setFormat(aliPayConfig.getFORMAT()); certAlipayRequest.setCharset(aliPayConfig.getCHARSET()); certAlipayRequest.setSignType(aliPayConfig.getSIGN_TYPE()); try { //獲取各證書路徑
AliPayConfig certPath = getCertPath(); //設置應用公鑰證書路徑
certAlipayRequest.setCertPath(certPath.getAPPLICATION_PUBLIC_KEY()); //設置支付寶公鑰證書路徑
certAlipayRequest.setAlipayPublicCertPath(certPath.getAliPay_PUBLIC_CERT()); //設置支付寶根證書路徑
certAlipayRequest.setRootCertPath(certPath.getROOT_CERT_PATH()); } catch (Exception e) { e.printStackTrace(); } AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest); AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); AliPay alipay = new AliPay(); //收款方信息
PayeeInfo payerInfo = new PayeeInfo(); payerInfo.setIdentity(aliPayTransfer.getIdentity()); if (aliPayTransfer.getName() != null) { payerInfo.setIdentity_type("ALIPAY_LOGON_ID"); payerInfo.setName(aliPayTransfer.getName()); }else { payerInfo.setIdentity_type("ALIPAY_USER_ID"); } alipay.setPayee_info(payerInfo); alipay.setOut_biz_no(aliPayTransfer.getOutBizNo()); alipay.setTrans_amount(aliPayTransfer.getTransAmount()); alipay.setProduct_code("TRANS_ACCOUNT_NO_PWD"); alipay.setBiz_scene("DIRECT_TRANSFER"); if (aliPayTransfer.getOrderTitle() != null) { alipay.setOrder_title(aliPayTransfer.getOrderTitle()); } if (aliPayTransfer.getRemark() != null) { alipay.setRemark(aliPayTransfer.getRemark()); } //轉成json格式放入
String json = new Gson().toJson(alipay); request.setBizContent(json); Map<String, Object> map = new HashMap<String, Object>(); try { AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request); map.put("code", response.getCode()); map.put("status", response.getStatus()); map.put("msg", response.getMsg()); map.put("outBizNo", response.getOutBizNo()); map.put("orderId", response.getOrderId()); map.put("payFundOrderId", response.getPayFundOrderId()); map.put("payTime", response.getTransDate()); if (response.isSuccess()) { map.put("des", "轉賬成功"); map.put("success", "true"); } else { log.error("轉賬失敗-->{}", response.getSubCode()); map.put("code", response.getCode()); map.put("des", "轉賬失敗"); map.put("success", "false"); map.put("sub_code", response.getSubCode()); map.put("sub_msg", response.getSubMsg()); } return map; } catch (AlipayApiException e) { log.error("轉賬出錯-->{}", e.getMessage()); e.printStackTrace(); map.put("success", "false"); map.put("des", "轉賬異常!"); } } catch (Exception e) { e.printStackTrace(); } throw new CustomException("系統繁忙,請稍后重試"); }
注意: identity_type為ALIPAY_LOGON_ID時一定要傳收款名稱(name), identity_type為ALIPAY_USER_ID時,identity傳的是pId,也就是支付寶的合作伙伴id,以2088開頭的。
方式二
新接口: alipay.fund.trans.uni.transfer
public Map<String, Object> aliPaySingleTransfer(AliPayTransferParam aliPayTransfer) { try { CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); certAlipayRequest.setServerUrl(aliPayConfig.getSERVER_URL()); certAlipayRequest.setAppId(aliPayConfig.getAPPID()); certAlipayRequest.setPrivateKey(aliPayConfig.getPAY_PRIVATE_KEY()); certAlipayRequest.setFormat(aliPayConfig.getFORMAT()); certAlipayRequest.setCharset(aliPayConfig.getCHARSET()); certAlipayRequest.setSignType(aliPayConfig.getSIGN_TYPE()); //獲取各證書路徑
AliPayConfig certPath = getCertPath(); //設置應用公鑰證書路徑
certAlipayRequest.setCertPath(certPath.getAPPLICATION_PUBLIC_KEY()); //設置支付寶公鑰證書路徑
certAlipayRequest.setAlipayPublicCertPath(certPath.getAliPay_PUBLIC_CERT()); //設置支付寶根證書路徑
certAlipayRequest.setRootCertPath(certPath.getROOT_CERT_PATH()); //支付寶客戶端
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest); AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); request.setBizContent("{" +
"\"out_biz_no\":\"" + aliPayTransfer.getOutBizNo() + "\"," +
"\"trans_amount\":" + aliPayTransfer.getTransAmount() + "," +
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
"\"biz_scene\":\"DIRECT_TRANSFER\"," +
"\"order_title\":\"" + aliPayTransfer.getOrderTitle() + "\"," +
"\"payee_info\":{" +
"\"identity\":\"" + aliPayTransfer.getIdentity() + "\"," +
"\"identity_type\":\"ALIPAY_LOGON_ID\"," +
"\"name\":\"" + aliPayTransfer.getName() + "\"" +
" }," +
"\"remark\":\"" + aliPayTransfer.getRemark() + "\"" +
" }"); AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request); //請求結束的返回參數
Map<String, Object> map = new HashMap<>(); map.put("code", response.getCode()); map.put("status", response.getStatus()); map.put("msg", response.getMsg()); map.put("outBizNo", response.getOutBizNo()); map.put("orderId", response.getOrderId()); map.put("payFundOrderId", response.getPayFundOrderId()); map.put("payTime", response.getTransDate()); if (response.isSuccess()) { map.put("des", "轉賬成功"); } else { map.put("des", "轉賬失敗"); map.put("subCode", response.getSubCode()); map.put("subMsg", response.getSubMsg()); log.info("提現失敗-->{}", response.getSubMsg()); } return map; } catch (Exception e) { log.error("轉賬異常-->{}", e.getMessage()); e.printStackTrace(); } throw new CustomException("系統繁忙,請稍后重試"); }
方式三
這是舊接口,支付寶官方已不再維護
alipay.fund.trans.toaccount.transfer
public Map<String, Object> aliPayTransfer(AliPayTransferParam aliPayTransfer) { try { CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); certAlipayRequest.setServerUrl(aliPayConfig.getSERVER_URL()); certAlipayRequest.setAppId(aliPayConfig.getAPPID()); certAlipayRequest.setPrivateKey(aliPayConfig.getPAY_PRIVATE_KEY()); certAlipayRequest.setFormat(aliPayConfig.getFORMAT()); certAlipayRequest.setCharset(aliPayConfig.getCHARSET()); certAlipayRequest.setSignType(aliPayConfig.getSIGN_TYPE()); try { //獲取各證書路徑
AliPayConfig certPath = getCertPath(); //設置應用公鑰證書路徑
certAlipayRequest.setCertPath(certPath.getAPPLICATION_PUBLIC_KEY()); //設置支付寶公鑰證書路徑
certAlipayRequest.setAlipayPublicCertPath(certPath.getAliPay_PUBLIC_CERT()); //設置支付寶根證書路徑
certAlipayRequest.setRootCertPath(certPath.getROOT_CERT_PATH()); } catch (Exception e) { e.printStackTrace(); } //構造Client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest); //實例化接口
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest (); AliPay alipay = new AliPay(); PayeeInfo payerInfo = new PayeeInfo(); payerInfo.setIdentity(aliPayTransfer.getIdentity()); payerInfo.setIdentity_type("ALIPAY_LOGON_ID"); payerInfo.setName(aliPayTransfer.getName()); alipay.setPayee_info(payerInfo); alipay.setPayee_type("ALIPAY_LOGONID"); alipay.setOut_biz_no(aliPayTransfer.getOutBizNo()); alipay.setAmount(aliPayTransfer.getTransAmount()); alipay.setProduct_code("TRANS_ACCOUNT_NO_PWD"); alipay.setBiz_scene("DIRECT_TRANSFER"); alipay.setOrder_title("提現成功"); alipay.setRemark("謝謝您的理解"); //轉成json格式放入
String json = new Gson().toJson(alipay); request.setBizContent(json); AlipayFundTransToaccountTransferResponse response = null; Map<String, Object> map = new HashMap<String, Object>(); try { response = alipayClient.certificateExecute(request); if ("10000".equals(response.getCode())) { map.put("code", response.getCode()); map.put("success", "true"); return map; } else { map.put("code", response.getCode()); map.put("des", "轉賬成功"); map.put("sub_code", response.getSubCode()); map.put("success", "false"); map.put("sub_msg", response.getSubMsg()); return map; } } catch (AlipayApiException e) { e.printStackTrace(); map.put("success", "false"); map.put("des", "轉賬失敗!"); } } catch (Exception e) { e.printStackTrace(); } return null; }
查詢支付寶轉賬訂單
public Map<String, Object> queryAliPayTransfer(AliPayTransferParam aliPayTransfer) { try { //構造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); //設置網關地址
certAlipayRequest.setServerUrl(aliPayConfig.getSERVER_URL()); //設置應用Id
certAlipayRequest.setAppId(aliPayConfig.getAPPID()); //設置應用私鑰
certAlipayRequest.setPrivateKey(aliPayConfig.getPAY_PRIVATE_KEY()); //設置請求格式,固定值json
certAlipayRequest.setFormat(aliPayConfig.getFORMAT()); //設置字符集
certAlipayRequest.setCharset(aliPayConfig.getCHARSET()); //設置簽名類型
certAlipayRequest.setSignType(aliPayConfig.getSIGN_TYPE()); //獲取各證書路徑
AliPayConfig certPath = getCertPath(); //設置應用公鑰證書路徑
certAlipayRequest.setCertPath(certPath.getAPPLICATION_PUBLIC_KEY()); //設置支付寶公鑰證書路徑
certAlipayRequest.setAlipayPublicCertPath(certPath.getAliPay_PUBLIC_CERT()); //設置支付寶根證書路徑
certAlipayRequest.setRootCertPath(certPath.getROOT_CERT_PATH()); //構造client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest); AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest(); request.setBizContent("{" +
"\"out_biz_no\":\"" + aliPayTransfer.getOutBizNo() + "\"," +
"\"order_id\":\"" + aliPayTransfer.getOrderId() + "\"" +
"}"); AlipayFundTransOrderQueryResponse response = alipayClient.certificateExecute(request); Map<String, Object> map = new HashMap<>(); if (response.isSuccess()) { map.put("code", response.getCode()); map.put("status", response.getStatus()); map.put("msg", response.getMsg()); map.put("outBizNo", response.getOutBizNo()); map.put("orderId", response.getOrderId()); map.put("payTime", response.getPayDate()); } else { System.out.println("調用失敗"); map.put("subCode", response.getSubCode()); map.put("subMsg", response.getSubMsg()); log.error(response.getMsg()); } return map; } catch (Exception e) { e.printStackTrace(); } throw new CustomException("系統繁忙,請稍后重試"); }
附加類
轉賬參數類:AliPay
/** * 商戶單號 必選 */
private String out_biz_no; /** * 轉賬金額 必選 */
private String trans_amount; /** * 業務產品碼,單筆無密轉賬到支付寶賬戶固定為: TRANS_ACCOUNT_NO_PWD; * 收發現金紅包固定為: STD_RED_PACKET * 必選 */
private String product_code; /** * 業務備注 可選 */
private String remark; /** * 轉賬標題 可選 */
private String order_title; /** * 描述特定的業務場景,可傳的參數如下: * DIRECT_TRANSFER:單筆無密轉賬到支付寶,B2C現金紅包; * PERSONAL_COLLECTION:C2C現金紅包-領紅包 * 可選 */
private String biz_scene; /** * 收款信息 必選 */
private PayeeInfo payee_info;
這里需要注意的是:為了預防出錯,這里的參數命名跟按照官方文檔的命名一致
官方文檔地址:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer
收款人信息類: PayeeInfo
/** * 手機號碼 或者郵箱 或者PID 參與方的唯一標識 * pid是合作伙伴身份,是支付寶賬號對應的唯一用戶號ID */
private String identity; /** * ALIPAY_USER_ID:支付寶的會員ID, PID 例如:2088123412341234 * ALIPAY_LOGON_ID: 支付寶登錄號, 支持郵箱和手機號格式 */
private String identity_type; /** * 參與方真實姓名,如果非空,將校驗收款支付寶賬號姓名一致性。當identity_type=ALIPAY_LOGON_ID時,本字段必填。 */
private String name;
支付寶轉賬配置類:AliPayConfig
package com.cainaer.transaction.config; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * 支付寶轉賬配置類 * 通過支付寶開放平台開發助手app獲取秘鑰(公鑰、私鑰),然后拿公鑰去支付寶開放平台獲取支付寶公鑰 * 如果還需要加簽成證書,那么就得到應用公鑰證書和支付寶公鑰證書 * 應用證書是可以解析應用公鑰的,注意: 應用私鑰請妥善保管,忘記了只能重新提取,是解析不出來的 * 支付寶公鑰證書可以解析支付寶公鑰的 * * @author serene * @since 2020-09-11 */ @Data @Component public class AliPayConfig { /** * 支付寶登錄號,支持郵箱和手機號格式。 */
private String PAYEE_TYPE = "ALIPAY_LOGONID"; /** * 服務器網址 */ @Value("${AliPayApi.SERVER_URL}") private String SERVER_URL; /** * 商戶appId */ @Value("${AliPayApi.APPID}") private String APPID; /** * 應用私鑰 應用公鑰可以通過應用公鑰證書解析 */ @Value("${AliPayApi.PRIVATE_KEY}") private String PAY_PRIVATE_KEY; /** * 格式 僅支持JSON */ @Value("${AliPayApi.FORMAT}") private String FORMAT; /** * 編碼 UTF-8、GBK */ @Value("${AliPayApi.CHAR_SET}") private String CHARSET; /** * 加密方式 RSA2 */ @Value("${AliPayApi.SIGN_TYPE}") private String SIGN_TYPE; /** * 應用公鑰證書 */ @Value("${AliPayApi.APPLICATION_PUBLIC_KEY_CERT}") private String APPLICATION_PUBLIC_KEY; /** * 支付寶公鑰證書 */ @Value("${AliPayApi.AliPay_PUBLIC_CERT}") private String AliPay_PUBLIC_CERT; /** * 支付寶根證書 */ @Value("${AliPayApi.ROOT_CERT_PATH}") private String ROOT_CERT_PATH; }
獲取證書路徑
/** * 獲取證書路徑 * * @return 各證書數據 * @throws FileNotFoundException */
private AliPayConfig getCertPath() throws FileNotFoundException { AliPayConfig aliPay = new AliPayConfig(); //設置應用公鑰證書路徑
String certPath = String.valueOf(ResourceUtils.getFile(aliPayConfig.getAPPLICATION_PUBLIC_KEY())); aliPay.setAPPLICATION_PUBLIC_KEY(certPath); //設置支付寶公鑰證書路徑
String aliPayPublicCertPath = String.valueOf(ResourceUtils.getFile(aliPayConfig.getAliPay_PUBLIC_CERT())); aliPay.setAliPay_PUBLIC_CERT(aliPayPublicCertPath); //設置支付寶根證書路徑
String rootCertPath = String.valueOf(ResourceUtils.getFile(aliPayConfig.getROOT_CERT_PATH())); aliPay.setROOT_CERT_PATH(rootCertPath); return aliPay; }
使用應用證書解析應用公鑰
String publicKey = AlipaySignature.getAlipayPublicKey("應用公鑰證書絕對路徑"); //輸出應用公鑰的值
System.out.println("publicKey:"+publicKey);
使用支付寶證書解析支付寶公鑰
String alipaypublicKey = AlipaySignature.getAlipayPublicKey("支付寶公鑰證書絕對路徑"); //輸出支付寶公鑰的值
System.out.println("alipaypublicKey:"+alipaypublicKey);
如有問題請下方留言哦......