用的是MUI的框架
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <meta name="HandheldFriendly" content="true"/> <title>Hello H5+</title> <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/> <script type="text/javascript" src="../js/common.js"></script> <script type="text/javascript"> var pays={}; function plusReady(){ plus.payment.getChannels(function(channels){// 获取支付通道 var content=document.getElementById('payment'); for(var i in channels){ var channel=channels[i]; if(channel.id!='alipay'){ continue;//过滤掉非支付宝的通道 } pays[channel.id]=channel; checkServices(channel); } },function(e){ outLine('获取支付通道失败:'+e.message); }); } document.addEventListener('plusready', plusReady, false); //检测是否安装支付服务Begin function checkServices(pc){ if(!pc.serviceReady){ var txt=null; switch(pc.id){ case 'alipay': txt='检测到系统未安装“支付宝快捷支付服务”,无法完成支付操作,是否立即安装?'; break; default: txt='系统未安装“'+pc.description+'”服务,无法完成支付,是否立即安装?'; break; } plus.nativeUI.confirm(txt, function(e){ if(e.index==0){ pc.installService(); } }, pc.description); } } //检测是否安装支付服务End //开始支付Begin var w=null; // 支付 function pay(id){ if(w){return;}//检查是否请求订单中 if(id==='appleiap'){ outSet('应用内支付'); clicked('payment_iap.html'); return; } outSet('----- 请求支付 -----'); w=plus.nativeUI.showWaiting(); // 请求支付订单 var xhr=new XMLHttpRequest(); xhr.onreadystatechange=function(){ switch(xhr.readyState){ case 4: w.close();w=null; if(xhr.status==200){ outLine('----- 请求订单成功 -----'); outLine(xhr.responseText); var order=xhr.responseText; plus.payment.request(pays[id],order,function(result){ outLine('----- 支付成功 -----'); outLine(JSON.stringify(result)); plus.nativeUI.alert('支付成功:感谢你的支持,我们会继续努力完善产品。',function(){ back(); },'捐赠'); },function(e){ outLine('----- 支付失败 -----'); outLine('['+e.code+']:'+e.message); }); }else{ outLine('----- 请求订单失败 -----'); outLine( xhr.status ); plus.nativeUI.alert('获取订单信息失败!', null, '捐赠'); } break; default:break; } } xhr.open('GET','http://www.baidu.com/alipay/test/AopClientTest.php'); outLine('请求支付订单:'+'http://www.baidu.com/alipay/test/AopClientTest.php'); xhr.send(); } //开始支付End </script> </head> <body> <div id="payment" class="button" onClick="pay('alipay')">支付宝支付</div> <div id="outpos"/> <div id="output" style="height:230px"> Payment模块管理支付功能,可通过js调用第三方支付服务。通过plus.payment可获取支付管理对象。 </div> </body> </html>
下面是后端 AopClientTest.php
<?php require_once '../AopClient.php'; require_once '../AopCertification.php'; require_once '../request/AlipayTradeQueryRequest.php'; require_once '../request/AlipayTradeWapPayRequest.php'; require_once '../request/AlipayTradeAppPayRequest.php'; //1、execute 使用 $aop = new AopClient; $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do"; $aop->appId = "20999999999998"; $aop->rsaPrivateKey = 'MIIEvQIBADja+x70IVqvzR2uB/H+kSNSRbwmfeivebwWPPF7Xa14B0xQPF23Zu1LfNDZIOgoNmarsqXfPuNtxxPgUTXjuigISEdcA3AViNDK3W8Xa/Wv2aLtbRdvDA0Tjl0zWcxT/R5WGWF63q1J5HmXPKsFXAqdo6FcfiBNpg2lXxweOxkGTrLWplBWbtWKQpfvGg7X7aN8h1e8SKWyuDnAZnlv9TNdudzAgMBAAECggEATKpbnUMV/3IE12D7V3xsHuWyqAZoyR0h924PowqQ4WCzmoD3lf4GMunnpDLjvXe2V27B8X+v3ei58eodN/5l1bt3rF23jDHywg6jK7Ga6LUcfm9i6PhGS3BtiYpgzwyM/gvixu+9+NARG5IdO/s5HZiwnLIUwRT8L2DGF268/5SzE7DPD7lxucvCYtmCTxMLmn5rZ0M0K6Iv5/12Zv7qaVVRASn/pbvkdU6rYxJIIgp4M611kGZUDzYo8VX4yvC+k/JrMtplfD3QXsrlMQJojHLtMIFfb9eACjKnHUOtLc7vz1Jf7I0xNoNqm3cuVjBxGYbk4E+SX9iM00SOpImjaQKBgQDnkRtgzU/ExSgrmWTi9UtgTVjNvADzMPFhfCpZt/+xAsKhIuNv+P7mz8oP7IkGWvxhHs0nDmTZsPnaxCnIabr+NzTdOZRv1Bpdi7JJwlRqOVdD+DlMTPtk/F73uJUrmg2/e7HoFcvcwU8r+J01hO0TkNJamf20Wajup8FE0WelVQKBgQDk+Bx4NjeMY6XsLCC8UIz/HtJoEuvepmzmYj+LpCAHs/A6+u9k3/RfjPIrmCLnWNQl1e0ybpxi/QfbaKInyb08XxdfQrh5z524cLZZLV5/cXTG7I9AsVBkaFlNAhqJkW9R8ckNprw9e5iK8Y7vRrlh4QfouFc//xsuakp66OHZpwKBgGO+YCGZLgPevIfTId6Us+v+RQMROQOHkGFiP0e39r4DJ1yLD5ZalUlMNYgYZYlERbN7tfhdvdj63xR5LFo0GKSJmQ61T5e/r7+dfaVBvGSo4ErZr6yK+DoubqWbbz2BtOxG1oHWaaJj7btN8kFsSUYFOmZmMB6wBBEQVzJSVKelAoGBANpyL88sgrCIvVrl+tFO1EXt5PVv3+KKyui9oTybeAeBT1cl4tFNrPqgIMcgnWQ0Zio2v9bWmpMCLsXQPKxTFxyK6GR05kUoo6O8vOu1kitqVewW8tDYmgdBWrlSoXjP7PG0CIniokLSMQWWUjTMN78/DumjPXKL8yXEOIystBSPAoGAWB+KFzMKzE2K4xypZABVa2sJJD13VZ3TymydMnOEvwO81ck443oSiWiuRKgUBcL7Yc+WrF4w66l2uh8OIS/y5zTPvqz/g/fxSWKB7ykjVf6ck6Rx24dWfejIL9mSHoP4eoNzKF6BXNRIoZBPilzNvJg1WU+wp67D9XCWHHssTa8='; $aop->format = "json"; $aop->charset = "UTF-8"; $aop->signType = "RSA2"; $aop->alipayrsaPublicKey = 'MIIBIdzrFl7Y9Z9c8ecUfQCwjWZS1b2H8/Mj4b8/g634UwRiCUsMIUDnY1yzyiTtpFn3aMXpFibANEmWdlHf+nYi2IWj2rCcCWzBXK5QSuYfvOaHgaAk71nMR0i766XoK1POdkXHbPiKecI7/mOE/DxER6J22JHLP9QVa1UoGG8var7Q9abwQXZL8Ge7jwwywhokLEmEwza/aOQIDAQAB'; //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay $request = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数 $arr_bizcontent['body'] = '我是测试数据'; $arr_bizcontent['subject'] = '我是测试数据'; $arr_bizcontent['out_trade_no'] = time().rand(111,999)."ac"; $arr_bizcontent['timeout_express'] ="30m"; $arr_bizcontent['total_amount'] ="0.01"; $arr_bizcontent['product_code'] ="QUICK_MSECURITY_PAY"; $bizcontent = json_encode($arr_bizcontent); $request->setNotifyUrl("商户外网可以访问的异步地址"); $request->setBizContent($bizcontent); //这里和普通的接口调用不同,使用的是sdkExecute $response = $aop->sdkExecute($request); //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题 echo $response;//就是orderString 可以直接给客户端请求,无需再做处理。
// 官方SDK中 echo $response; 这儿是 htmlspecialchars($response); 必须吧转义去掉不然无法使用