鉴于很多对接app支付的童鞋经常出现app支付系统繁忙,ali40247,订单信息异常在等等的错误,其实那些错误都是大同小异的,无非就是没权限,密钥不对,参数传值错误,请求信息拼接有问题等等,所以这边写app支付开发指南分享帖,希望能够帮到大家!!!
一、文档地址
app支付官方文档地址:[url]https://docs.open.alipay.com/204[/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]
5、下载客户端demo:[url]https://docs.open.alipay.com/54/104509/[/url]
注意事项:1、支持沙箱测试;2、需签约并需要添加功能;3、不支持第三方调用。
检查调用appid的应用功能列表中是否添加“app支付”。
注意:还有一个比较坑的点就是如果你添加app支付功能的时间早于你签约的时间,也是需要先将功能包删除后再重新添加才能正常使用的,否则也会报错
三、接口集成代码示例
1、参数说明
(1)out_trade_no:商户订单号,由商户自定义设置;
(2)subject:商品标题;
(3)product_code:固定产品码值:QUICK_MSECURITY_PAY;
(4)total_amount:交易订单金额,精确到小数点后2位,最小设置为0.01;
2、JAVA版请求示例
public static void main(String[] args) throws AlipayApiException, UnsupportedEncodingException { //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key,AlipayConfig.sign_type); AlipayTradeAppPayRequest alipayRequest=new AlipayTradeAppPayRequest(); //设置同步回调地址 alipayRequest.setReturnUrl(AlipayConfig.return_url); //设置异步回调地址 alipayRequest.setNotifyUrl(AlipayConfig.notify_url); //公共回传参数,用于传值自定义信息,必须encode,不能带单双引号之类的之定义参数,否则会报错 String passback_params = "公用回传参数测试123#34!"; String passback_params2 =URLEncoder.encode(passback_params,"UTF-8"); // alipayRequest.setBizContent("{\"out_trade_no\":\"201809251015343222843\"," // + "\"total_amount\":\"0.01\"," // + "\"subject\":\"app支付测试\"," // + "\"passback_params\":\""+ passback_params2 +"\"," // + "\"product_code\":\"QUICK_MSECURITY_PAY\"}"); AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.setOutTradeNo("20181012015343222843"); model.setTotalAmount("0.01"); model.setSubject("app支付subject测试"); model.setPassbackParams(passback_params2); model.setProductCode("QUICK_MSECURITY_PAY"); alipayRequest.setBizModel(model); AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest); if(response.isSuccess()){ System.out.println("调用成功"); } else { System.out.println("调用失败"); } System.out.println(response.getBody()); }
返回信息
alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2016101800718925&biz_content=%7B%22out_trade_no%22%3A%22201809251015343222843%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%22app%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95%22%2C%22passback_params%22%3A%22%25E5%2585%25AC%25E7%2594%25A8%25E5%259B%259E%25E4%25BC%25A0%25E5%258F%2582%25E6%2595%25B0%25E6%25B5%258B%25E8%25AF%259512334%25EF%25BC%2581%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=utf-8&format=JSON&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2F106.14.187.178%2Fopendevtools%2Fnotify%2Fdo%2Fa9bed896-0fc0-4b05-ba55-6a2550cacd36&return_url=https%3A%2F%2Fwww.baidu.com%2F&sign=qiOEfMcQoObzuPdZNkMOzavHfJLskTUWJxb08YObj8D0SexDStOw%2BHEwOs7x1hGih8Zs3rsT%2BA3aYVnmwp0FTnTYHx2cTbvz1tkNTzoguOG%2BdNR4b5dsJ%2BvUU4UbHV2KDOxg%2FASUcjcbhqitYraWjBgL02QWgTa%2FpA7dpZnxaOKyksZ1tvp7dR3zYvfDdVnfo3vpXlJxc8QTXsYvZdpODvmKG9odw%2FTsP2fZdt3Up7aiq7Ae8rby%2FNg%2BIMcGJjH%2F5MnUC99%2FU9%2Bjwt%2Biqt7jwU4PVfGimDY6ifIYs3PosGwfwrMTSZkI8AzdcsmwHRcqGJJNzlzegl9jQHw9mBzSAw%3D%3D&sign_type=RSA2×tamp=2018-09-25+13%3A09%3A52&version=1.0
注意:php跟.net 要使用pkcs1的私钥
3、PHP版请求示例
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 AlipayTradeAppPayRequest();//异步地址传值方式$request->setNotifyUrl("https://www.alipay.com");$request->setBizContent("{\"out_trade_no\":\"".$date.$arr[0]."\",\"total_amount\":0.01,\"product_code\":\"QUICK_MSECURITY_PAY\",\"subject\":\"app测试\"}");$result = $aop->sdkExecute($request);print_r(htmlspecialchars($result));?>
返回结果
alipay_sdk=alipay-sdk-php-20161101&app_id=2017121300675523&biz_content=%7B%22out_trade_no%22%3A%22201809250359474566%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22app%E6%B5%8B%E8%AF%95%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay¬ify_url=https%3A%2F%2Fwww.alipay.com&sign_type=RSA2×tamp=2018-09-25+03%3A59%3A47&version=1.0&sign=ueXycfafCqB2Uc31pyAjiRF4rDW5nn8MCT8Hy0lB8jukbgLyhn76hVKuw7mOgqGQwxVdYWq7Rm2A%2FpXBPwjWBcifuowtoiyB2hhw2FHR6yje8xKNNe3jGfVPrMxyze0HmOD3BQwzz53U4VcpQACRHbAZU8ZOT3U8MzJnJqMCsdD8IGZU1b8meig1gn6DANvMfmKuubUNod5DFMJlEpHZENMzSw5F546TW%2Bz6VhyxC6Hzp0%2FFd%2BF8zHK9THbyK6sR4EJQfvm7hmdOR%2FRKaS3RxHbBZ%2BZha%2FFjwA0hFYXTI6D7xB5cGiVtQCfrDmPZaQvwY7jdNDuav1dHj1is5nsjWw%3D%3D
4、.NET版请求示例
static void Main(string[] args) { 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); AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();//创建API对应的request AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); model.OutTradeNo = "201809251315181234561223434123"; model.Subject = "手机网站支付测试"; model.TotalAmount = "0.01"; model.ProductCode = "QUICK_MSECURITY_PAY"; alipayRequest.SetBizModel(model); AlipayTradeAppPayResponse response = client.SdkExecute(alipayRequest); Console.WriteLine("接口调用返回信息:" + response.Body); }
.net返回结果
接口调用返回信息:app_id=2016101800718925&biz_content=%7b%22out_trade_no%22%3a%22201809251315181234561223434123%22%2c%22product_code%22%3a%22QUICK_MSECURITY_PAY%22%2c%22subject%22%3a%22%ca%d6%bb%fa%cd%f8%d5%be%d6%a7%b8%b6%b2%e2%ca%d4%22%2c%22total_amount%22%3a%220.01%22%7d&charset=GBK&format=json&method=alipay.trade.app.pay&sign_type=RSA2×tamp=2018-09-25+12%3a06%3a52&version=1.0&sign=HIatgoFqis%2bgwCPw%2bnDGQhbBR3i7X4P%2fnagTXO6DkLVLi7kPjx233skwjtpwnFzmSHIp84%2foaBpUHkPXMAkX%2b7rBJdYxW5FLqNXz9DYZh5NBtzmoNfpD%2baPUaIO%2fKylESh3r0Twgz4BQcmdJ6HkS4Erq5blTFHNK8OUB9ISI9absu30fS5teYBkkBkM2QDaMIpIxAzjvIBPZzUZU7%2fgXIq%2f3A9B5f7E70QH1hycktjjk8RgC%2fsTjMWx%2f8q2WN5T0fj9053WM%2b34CFhzXK9kNkI2Hf2Sd6e%2fBlToS%2bYt1%2bByvtiYifrWIxc1ldBbWtH5NqcXpgYzwk1JHjieSeTqn%2fw%3d%3d
如果不是使用sdk签名方式,使用的是自己的签名方法,参数排序请严格按照我们文档的排查方式,不要随意排序拼接,详情请参考文档:[url]https://docs.open.alipay.com/204/105465/[/url]
四、客户端集成
android版客户端集成方案:[url]https://docs.open.alipay.com/204/105296[/url]
IOS版客户端集成方案:[url]https://docs.open.alipay.com/204/105295[/url]
生成请求示例之后,我们再将示例中的信息传值到客户端,无需做任何处理,直接传值
//将服务端生成的请求信息不做任何处理直接传入客户端,调用收银台final String orderInfo ="alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2014100900013222&biz_content=%7B%22out_trade_no%22%3A%2220180823101534222843%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%22app%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95%22%2C%22passback_params%22%3A%22%25E5%2585%25AC%25E7%2594%25A8%25E5%259B%259E%25E4%25BC%25A0%25E5%258F%2582%25E6%2595%25B0%25E6%25B5%258B%25E8%25AF%2595123%252334%25EF%25BC%2581%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=utf-8&format=JSON&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2F106.14.187.178%2Fopendevtools%2Fnotify%2Fdo%2Fa9bed896-0fc0-4b05-ba55-6a2550cacd36&return_url=https%3A%2F%2Fwww.baidu.com%2F&sign=RyGvj%2FyEFCBDNbFjMKJGToD13kSO12LzEqd%2FXTKiMADlILQwvWM7ec4gIT6tf6EcnTry%2Fe325EHfFIjUjQSqpOmqftssyZF0jQpMX8Nd7Km%2F%2FE4H9BEP6QgdPE7lgPItE5mwFYmkUVKJmO6Bf1Zodn3sQcEQp%2FanTmK0tivX9ZpFup%2BLJKexIOgrGb3baAfWJdp1KX4CTPB3JlUHG2FuAFdblsjjZvzI8mv6A6PBjt72C%2B2vTAAMFhakAvY7j6ql23dVMxlKCA19ar11cBrEY6hH5uxhVhAb%2BcCzBm07RqiqXwMFBXVnUpTcE%2FIcG1Gx4zPE9WW3BxmCCI2RavZ0yQ%3D%3D&sign_type=RSA2×tamp=2018-09-25+11%3A48%3A38&version=1.0"; Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask(PayDemoActivity.this); Map
result = alipay.payV2(orderInfo, true); Log.e("msp", result.toString()); Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; Thread payThread = new Thread(payRunnable); payThread.start();
五、注意事项
1、app支付集成流程
app支付需服务端与客户端集成调用,服务端生成请求参数字符串,将该请求参数字符串交由客户端发起请求进行支付。
注:响应参数是在客户端发起请求调用后返回,不是在服务端调用接口直接返回。
2、app支付成功后回调商户页面
android版app支付支持支付后自动回调;
IOS版app支付无法自动回调,需点击完成按钮,手动切换。
注:IOS版需配置scheme,使调用接口返回信息返回商户app。
3、沙箱客户端集成方式
沙箱账号查看地址:[url]https://openhome.alipay.com/platform/appDaily.htm?tab=info[/url]
沙箱环境测试只支持安卓版,使用官方sdk,需要在支付接口前调用如下方法;
详情请参考文档:[url]https://docs.open.alipay.com/204/106450/[/url]
EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
4、客户端调试工具
测试服务端生成的请求参数是否正常,可使用客户端调试工具。
客户端调试工具:[url]https://openclub.alipay.com/read.php?tid=7695&fid=60[/url]
5、辅助接口
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]
六、显示效果
登录沙箱账号点击【下一步】,进入账单详情,点击【确认付款】,进入输入密码页面
注意:未输入密码之前,支付宝订单还是为创建状态,只有输入密码之后,才会生成支付宝订单,所以这个时候用查询接口查询会报错“订单不存在”
如有疑问,欢迎大家留言提问或者吐槽,谢谢大家!!!!