您的位置:首页 > 其它

支付宝支付-常用支付API详解(查询、退款、提现等)

2017-05-13 17:08 453 查看
本文章已同步发布到简书 http://www.jianshu.com/p/b6e6291709c7

1、前言

此项目已开源欢迎Start、PR、发起Issues一起讨论交流共同进步

https://github.com/Javen205/IJPay

http://git.oschina.net/javen205/IJPay

前面几篇文件详细介绍了 支付宝提现扫码支付条码支付Wap支付App支付

支付宝支付-提现到个人支付宝

支付宝支付-扫码支付

支付宝支付-刷卡支付(条码支付)

支付宝Wap支付你了解多少?

Android版-支付宝APP支付

一张二维码集成微信、支付宝支付

其中也断断续续的提到了一些接口。本片文章主要是总结支付宝支付中常用的一些接口

2、常用的接口总结

这里使用表格的方式列出 官方接口列表以及详细的参数说明

API列表类型描述使用场景
alipay.trade.query免费统一收单线下交易查询提供所有支付宝支付订单的查询
alipay.trade.refund免费统一收单交易退款接口买家或者卖家的原因需要退款时
alipay.trade.fastpay.refund.query免费统一收单交易退款查询查看退款请求是否执行成功
alipay.trade.pay免费统一收单交易支付接口将二维码或条码信息/声波信息通过本接口上送至支付宝发起支付。
alipay.trade.precreate免费统一收单线下交易预创建二维码支付
alipay.trade.cancel免费统一收单交易撤销接口撤销交易
alipay.trade.create免费统一收单交易创建接口创建下单
alipay.trade.close免费统一收单交易关闭接口用于交易创建后,用户在一定时间内未进行支付时可以通过此接口关闭订单
alipay.trade.order.settle免费统一收单交易结算接口用于在线下场景交易支付后,进行结算
alipay.fund.trans.toaccount.transfer免费单笔转账到支付宝账户接口支付宝提现
alipay.fund.trans.order.query免费查询转账订单接口提现结果查询
alipay.data.dataservice.bill.downloadurl.query免费查询对账单下载地址为方便商户快速查账

3、使用服务端SDK封装接口

3.1 服务端SDK下载及其使用方法

参考 开放平台服务端SDK

Maven项目引用JAR包可以参考 支付宝Wap支付你了解多少? 里面有详细的介绍

重要说明

1、接口使用的编码格式为 UTF-8

2、接口数据交互使用的是 json

3、接口加密的模式使用官方推荐的 RSA2

4、本片文章主要是介绍Java的使用方法与封装

3.2 初始化SDK

在SDK调用前需要进行初始化

AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);


关键参数说明:

配置参数示例值解释获取方式/示例值
URL支付网关(固定)正式环境:https://openapi.alipay.com/gateway.do 沙箱环境:https://openapi.alipaydev.com/gateway.do
APP_IDAPPID即创建应用后生成获取见上面创建应用并获取APPID
APP_PRIVATE_KEY开发者应用私钥,由开发者自己生成获取详见上面配置密钥
FORMAT参数返回格式,只支持jsonjson(固定)
CHARSET请求和签名使用的字符编码格式,支持GBK和UTF-8开发者根据实际工程编码配置
ALIPAY_PUBLIC_KEY支付宝公钥,由支付宝生成获取详见上面配置密钥
SIGN_TYPE商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2RSA2
3.3 API接口封装

3.3.1 alipay.trade.query接口封装

该接口提供所有支付宝支付订单的查询,商户可以通过该接口主动查询订单状态,完成下一步的业务逻辑。 需要调用查询接口的情况: 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知; 调用支付接口后,返回系统错误或未知交易状态情况; 调用alipay.trade.pay,返回INPROCESS的状态; 调用alipay.trade.cancel之前,需确认支付状态;

/**
* 交易查询接口
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.8H2JzG&docType=4&apiId=757 * @param bizContent
* @return
* @throws AlipayApiException
*/
public static boolean isTradeQuery(AlipayTradeQueryModel model) throws AlipayApiException{
AlipayTradeQueryResponse response = tradeQuery(model);
if(response.isSuccess()){
return true;
}
return false;
}

public static AlipayTradeQueryResponse  tradeQuery(AlipayTradeQueryModel model) throws AlipayApiException{
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}


3.3.2 alipay.trade.refund 接口封装

当交易发生之后一段时间内,由于买家或者卖家的原因需要退款时,卖家可以通过退款接口将支付款退还给买家,支付宝将在收到退款请求并且验证成功之后,按照退款规则将支付款按原路退到买家帐号上。 交易超过约定时间(签约时设置的可退款时间)的订单无法进行退款 支付宝退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。一笔退款失败后重新提交,要采用原来的退款单号。总退款金额不能超过用户实际支付金额

/**
* 退款
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.SAyEeI&docType=4&apiId=759 * @param content
* @return
* @throws AlipayApiException
*/
public static String tradeRefund(AlipayTradeRefundModel model) throws AlipayApiException{
AlipayTradeRefundResponse response = tradeRefundToResponse(model);
return response.getBody();
}
public static AlipayTradeRefundResponse tradeRefundToResponse(AlipayTradeRefundModel model) throws AlipayApiException{
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}


3.3.3 alipay.trade.fastpay.refund.query接口封装

商户可使用该接口查询自已通过alipay.trade.refund提交的退款请求是否执行成功。 该接口的返回码10000,仅代表本次查询操作成功,不代表退款成功。如果该接口返回了查询数据,则代表退款成功,如果没有查询到则代表未退款成功,可以调用退款接口进行重试。重试时请务必保证退款请求号一致。

/**
* 退款查询
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.KQeTSa&apiId=1049&docType=4 * @param model
* @return
* @throws AlipayApiException
*/
public static String tradeRefundQuery(AlipayTradeFastpayRefundQueryModel model) throws AlipayApiException{
AlipayTradeFastpayRefundQueryResponse response = tradeRefundQueryToResponse(model);
return response.getBody();
}

public static AlipayTradeFastpayRefundQueryResponse tradeRefundQueryToResponse(AlipayTradeFastpayRefundQueryModel model) throws AlipayApiException{
AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}


3.3.4 alipay.trade.pay接口封装

收银员使用扫码设备读取用户手机支付宝“付款码”/声波获取设备(如麦克风)读取用户手机支付宝的声波信息后,将二维码或条码信息/声波信息通过本接口上送至支付宝发起支付。

/**
* 条形码支付、声波支付
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.XVqALk&apiId=850&docType=4 * @param notifyUrl
* @throws AlipayApiException
*/
public static String tradePay(AlipayTradePayModel model, String notifyUrl) throws AlipayApiException {
AlipayTradePayResponse response = tradePayToResponse(model,notifyUrl);
return response.getBody();
}

public static AlipayTradePayResponse tradePayToResponse(AlipayTradePayModel model, String notifyUrl) throws AlipayApiException{
AlipayTradePayRequest request = new AlipayTradePayRequest();
request.setBizModel(model);// 填充业务参数
request.setNotifyUrl(notifyUrl);
return alipayClient.execute(request); // 通过alipayClient调用API,获得对应的response类
}


3.3.5 alipay.trade.precreate 接口封装

收银员通过收银台或商户后台调用支付宝接口,生成二维码后,展示给用户,由用户扫描二维码完成订单支付。

/**
* 扫码支付
* https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.i0UVZn&treeId=193&articleId=105170&docType=1#s4 * @param notifyUrl
* @return
* @throws AlipayApiException
*/
public static String tradePrecreatePay(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
AlipayTradePrecreateResponse response = tradePrecreatePayToResponse(model,notifyUrl);
return response.getBody();
}
public static AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
request.setBizModel(model);
request.setNotifyUrl(notifyUrl);
return alipayClient.execute(request);
}


3.3.6 alipay.trade.cancel 接口封装

支付交易返回失败或支付系统超时,调用该接口撤销交易。如果此订单用户支付失败,支付宝系统会将此订单关闭;如果用户支付成功,支付宝系统会将此订单资金退还给用户。 注意:只有发生支付系统超时或者支付结果未知时可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。

/**
* 交易撤销接口
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.XInh6e&docType=4&apiId=866 * @param bizContent
* @return
* @throws AlipayApiException
*/
public static boolean isTradeCancel(AlipayTradeCancelModel model) throws AlipayApiException{
AlipayTradeCancelResponse response = tradeCancel(model);
if(response.isSuccess()){
return true;
}
return false;
}

public static AlipayTradeCancelResponse tradeCancel(AlipayTradeCancelModel model) throws AlipayApiException{
AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
request.setBizModel(model);
AlipayTradeCancelResponse response = alipayClient.execute(request);
return response;
}


3.3.7 alipay.trade.create 接口封装

商户通过该接口进行交易的创建下单

/**
* 统一收单交易创建接口
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.21yRUe&apiId=1046&docType=4 * @param model
* @param notifyUrl
* @return
* @throws AlipayApiException
*/
public static AlipayTradeCreateResponse tradeCreate(AlipayTradeCreateModel model, String notifyUrl) throws AlipayApiException{
AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
request.setBizModel(model);
request.setNotifyUrl(notifyUrl);
return alipayClient.execute(request);
}


3.3.8 alipay.trade.close 接口封装

用于交易创建后,用户在一定时间内未进行支付,可调用该接口直接将未付款的交易进行关闭。

/**
* 关闭订单
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.21yRUe&apiId=1058&docType=4 * @param model
* @return
* @throws AlipayApiException
*/
public static boolean isTradeClose(AlipayTradeCloseModel model) throws AlipayApiException{
AlipayTradeCloseResponse response = tradeClose(model);
if(response.isSuccess()){
return true;
}
return false;
}

public static AlipayTradeCloseResponse tradeClose(AlipayTradeCloseModel model) throws AlipayApiException{
AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
request.setBizModel(model);
return alipayClient.execute(request);

}


3.3.9 alipay.trade.order.settle接口封装

用于在线下场景交易支付后,进行结算

/**
* 交易结算接口
* https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.nl0RS3&docType=4&apiId=1147 * @param bizContent
* @return
* @throws AlipayApiException
*/
public static boolean isTradeOrderSettle(AlipayTradeOrderSettleModel model) throws AlipayApiException{
AlipayTradeOrderSettleResponse  response  = tradeOrderSettle(model);
if(response.isSuccess()){
return true;
}
return false;
}

public static AlipayTradeOrderSettleResponse tradeOrderSettle(AlipayTradeOrderSettleModel model) throws AlipayApiException{
AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}


3.3.10 alipay.fund.trans.toaccount.transfer接口封装

可以参考 支付宝支付-提现到个人支付宝

3.3.11 alipay.fund.trans.order.query接口封装

可以参考 支付宝支付-提现到个人支付宝

3.3.12 alipay.data.dataservice.bill.downloadurl.query 接口封装

为方便商户快速查账,支持商户通过本接口获取商户离线账单下载地址

/**
* 查询对账单下载地址
* @param bizContent
* @return
* @throws AlipayApiException
*/
public static String billDownloadurlQuery(AlipayDataDataserviceBillDownloadurlQueryModel model) throws AlipayApiException{
AlipayDataDataserviceBillDownloadurlQueryResponse response =  billDownloadurlQueryToResponse(model);
return response.getBillDownloadUrl();
}

public static AlipayDataDataserviceBillDownloadurlQueryResponse  billDownloadurlQueryToResponse (AlipayDataDataserviceBillDownloadurlQueryModel model) throws AlipayApiException{
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
request.setBizModel(model);
return alipayClient.execute(request);
}


4、异步通知封装

将异步通知的参数转化为Map为验签做准备

toMap(HttpServletRequest request) {
System.out.println(">>>>" + request.getQueryString());
Map params = new HashMap();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
return params;
}" data-snippet-id="ext.fcfa3bb8273c208971e4c881dcfef78a" data-snippet-saved="false" data-codota-status="done">[code]/**
* 将异步通知的参数转化为Map
* @param request
* @return
*/
public static Map<String, String> toMap(HttpServletRequest request) {
System.out.println(">>>>" + request.getQueryString());
Map<String, String> params = new HashMap<String, String>();
Map<String, String[]> requestParams = request.getParameterMap();
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用。
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
return params;
}


使用
AlipaySignature.rsaCheckV1(....)
接口进行验证签名

params = AliPayApi.toMap(getRequest());

for (Map.Entry entry : params.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}

boolean verify_result = AlipaySignature.rsaCheckV1(params, AliPayApi.ALIPAY_PUBLIC_KEY, AliPayApi.CHARSET,
AliPayApi.SIGN_TYPE);

if (verify_result) {// 验证成功
// TODO 请在这里加上商户的业务逻辑程序代码 异步通知可能出现订单重复通知 需要做去重处理
System.out.println("notify_url 验证成功succcess");
renderText("success");
return;
} else {
System.out.println("notify_url 验证失败");
// TODO
renderText("failure");
return;
}
} catch (AlipayApiException e) {
e.printStackTrace();
renderText("failure");
}
}" data-snippet-id="ext.9ffa235d61d9fc29942cd3436763c19e" data-snippet-saved="false" data-codota-status="done">[code]public void notify_url() {
try {
// 获取支付宝POST过来反馈信息
Map<String, String> params = AliPayApi.toMap(getRequest());

for (Map.Entry<String, String> entry : params.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}

boolean verify_result = AlipaySignature.rsaCheckV1(params, AliPayApi.ALIPAY_PUBLIC_KEY, AliPayApi.CHARSET,
AliPayApi.SIGN_TYPE);

if (verify_result) {// 验证成功
// TODO 请在这里加上商户的业务逻辑程序代码 异步通知可能出现订单重复通知 需要做去重处理
System.out.println("notify_url 验证成功succcess");
renderText("success");
return;
} else {
System.out.println("notify_url 验证失败");
// TODO
renderText("failure");
return;
}
} catch (AlipayApiException e) {
e.printStackTrace();
renderText("failure");
}
}


此项目已开源欢迎Start、PR、发起Issues一起讨论交流共同进步

https://github.com/Javen205/IJPay

http://git.oschina.net/javen205/IJPay
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息