支持沙盒环境的测试
此项目已开源欢迎Start、PR、发起Issues一起讨论交流共同进步
https://github.com/Javen205/IJPay
http://git.oschina.net/javen205/IJPay
提现到个人支付宝官方的名称是单笔转账到支付宝账户
1、创建应用并获取APPID
如果没有在开发平台创建应用就得创建一个《开放平台应用创建指南》
如果之前有创建过应用那么就可以直接添加功能
如果只是测试不上线可以跳过上面的步骤,直接使用沙盒环境测试
2、配置密钥
可以参考《配置应用环境》
3、下载服务端SDK
4、使用服务端SDK
4.1 初始化SDK
AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
参数说明可以参考关键参数说明
4.2 接口调用
调用流程
说明:
1、如果商户重复请求转账,支付宝会幂等返回成功结果,商户必须对重复转账的业务做好幂等处理;如果不判断,存在潜在的风险,商户自行承担因此而产生的所有损失。
2、如果调用alipay.fund.trans.toaccount.transfer掉单时,或返回结果code=20000时,或返回结果code=40004,sub_code= SYSTEM_ERROR时,请调用alipay.fund.trans.order.query发起查询,如果未查询到结果,请保持原请求不变再次请求alipay.fund.trans.toaccount.transfer接口。
3、商户处理转账结果时,对于错误码的处理,只能使用sub_code作为后续处理的判断依据,不可使用sub_msg作为后续处理的判断依据。
4.3 SDK的调用
主要涉及到两个接口
4.4 单笔转账到支付宝账户接口alipay.fund.trans.toaccount.transfer 封装
直接调用转账接口如果返回不是
Success
就调用转账查询接口
/**
* 单笔转账到支付宝账户
* https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&treeId=193&articleId=106236&docType=1
* @param content
* @return
* @throws AlipayApiException
*/
public static boolean transfer(AlipayFundTransToaccountTransferModel model) throws AlipayApiException{
AlipayFundTransToaccountTransferResponse response = transferToResponse(model);
String result = response.getBody();
log.info("transfer result>"+result);
System.out.println("transfer result>"+result);
if (response.isSuccess()) {
return true;
} else {
//调用查询接口查询数据
JSONObject jsonObject = JSONObject.parseObject(result);
String out_biz_no = jsonObject.getJSONObject("alipay_fund_trans_toaccount_transfer_response").getString("out_biz_no");
AlipayFundTransOrderQueryModel queryModel = new AlipayFundTransOrderQueryModel();
model.setOutBizNo(out_biz_no);
boolean isSuccess = transferQuery(queryModel);
if (isSuccess) {
return true;
}
}
return false;
}
public static AlipayFundTransToaccountTransferResponse transferToResponse(AlipayFundTransToaccountTransferModel model) throws AlipayApiException{
AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}
4.5 查询转账订单接口alipay.fund.trans.order.query 封装
/**
* 转账查询接口
* @param content
* @return
* @throws AlipayApiException
*/
public static boolean transferQuery(AlipayFundTransOrderQueryModel model) throws AlipayApiException{
AlipayFundTransOrderQueryResponse response = transferQueryToResponse(model);
log.info("transferQuery result>"+response.getBody());
System.out.println("transferQuery result>"+response.getBody());
if(response.isSuccess()){
return true;
}
return false;
}
public static AlipayFundTransOrderQueryResponse transferQueryToResponse(AlipayFundTransOrderQueryModel model) throws AlipayApiException{
AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}
5、 接口测试
/**
* 单笔转账到支付宝账户
* https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&treeId=193&articleId=106236&docType=1
*/
public void transfer() {
boolean isSuccess = false;
String total_amount = "100";
AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
model.setOutBizNo(StringUtils.getOutTradeNo());//生成订单号
model.setPayeeType("ALIPAY_LOGONID");//固定值
model.setPayeeAccount("abpkvd0206@sandbox.com");//转账收款账户
model.setAmount(total_amount);
model.setPayerShowName("测试退款");
model.setPayerRealName("沙箱环境");//账户真实名称
model.setRemark("javen测试单笔转账到支付宝");
try {
isSuccess = AliPayApi.transfer(model);
} catch (Exception e) {
e.printStackTrace();
}
renderJson(isSuccess);
}
故意把账户真实名称写错
{
"alipay_fund_trans_toaccount_transfer_response": {
"code": "40004",
"msg": "Business Failed",
"sub_code": "PAYER_USER_INFO_ERROR",
"sub_msg": "付款用户姓名或其它信息不一致",
"out_biz_no": "051023044814944"
},
"sign": "Zbm9lI9GbTlLbYsPQoJhd5y7+oevOInPFoKlRWp2064VUPZYUGBJRiM/8Ip8Vfz4MDhu+0Uc3gEzvoXk1O6eVj7bAPjGLc5cZI3gQNmbogTxeK/4eGgIjxJBKK46r6rzKgK2/e7YEBmExi6hACbo3inBqX0OnaIxIbedZnYY2qrkNdhIjiD1G/EWJNH846IEwhwLkihV7vVKXhIgfmfKmGu5jE7aNddwxKhAK8fAzTR7JOs8p/ZOcLD9/RHfUP1ro4HoNlUOrFUZfhxRuUEFwLxvcJon0HkcO6dnjNnXQx3jh/Ne3632SpWca1pZczervU3/z9/C0LVflQWna42t9g=="
}
正确返回结果
{
"alipay_fund_trans_toaccount_transfer_response": {
"code": "10000",
"msg": "Success",
"order_id": "20170510110070001500460000004431",
"out_biz_no": "051023003214944",
"pay_date": "2017-05-10 23:00:30"
},
"sign": "hedaOEcrS8CwzcLNFAQhLWJnmevaA4a+SsNuzuuyBHABUjJ+ZvagMoS1/eUpRIHfwXVOLxGVjCCtJzi4Irclqu2Roz9aHo8ROkNKFKbw66lcT2dOo9AWCYw8UVhQDUjjSZ/d+lu9nnpHPf3ZPPdFHvziBo6ghZF0DRiIX/9ZVx7uH7grFJb8SRbCbcF5C7eouJU8Aw9sMdu/XjREdlW7pLvjeinzouOLbIpICRP33JtGC/KhgdQltDzlXHtVgi9xWRJVXJVp7+jtDRbRP+V+ImY9NqYKtpfxtTBZZ1bW6nOxJaMV7ePgC/6GpDIyWjg+LdHQ09eeBtTy4XCOxtGe1g=="
}
推荐阅读:
微信、支付宝App支付-JPay0.0.2发布
支付宝支付-刷卡支付(条码支付)
支付宝支付-扫码支付
一张二维码集成微信、支付宝支付
支付宝Wap支付你了解多少?
Android版-支付宝APP支付