接口名称:统一收单交易撤销接口
英文名称:alipay.trade.cancel
主要用于只有发生支付系统超时或者支付结果未知时可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。
一、文档地址
官方文档地址:[url]https://docs.open.alipay.com/api_1/alipay.trade.cancel[/url]
二、开发前准备工作
调用步骤:[url]https://openclub.alipay.com/read.php?tid=12194&fid=69[/url]
注意事项:1、支持沙箱测试;2、需签约并添加功能;3、支持第三方调用。
如何签约以及签约无法成功等相关签约问题:[url]https://openclub.alipay.com/read.php?tid=276&fid=72[/url]
注:该接口为辅助接口,只能签约当面付后,用于当面付接口。
三、接口集成代码示例
1、参数说明
(1)out_trade_no:商户订单号,该参数值为退款接口中设置的out_trade_no参数值;
(2)trade_no:支付宝交易号,该参数值为支付接口订单创建后,退款接口设置的trade_no参数值;
注:out_trade_no和trade_no至少必传其中一个参数,两者同时设置优先取值trade_no。
2、JAVA版请求示例
public static void main(String[] args) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); AlipayTradeCancelModel model = new AlipayTradeCancelModel(); //订单号和支付宝交易号二选一传入即可 model.setOutTradeNo("sao201881610515614521"); //model.setTradeNo("2019032622001494891000009752"); request.setBizModel(model); AlipayTradeCancelResponse response = alipayClient.execute(request); System.out.println(response.getBody()); }
响应参数
{"alipay_trade_cancel_response":{"code":"10000","msg":"Success","action":"close","out_trade_no":"sao201881610515614521","retry_flag":"N","trade_no":"2019032622001494891000009752"},"sign":"Ofs6d6BgfDAurk9li1FGBmqEZfCQrTiobx6/61ws6POInXaRckOCIyyUiS7r87GIZzwI0z+k1eqpfV1ymbHSpSViO2/r4yTptsQOVvLhQ/KGEdi4k8BJm3OH8Sd2t/VJdaQ2mKJjycPqaovSIzma6ZjhhglawZ9r8/RvviwTT0he3Q7yjE1OJK+0qZ2O7Df/QnNTUnQPxSW6QTG9vJsfMEeFSa763f7xdMSRv+rO/qUYUgrfZ41g/fAdbHvMMphv89fSJRJ1zNRhXeagFwVD8+9pJlIvq02E5YULiMGqvYT2h1YCm+bH4QWhnF1cJb+Cj+uj9LaF/A4xjZLeQ2uYLw=="}
3、PHP版请求示例
<?php require_once 'AopSdk.php'; $aop = new AopClient (); $aop->appId = '填写您的appid'; $aop->rsaPrivateKey = '填写您的私钥'; $aop->alipayrsaPublicKey='填写您的支付宝公钥'; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset='utf-8'; $aop->format='json'; $date=date("YmdHis"); $arr=range(1000,9999); shuffle($arr); $request = new AlipayTradeCloseRequest (); $request->setBizContent("{" . ""out_trade_no":"sao201881610515614521"," . ""trade_no":"2017080921001004680200182368"" . " }"); $result = $aop->execute($request); //第三方调用 //$result = $aop->execute($request,"","app_auth_token"); echo json_encode($result);
响应参数
{"alipay_trade_cancel_response":{"code":"10000","msg":"Success","action":"close","out_trade_no":"sao201881610515614521","retry_flag":"N","trade_no":"2019032622001494891000009752"},"sign":"Ofs6d6BgfDAurk9li1FGBmqEZfCQrTiobx6/61ws6POInXaRckOCIyyUiS7r87GIZzwI0z+k1eqpfV1ymbHSpSViO2/r4yTptsQOVvLhQ/KGEdi4k8BJm3OH8Sd2t/VJdaQ2mKJjycPqaovSIzma6ZjhhglawZ9r8/RvviwTT0he3Q7yjE1OJK+0qZ2O7Df/QnNTUnQPxSW6QTG9vJsfMEeFSa763f7xdMSRv+rO/qUYUgrfZ41g/fAdbHvMMphv89fSJRJ1zNRhXeagFwVD8+9pJlIvq02E5YULiMGqvYT2h1YCm+bH4QWhnF1cJb+Cj+uj9LaF/A4xjZLeQ2uYLw=="}
4、.NET版请求示例
IAopClient client = new DefaultAopClient(config.gatewayUrl, config.app_id, config.private_key, "json", "1.0", "RSA2", config.alipay_public_key, config.charset, false); AlipayTradeCancelRequest request = new AlipayTradeCancelRequest(); request.BizContent = "{" + ""out_trade_no":"20190326ygyg45484787400002"" + // ""trade_no":"20190321ygyg45447000001"" + "}"; AlipayTradeCancelResponse response = client.Execute(request);
响应参数返回示例
{"alipay_trade_cancel_response":{"code":"10000","msg":"Success","action":"close","out_trade_no":"20190326ygyg45484787400002","retry_flag":"N","trade_no":"2019032622001401091000012169"},"sign":"FyqFIz+w/HreJx9NJZ//BS8iMmDKl4EJAYclzJSE8hf6CFyh665+/Tqv4zLlSKHmrcGUYFtvkECCJaAyz7hYXWyESW+pFfpN+y2NqRbkCXb0eAtR8IzXNkUlPoTeIKFR9eKNbhV5SbcyHFGHNQnG3SBfWFTZ4cw9YVwHXv5n0TzjXNYVTgLFgRjlg4YFnWhVaClWnU3M/IRB7C2W1ohUMYvpaFfLb7HEOExoEzt7eY2eVv8WNGpw2dxojSREnUA7fj+jwLtQqBVgJCT+N34P58z3iamHAQnFtVfA/ywLIPzHHtGgOd9nRRFsmbcHa7jIMf767wkuY6TPil6/A/AZNw=="}
四、注意事项
1、撤销要求
(1)当面付接口产生的交易;
(2)撤销接口在提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。
2、调用撤销接口的作用
支付交易返回失败或支付系统超时,调用该接口撤销交易。
(1)如果此订单用户支付失败,支付宝系统会将此订单关闭,交易状态变成trade_close;
(2)如果用户支付成功,支付宝系统会将此订单资金退还给用户,交易状态变成trade_close。
如果还是报错,请按如下方式进行提问:
正确提问方式:使用环境(沙箱还是正式环境)+接口名称(中文+英文)+ 请求参数 + 报错信息详情