证书模式及非证书模式转账
- 查询证书路径
public String queryPath() throws FileNotFoundException, ServerException {
String basePath = ResourceUtils.getURL("classpath:").getPath();
basePath = this.initCart(basePath);
return basePath;
}
private String initCart(String basePath) throws ServerException {
if (basePath.contains("jar!")) {
if (basePath.startsWith("file:")) {
basePath = basePath.replace("file:", "");
}
String appCertPath = aliPayProperties.getAppCertPath();
this.checkAndcopyCart(basePath, appCertPath);
String alipayCertPath = aliPayProperties.getAlipayCertPath();
this.checkAndcopyCart(basePath, alipayCertPath);
String alipayRootCertPath = aliPayProperties.getAlipayRootCertPath();
this.checkAndcopyCart(basePath, alipayRootCertPath);
}
return basePath;
}
private void checkAndcopyCart(String basePath, String path) throws ServerException {
InputStream cartInputStream = null;
OutputStream cartOutputStream = null;
String cartPath = basePath + path;
File cartFile = new File(cartPath);
File parentFile = cartFile.getParentFile();
parentFile.mkdirs();
try {
if (!cartFile.exists()) {
cartInputStream = ClassUtils.getDefaultClassLoader().getResourceAsStream(path);
cartOutputStream = new FileOutputStream(cartFile);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = cartInputStream.read(buf)) != -1) {
cartOutputStream.write(buf, 0, bytesRead);
}
cartInputStream.close();
cartOutputStream.close();
}
} catch (IOException e) {
throw new ServerException(e.getMessage());
}
}
- 调用支付宝转账接口 如果非证书模式 则可以直接将 构造client 之前的都不要
String basePath = this.queryPath();
// 构造client
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
// 设置网关地址
certAlipayRequest.setServerUrl(aliPayProperties.getGatewayUrl());
// 设置应用Id
certAlipayRequest.setAppId(aliPayProperties.getAppid());
// 设置应用私钥
certAlipayRequest.setPrivateKey(aliPayProperties.getAppPrivateKey());
// 设置请求格式,固定值json
certAlipayRequest.setFormat("json");
// 设置字符集
certAlipayRequest.setCharset(aliPayProperties.getCharset());
// 设置签名类型
certAlipayRequest.setSignType(aliPayProperties.getSignType());
// 设置应用公钥证书路径
certAlipayRequest.setCertPath(basePath + aliPayProperties.getAppCertPath());
// 设置支付宝公钥证书路径
certAlipayRequest.setAlipayPublicCertPath(basePath + aliPayProperties.getAlipayCertPath());
// 设置支付宝根证书路径
certAlipayRequest.setRootCertPath(basePath + aliPayProperties.getAlipayRootCertPath());
// 构造client
AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
Map<String, String> map = new HashMap<>();
map.put("out_biz_no", "自己平台订单Id");
map.put("biz_scene", "DIRECT_TRANSFER");
map.put("product_code", "TRANS_ACCOUNT_NO_PWD");
map.put("trans_amount", "转账金额");
map.put("order_title", "转账标题");
map.put("remark", "转账备注");
// 转账账户
Map<String, String> map2 = new HashMap<>();
map2.put("identity", "转账账户支付宝账户");
map2.put("identity_type", "ALIPAY_LOGON_ID");
map2.put("name", "转账账户真实姓名");
map.put("payee_info", FastJsonUtil.beanToJson(map2));
// 转成json格式放入
String json = FastJsonUtil.beanToJson(map);
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
request.setBizContent(json);
log.info("请求参数:" + request.getBizContent());
AlipayFundTransUniTransferResponse response = null;
try {
response = alipayClient.certificateExecute(request);
if ("10000".equals(response.getCode())) {
log.info("打印日志返回结果-----" + response.getTransDate());
return Tuple.createTuple("0", "转账成功!");
} else {
log.info("打印日志返回结果-----" + response.getBody());
return Tuple.createTuple(response.getCode(), response.getSubMsg());
}
} catch (AlipayApiException e) {
log.info("打印日志返回结果-----" + response);
e.printStackTrace();
return Tuple.createTuple("1", "转账失败!");
}
注意 付款方余额必须大于1元 否则会提示付款方余额不够