首先引入支付宝的jar
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.49.ALL</version>
</dependency>
然后调用支付宝转账
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "APP_ID", "APP_PRIVATE_KEY", "JSON", "UTF-8", "ALIPAY_PUBLIC_KEY", "RSA2");
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
request.setBizContent("{" +
"\"out_biz_no\":\""+System.currentTimeMillis()+"\"," +
"\"trans_amount\":0.1," +
"\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," +
"\"biz_scene\":\"DIRECT_TRANSFER\"," +
"\"order_title\":\"支付宝转账测试\"," +
"\"payee_info\":{" +
"\"identity\":\"你的支付宝号\"," +
"\"identity_type\":\"ALIPAY_LOGON_ID\"," +
"\"name\":\"你的真实姓名\"" +
" }," +
"\"remark\":\"单笔转账\"," +
" }");
AlipayFundTransToaccountTransferResponse response=null;
Map<String, Object> map = new HashMap<String,Object>();
try{
log.info("转账发送---,参数为:{}",request.getBizContent());
response = alipayClient.execute(request);
if("10000".equals(response.getCode())){
map.put("code", response.getCode());
map.put("success", "true");
map.put("des", "转账成功");
log.info("转账成功,返回参数:{}",response);
}else{
map.put("code", response.getCode());
map.put("sub_code", response.getSubCode());//详情状态码
map.put("success", "false");
map.put("sub_msg", response.getSubMsg());//详情原因
log.info("转账失败,返回参数:{}",response);
}
}catch(AlipayApiException e){
e.printStackTrace();
map.put("success", "false");
map.put("des", "转账失败!");
}
return map;
}