复制代码
/**
* 功能描述: 支付宝转账接口-公钥证书模式
*
* @param aliPayConfig 支付宝配置
* @param orderNo 系统订单号
* @param account 收款方帐号
* @param userName 收款方姓名
* @param amount 转账金额
**/
public static boolean transfer(AliPayConfig aliPayConfig, String orderNo, String account, String userName, String amount) throws AlipayApiException {
log.info("支付宝转账开始,订单号 = {}", orderNo);
//构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//设置网关地址
certAlipayRequest.setServerUrl(aliPayConfig.getServerUrl());
//设置应用Id
certAlipayRequest.setAppId(aliPayConfig.getAppId());
//设置应用私钥
certAlipayRequest.setPrivateKey(aliPayConfig.getPrivateKey());
//设置请求格式,固定值json
certAlipayRequest.setFormat(AliPayConfig.FORMAT);
//设置字符集
certAlipayRequest.setCharset(AliPayConfig.CHARSET);
//设置签名类型
certAlipayRequest.setSignType(AliPayConfig.SIGN_TYPE);
//设置应用公钥证书路径
certAlipayRequest.setCertPath(AliPayConfig.APP_CERT_PATH);
//设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertPath(AliPayConfig.PUBLIC_CERT_PATH);
//设置支付宝根证书路径
certAlipayRequest.setRootCertPath(AliPayConfig.ROOT_CERT_PATH);
//构造client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
AlipayFundTransToaccountTransferModel transferModel = new AlipayFundTransToaccountTransferModel();
//自身系统订单号,
transferModel.setOutBizNo(orderNo);
transferModel.setPayeeType(AliPayConfig.IDENTITY_TYPE);
transferModel.setPayeeAccount(account);
transferModel.setAmount(amount);
transferModel.setPayeeRealName(userName);
transferModel.setRemark("提款");
request.setBizModel(transferModel);
log.info("支付宝转账请求参数 = {}", JSONObject.toJSON(transferModel));
try {
AlipayFundTransToaccountTransferResponse response = alipayClient.certificateExecute(request);
if (AliPayConfig.SUCCESS_CODE.equals(response.getCode())) {
log.info("转账成功,返回信息{}", JSONObject.toJSON(response));
return true;
} else {
log.error("转账失败,错误信息 = {}", JSONObject.toJSON(response));
return false;
}
} catch (AlipayApiException e) {
log.error("转账异常,异常信息 = {}", e.getMessage());
return false;
}
}
评论