本帖仅供参考,非官方demo
一、文档地址
文档地址:点击【查看】
sdk下载:点击【查看】
官方demo下载地址:点击【查看】
二、开发前准备工作
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)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";//正式环境网关地址 //public static final String gatewayUrl = "https://openapi.alipay.com/gateway.do";
(适用于jdk1.5以上)
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); request.setNotifyUrl("https://docs.open.alipay.com/200"); AlipayTradePrecreateModel model = new AlipayTradePrecreateModel(); //out_trade_no(订单号)必须是唯一的,测试一次必须修改新的订单号 model.setOutTradeNo("201811287635213732424"); model.setTotalAmount("0.1"); model.setSubject("扫码支付测试"); model.setTimeoutExpress("1m"); model.setBody("body"); //二维码失效时间,最小2m中,1m会直接失效 model.setQrCodeTimeoutExpress("3m"); request.setBizModel(model); AlipayTradePrecreateResponse response = alipayClient.execute(request); //第三方请求,传入的是谁的授权token,资金就收款到谁的账户,不传token则收款到app_id对应的账户 // AlipayTradePrecreateResponse response = alipayClient.execute(request,"","app_auth_token"); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } System.out.println("二维码码串:"+response.getQrCode()); System.out.println("接口调用返回结果:"+response.getBody());
返回结果
二维码码串:[url]https://qr.alipay.com/bax0366066dftdpgmyn40063[/url]接口调用返回结果:{"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"201811287635213732424","qr_code":"https:\/\/qr.alipay.com\/bax0366066dftdpgmyn40063"},"sign":"By6cfRS9uONd4BgLDa60oW3DXVLhHgfOldkBH7ctx1sYh1qDdCDmf+rP9mbKXZA5IoQ47R8QNQ4fATrR0QMs/zAg6qgB9nP1Oj9zg4DVNOPXMUi1bGx2texH3W5cfGXpQ8ffVqKcnVk7qzufTBgjjZJfQ8jV1i8ZeTnd4+L662pk45EHTOBcElc04JXOwlTLurYiVw/9htN9m1KQE0pjKvstBF5fr57fP7e0RJBUAGw1oezyA+GQRjTrgpXFxu0wqzZxJ0ylN4xdH/ZBTd6IW7ClE5+VuNrlM/57T+UDqkDXzndsM4bRnwALN/16E4wp9MT/AJkRT0CqSYpkRWy46g=="}
3、PHP版请求参数
(适用于php 5.5以上环境)
appId = '填写您的appid';$aop->rsaPrivateKey = '填写您的私钥';$aop->alipayrsaPublicKey='填写您的支付宝公钥';$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';$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 AlipayTradePrecreateRequest();$request->setBizContent("{" . "\"out_trade_no\":\"".$date.$arr[0]."\"," . "\"total_amount\":\"0.01\"," . "\"subject\":\"tets\"," . "\"timeout_express\":\"2m\"" . "}");$result = $aop->execute($request);//第三方请求,传入的是谁的授权token,资金就收款到谁的账户,不传token则收款到app_id对应的账户//$result = $aop->execute($request,"","app_auth_token");var_dump($result);?>
返回结果:
public 'code' => string '10000' (length=5) public 'msg' => string 'Success' (length=7) public 'out_trade_no' => string '201811280635179895' (length=18) public 'qr_code' => string 'https://qr.alipay.com/bax06738zqab6gooosbs00ed' (length=46) public 'sign' => string 'ZH7BJQEM8Y+Rjfqm/FOztvwJ9EWa533AZkC+Xs0WlcYRvCmYyxHPYYPa1BuMPTC7uvQjCL1nm7BMwrofB8wZ604cWXwSgL/JIG7Qwx/69S5rfDuiFMepS6Y/G6RP6uijDQCblQW0h0EXWRQn9gQxDpjjpUheKx+WQH5cmpbatreCfOyeeEs4DI8K+1BpBfacV01e/ZorTuYaJYgIn2SueoPHIub5qN5TEvvpbDxF365ZwIJE7rgOivTHPXHm6JHkXdYV3oRehyJrcDzCg6GtGeKTWwm07bYhoLUNp2FH+36E0KpCClYtmo2fMe2eSAGtLRzw6Vz44hdD5P+8wBZjHg=='
4、.NET版请求参数
公共参数配置
public class Config { //应用appId沙箱 public static string appId = "app_id"; //支付宝网关 public static string serverUrl = "https://openapi.alipaydev.com/gateway.do"; //沙箱支付宝公钥 public static string alipay_public_key = @"支付宝公钥"; //开发者原始私钥,千万不要用pkcs8格式的 沙箱 public static string merchant_private_key = @"私钥"; public static string format = "json"; //编码,无需修改 public static string charset = "utf-8"; //签名类型,支持RSA2(推荐!)、RSA public static string sign_type = "RSA2"; //版本号,无需修改 public static string version = "1.0"; /// /// 私钥文件类型转换成纯文本类型 /// ///
过滤后的字符串类型私钥
public static string getMerchantPriveteKeyStr() { StreamReader sr = new StreamReader(merchant_private_key); string pubkey = sr.ReadToEnd(); sr.Close(); if (pubkey != null) { pubkey = pubkey.Replace("-----BEGIN PUBLIC KEY-----", ""); pubkey = pubkey.Replace("-----END PUBLIC KEY-----", ""); pubkey = pubkey.Replace("\r", ""); pubkey = pubkey.Replace("\n", ""); } return pubkey; } }
请求示例
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); AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();//创建API对应的request AlipayTradePrecreateModel model = new AlipayTradePrecreateModel(); model.OutTradeNo = "2018092815181234561223434123"; model.Subject = "扫码支付测试"; model.TotalAmount = "0.01"; model.QrCodeTimeoutExpress = "5"; alipayRequest.SetBizModel(model); //第三方调用方式 //AlipayTradePrecreateResponse response = client.Execute(alipayRequest, "", "app_auth_token"); //自调用方式 AlipayTradePrecreateResponse response = client.Execute(alipayRequest); Console.WriteLine("扫码支付返回信息:" + response.Body);
返回结果
扫码支付返回信息:{"alipay_trade_precreate_response":{"code":"10000","msg":"Success","out_trade_no":"2018092815181234561223434123","qr_code":"https:\/\/qr.alipay.com\/bax06292n6mzvqlvcefc00dd"},"sign":"eHPVyjPjaH/dXdnftit/WerHSihQvmFeA7Nu7PxSVDHIBDLE+RTaBWBPLvVqQPY1VmCCxFr+2I0aG93OYzyCwHbN3oj5+VSLiJZhzmGJnxm33MD8Iokxn3L3Q3f99O9ek3f58ui+92Mydy/jc/5ar9Ayh0moaW3GnskqPUTSAjryirUAAER/2Y6DU7srXe2O9TYofPAqQRBMMjCSSmGZ+cZCKtzeL2yXU81X9pQUlB2wGpWyGTLTCrBZImNauSu4ihop7tMiSDN6j5xhC+twnH3d5JwDB+8D7vBSEfBXMJ1Gj3JO0md7U3dnVu4w73lzvY4aoeP+PfuscKSiFxGtdw=="}
四、注意事项
1、基础知识
1.将返回的二维码串(Qr_Code)传入前台生成二维码,用户使用手机支付宝扫码进行支付;我这边就偷懒使用工具生成了,各位别学我哈
2."code":"10000" 不代表支付成功,只是接口调用成功,请勿混为一谈,判断支付是否成功,请通过异步通知(notify_url)的返回值进行判断
3.qr_code_timeout_express:允许用户最晚付款时间,从接口调用成功开始计时,最小值2m;传值1m扫码支付会直接报二维码已失效,二维码有效时间是2小时,所以该参数设置值超过2h也是无效值;
4.timeout_express:最小值为1m,最大值为15t,不传默认15t;
5.app_auth_token:
使用场景:用于不会开发的商家找服务商代发起请求用,避免商户获取密钥,
如何获取:服务商使用第三方应用授权获取商家的app_auth_token,再将app_auth_token传入接口中进行请求,不传值app_auth_token,资金默认收入对应的appid,传值app_auth_token资金将收入到app_auth_token对应的授权商家账号中;
如何进行第三方应用授权请参考该帖:[url]https://openclub.alipay.com/read.php?tid=1649&fid=68[/url]
2、辅助接口
alipay.trade.cancel(统一收单交易撤销接口)集成流程:[url]https://openclub.alipay.com/read.php?tid=13966&fid=56[/url]
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]
五、常见问题
无权限的小伙伴:[url]https://openclub.alipay.com/read.php?tid=1672&fid=72[/url]
验签出错的小伙伴:[url]https://openclub.alipay.com/read.php?tid=2432&fid=72[/url]
验签失败的小伙伴:[url]https://openclub.alipay.com/read.php?tid=1598&fid=69[/url]
如果还是报错,请按如下方式进行提问:
正确提问方式:使用环境(沙箱还是正式环境)+接口名称(中文+英文)+ 请求参数 + 报错信息详情