说明:
交易创建接口主要是用于创建交易生成trade_no,配合jsapi唤起收银台来唤起支付的。
一、官方文档
官方文档地址:[url]https://docs.open.alipay.com/api_1/alipay.trade.create/[/url]
二、开发前准备工作
1、签约:[url]https://openclub.alipay.com/read.php?tid=276&fid=72[/url]
2、创建应用:[url]https://openclub.alipay.com/read.php?tid=1606&fid=72[/url]
3、生成密钥:[url]https://openclub.alipay.com/read.php?tid=1833&fid=69[/url]
4、下载服务端sdk:[url]https://docs.open.alipay.com/54/106370/[/url]
如何签约以及签约无法成功等相关签约问题:[url]https://openclub.alipay.com/read.php?tid=276&fid=72[/url]
注意事项:1、支持沙箱测试;2、需签约并添加功能;3、支持第三方调用。
检查调用appid的应用功能列表中是否添加“当面付”并显示已签约。
三、接口集成示例代码
1、参数说明
(1)out_trade_no:商户订单号,由商户自定义设置;
(2)subject:商品标题;
(3)buyer_id:买家的支付宝唯一用户号(2088开头的16位纯数字);
(4)total_amount:交易订单金额,精确到小数点后2位,最小设置为0.01;
2、JAVA版请求示例
沙箱账号
//沙箱APPID public static final String app_id = "您的appid"; //沙箱私钥pkcs8格式 public static final String merchant_private_key = "您的应用私钥"; //支付宝公钥 public static final String alipay_public_key = "您的支付宝公钥"; //沙箱网关地址 public static final String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
请求示例
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();//model格式传值AlipayTradeCreateModel model =new AlipayTradeCreateModel(); model.setOutTradeNo("20150320010101002"); model.setTotalAmount("0.01"); model.setBuyerId("20888028623****");//传入付款方的pid model.setSubject("Iphone6 16G"); model.setBody("Iphone6 16G");request.setBizModel(model);/** json格式传值request.setBizContent("{" + "\"out_trade_no\":\"20150320010101001\"," + "\"total_amount\":0.01," + "\"subject\":\"Iphone6 16G\"," + "\"body\":\"Iphone6 16G\"," + "\"buyer_id\":\"20888028623****\"" +//传入付款方的pid " }");**/AlipayTradeCreateResponse response = alipayClient.execute(request);//使用第三方应用授权app_auth_token传值方式//AlipayTradeCreateResponse response = alipayClient.execute(request,null,"app_auth_token");if(response.isSuccess()){ System.out.println("调用成功"); System.out.println(response.getBody());} else { System.out.println("调用失败"); System.out.println(response.getBody());}
返回结果
{"alipay_trade_create_response":{"code":"10000","msg":"Success","out_trade_no":"20150320010101001","trade_no":"2018121722001473950576863846"},"sign":"OqTZURLG3Oa6w1oD2Rg8NLvRzLQsa5GSxBmwvQlrBed+xCh4IfRgWhYQJuk8ru/uoE00DgkFO+zWWYwuW6a50s4+LnSbOo0N9aEE1Ft7ncjXGNalFdhDLumuTvOzgvpS59QsdSx5FsCplMCfG7+rm5ZTN189dICQKzuFridKeMXdrzKEnuJBEdld29+6ptW0Q00Ua1kHWkCHLKKIEMfbob6kKrV3iYsTtCCuAb8lgrpuI+7WyAQQpyakbkoPyxIqUNdaeiijfEHNjMwCh0Nx3qoziyrebK2fiQjWNWXRqW/AoIgPBMdsTr7jCpIMAulANw8z587woA4ELJIfcM879Q=="}
3、PHP版请求示例
gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';$aop->appId = '您的appid';$aop->rsaPrivateKey = '您的应用私钥';$aop->apiVersion = '1.0';$aop->postCharset='utf-8';$aop->format='json';$aop->signType = 'RSA2';//生成随机订单号$date=date("YmdHis");$arr=range(1000,9999);shuffle($arr);$request = new AlipayTradeCreateRequest ();$request->setBizContent("{" . "\"out_trade_no\":\"".$date.$arr[0]."\"," . "\"total_amount\":\"0.01\"," . "\"subject\":\"tets\"," . "\"buyer_id\":\"2088102175822813\"," . "\"timeout_express\":\"2m\"" . "}");$result = $aop->execute($request);//第三方请求,传入的是谁的授权token,资金就收款到谁的账户,不传token则收款到app_id对应的账户//$result = $aop->execute($request,"","auth_token");var_dump($result);?>
返回结果
object(stdClass)[9] public 'alipay_trade_create_response' => object(stdClass)[10] public 'code' => string '10000' (length=5) public 'msg' => string 'Success' (length=7) public 'out_trade_no' => string '201901030700291291' (length=18) public 'trade_no' => string '2019010322001422810200828570' (length=28) public 'sign' => string 'Tkud9S3K+866Ldp/aB/11fj9mcd3s9KPjvwooX4Sy1JFJS+9IA7c+X7JldNXRBfhePiJNgT5fiGg/ZYk7g1qzr1zVaCCk+DyoTcNJiy8eLD+FhoqspR4ClXyEjscupLhSEOVvvTeDlBqAb409JWyux9ZMFJoI7InxFS2o7kQiI9MdcaDBM7BYNqUVU2GlZ0EOCqq581yN4I+nynXfIxNIUDCIveSYipqWxUaqKUENCfHOAx4mH+VLEA4337n3oXIu9D1MvGqge1Rr+maUyzcZKQMQeaQ06nYxCW3TmxlTzUYrWoEOLllVS3PXTwfqQj09rCD0lHe5AIrt67BcOGtEQ==' (length=344)
4、.NET版请求示例
IAopClient client = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", Com.Alipay.Config.appId, Com.Alipay.Config.merchant_private_key, "json", "1.0", "RSA2", Com.Alipay.Config.alipay_public_key, "GBK", false); AlipayTradeCreateRequest alipayRequest = new AlipayTradeCreateRequest();//创建API对应的request AlipayTradeCreateModel model = new AlipayTradeCreateModel(); model.OutTradeNo = "2018092815181234561223434123"; model.Subject = "交易创建接口测试"; model.TotalAmount = "0.01"; //用户pid(付款方pid)可使用用户信息授权方式获取 model.BuyerId = "2088102175822813"; alipayRequest.SetBizModel(model); //第三方调用方式 //AlipayTradeCreateResponse response = client.Execute(alipayRequest, "", "app_auth_token"); //自调用方式 AlipayTradeCreateResponse response = client.Execute(alipayRequest); Console.WriteLine("交易创建接口返回信息:" + response.Body);
返回结果
交易创建接口返回信息:{"alipay_trade_create_response":{"code":"10000","msg":"Success","out_trade_no":"2018092815181234561223434123","trade_no":"2019010322001422810200828571"},"sign":"eqiXYmUwA7thyXUpF46RQSZy95oaBui78K4DPajWw4BrXUTDzeKWyXBBmQk0PMHLQDNXbk1oO71TsiZzMqFwQ1WJqZwdEuy3nz81bjiFauK6NwdZs4odE/w+LiY8MTBCt7sjWSHI4nj4wtagsmLzixe16TUQP0CDhw29X2/6CQMjz8+YhaV8BAciAt1D2bl7Zk12dYCYilOZugpcWJxZ58pZ3OZ+vYAk1l9DtUqeum+EldimP4ebQIMYnBs0bcqjHrePXd4sxzJW0alSunYrydn2ZgbhWLsuqWhsoGtaac1svDS4uhehgKtAMP2Y0Z78ZSBV2npM4c74RQw4OPtmGQ=="}
四、注意事项
1、获取买家的buyer_id
建议使用用户信息授权接口获取pid,详细的获取方案:[url]https://openclub.alipay.com/read.php?tid=1813&fid=68[/url]
2、基础常识
1).交易创建接口支持第三方代商户发起请求,详细的如何传入app_auth_token的代码在示例里面已经说明
2).接口调用成功返回10000后,这边调用查询接口是可以查询到订单信息的,但是是等待支付状态的订单
3、trade_no请求方式
商家获取到返回的trade_no参数,需要使用jsapi唤起支付,具体详细的建议参考文档
创建订单+JSAPI唤起收银台支付:[url]https://docs.open.alipay.com/common/105591[/url]
4、辅助接口
alipay.trade.query(统一收单线下交易查询)集成流程:[url]https://openclub.alipay.com/read.php?tid=13842&fid=56[/url]
alipay.trade.refund(统一收单交易退款接口) 集成流程:[url]https://openclub.alipay.com/read.php?tid=13853&fid=56[/url]
alipay.trade.fastpay.refund.query(统一收单交易退款查询) :[url]https://openclub.alipay.com/read.php?tid=13962&fid=56[/url]
alipay.trade.close(统收单交易关闭接口)集成流程:[url]https://openclub.alipay.com/read.php?tid=13963&fid=56[/url]
alipay.data.dataservice.bill.downloadurl.query(查询对账单下载地址):[url]https://openclub.alipay.com/read.php?tid=14016&fid=56[/url]
五、Java版本demo
json传值demo:download:交易创建接口json传值Javademo.zip
model传值demo:download:交易创建接口model传值Java demo.zip