接口名称:统一收单线下交易查询
英文名称:alipay.trade.query
用于查询订单是否存在以及订单的详细信息;
一、文档地址
官方文档地址:[url]https://docs.open.alipay.com/api_1/alipay.trade.query/[/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]
注:该接口为辅助接口,只要签约当面付、手机网站支付、电脑网站支付和APP支付,四个支付接口中其中一个就可以调用。
三、接口集成代码示例
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); AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); //out_trade_no(外部订单号)和trade_no(支付宝交易号)二选一用 即可查询 AlipayTradeQueryModel model = new AlipayTradeQueryModel(); model.setOutTradeNo("20150320010101004498"); //model.setTradeNo("2019031222001494890500825908"); request.setBizModel(model); AlipayTradeQueryResponse response = alipayClient.execute(request); System.out.println(response.getBody());}
返回的响应参数示例:
{"alipay_trade_query_response":{"code":"10000","msg":"Success","buyer_logon_id":"uqq***@sandbox.com","buyer_pay_amount":"2.00","buyer_user_id":"2088102175794899","buyer_user_type":"PRIVATE","fund_bill_list":[{"amount":"2.00","fund_channel":"ALIPAYACCOUNT"}],"invoice_amount":"2.00","out_trade_no":"20150320010101004498","point_amount":"0.00","receipt_amount":"2.00","send_pay_date":"2019-03-12 10:50:09","total_amount":"2.00","trade_no":"2019031222001494890500825908","trade_status":"TRADE_SUCCESS"},"sign":"gK9/bRceHVXou0vX4U+GgQv+F9pgL15lDtpQqR/y2glaWywOvYUsmV1/5UpbVFOP28yXHMEQJxqwp8j81GuF8zQsXlYmdlaOgZ0BaTa3Eh82zftejcRkIwgiLhUg3ZQBfbpjExgazhF0CvGoNaauyIfoKICJF61vsvN0ISAOXEPze+/XVmzjTueFUn3OWoknaD/mxtlGYOe67AU5vc+ueOUklaxis0ERivJB+eo9DWUDA3NzRvms2arkaQ3BEdZ4+HMMI4wK94RxpUchMbZHmta+msMTSpDtRRxWgr9gT9XFw59ZgYr/rpZYPCHKAAp/RbhzOLpnpBlAE/eqLSZqeA=="}
3、PHP版请求示例
<?phprequire_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 AlipayTradeQueryRequest ();$request->setBizContent("{" . "\"out_trade_no\":\"20150320010101004498\"," . "\"trade_no\":\"2019031222001494890500825908\"" . " }");$result = $aop->execute($request); //第三方调用//$result = $aop->execute($request,"","app_auth_token"); var_dump($result);
返回的响应参数示例:
object(stdClass)[11] public 'alipay_trade_query_response' => object(stdClass)[9] public 'code' => string '10000' (length=5) public 'msg' => string 'Success' (length=7) public 'buyer_logon_id' => string 'uqq***@sandbox.com' (length=18) public 'buyer_pay_amount' => string '2.00' (length=4) public 'buyer_user_id' => string '2088102175794899' (length=16) public 'buyer_user_type' => string 'PRIVATE' (length=7) public 'fund_bill_list' => array (size=1) 0 => object(stdClass)[10] ... public 'invoice_amount' => string '2.00' (length=4) public 'out_trade_no' => string '20150320010101004498' (length=20) public 'point_amount' => string '0.00' (length=4) public 'receipt_amount' => string '2.00' (length=4) public 'send_pay_date' => string '2019-03-12 10:50:09' (length=19) public 'total_amount' => string '2.00' (length=4) public 'trade_no' => string '2019031222001494890500825908' (length=28) public 'trade_status' => string 'TRADE_SUCCESS' (length=13) public 'sign' => string 'gK9/bRceHVXou0vX4U+GgQv+F9pgL15lDtpQqR/y2glaWywOvYUsmV1/5UpbVFOP28yXHMEQJxqwp8j81GuF8zQsXlYmdlaOgZ0BaTa3Eh82zftejcRkIwgiLhUg3ZQBfbpjExgazhF0CvGoNaauyIfoKICJF61vsvN0ISAOXEPze+/XVmzjTueFUn3OWoknaD/mxtlGYOe67AU5vc+ueOUklaxis0ERivJB+eo9DWUDA3NzRvms2arkaQ3BEdZ4+HMMI4wK94RxpUchMbZHmta+msMTSpDtRRxWgr9gT9XFw59ZgYr/rpZYPCHKAAp/RbhzOLpnpBlAE/eqLSZqeA==' (length=344)
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); AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.BizContent = "{" + "\"out_trade_no\":\"20150320010101004498\"," + "\"trade_no\":\"2019031222001494890500825908\"" + "}"; AlipayTradeQueryResponse response = client.Execute(request); Console.WriteLine(response.Body);
返回的响应参数示例:
{"alipay_trade_query_response":{"code":"10000","msg":"Success","buyer_logon_id":"uqq***@sandbox.com","buyer_pay_amount":"2.00","buyer_user_id":"2088102175794899","buyer_user_type":"PRIVATE","fund_bill_list":[{"amount":"2.00","fund_channel":"ALIPAYACCOUNT"}],"invoice_amount":"2.00","out_trade_no":"20150320010101004498","point_amount":"0.00","receipt_amount":"2.00","send_pay_date":"2019-03-12 10:50:09","total_amount":"2.00","trade_no":"2019031222001494890500825908","trade_status":"TRADE_SUCCESS"},"sign":"gK9/bRceHVXou0vX4U+GgQv+F9pgL15lDtpQqR/y2glaWywOvYUsmV1/5UpbVFOP28yXHMEQJxqwp8j81GuF8zQsXlYmdlaOgZ0BaTa3Eh82zftejcRkIwgiLhUg3ZQBfbpjExgazhF0CvGoNaauyIfoKICJF61vsvN0ISAOXEPze+/XVmzjTueFUn3OWoknaD/mxtlGYOe67AU5vc+ueOUklaxis0ERivJB+eo9DWUDA3NzRvms2arkaQ3BEdZ4+HMMI4wK94RxpUchMbZHmta+msMTSpDtRRxWgr9gT9XFw59ZgYr/rpZYPCHKAAp/RbhzOLpnpBlAE/eqLSZqeA=="}
四、注意事项
1、如何判断交易状态
根据接口返回响应参数trade_status(交易状态)进行判断,详见trade_status参数分析如下:
支付宝交易状态快速掌握:[url]https://openclub.alipay.com/read.php?tid=1295&fid=72[/url]
交易状态问题解析:[url]https://openclub.alipay.com/read.php?tid=5407&fid=72[/url]
2、如何判断用户支付渠道
根据接口返回响应参数fund_bill_list(交易支付使用的资金渠道)的fund_channel(交易使用的资金渠道参数)进行判断:
fund_channel返回对应渠道参考:[url]https://alipay.open.taobao.com/doc2/detail?treeId=26&articleId=103259&docType=1[/url]
五、常见报错
1、Q:报错ACQ.TRADE_NOT_EXIST(交易不存在)?
A:解决方案:[url]https://openclub.alipay.com/read.php?tid=13844&fid=56[/url]
如果还是报错,请按如下方式进行提问:
正确提问方式:使用环境(沙箱还是正式环境)+接口名称(中文+英文)+ 请求参数 + 报错信息详情