payment.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <!--<meta name="MobileOptimized" content="320"/>-->
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var pays={};
  12. function plusReady(){
  13. // 获取支付通道
  14. plus.payment.getChannels(function(channels){
  15. var content=document.getElementById('payment');
  16. var info=document.getElementById('info');
  17. var txt='支付通道信息:';
  18. for(var i in channels){
  19. var channel=channels[i];
  20. if(channel.id=='qhpay'||channel.id=='qihoo'){ // 过滤掉不支持的支付通道:暂不支持360相关支付
  21. continue;
  22. }
  23. pays[channel.id]=channel;
  24. txt += 'id:'+channel.id+', ';
  25. txt += 'description:'+channel.description+', ';
  26. txt += 'serviceReady:'+channel.serviceReady+'; ';
  27. var de=document.createElement('div');
  28. de.setAttribute('class', 'button');
  29. de.setAttribute('onclick', 'pay(this.id)');
  30. de.id=channel.id;
  31. de.innerText=channel.description+'支付';
  32. content.appendChild(de);
  33. checkServices(channel);
  34. }
  35. info.innerText=txt;
  36. },function(e){
  37. outLine('获取支付通道失败:'+e.message);
  38. });
  39. }
  40. document.addEventListener('plusready', plusReady, false);
  41. // 检测是否安装支付服务
  42. function checkServices(pc){
  43. if(!pc.serviceReady){
  44. var txt=null;
  45. switch(pc.id){
  46. case 'alipay':
  47. txt='检测到系统未安装“支付宝快捷支付服务”,无法完成支付操作,是否立即安装?';
  48. break;
  49. default:
  50. txt='系统未安装“'+pc.description+'”服务,无法完成支付,是否立即安装?';
  51. break;
  52. }
  53. plus.nativeUI.confirm(txt, function(e){
  54. if(e.index==0){
  55. pc.installService();
  56. }
  57. }, pc.description);
  58. }
  59. }
  60. var w=null;
  61. var PAYSERVER='http://demo.dcloud.net.cn/payment/?payid=';
  62. // 支付
  63. function pay(id){
  64. if(w){return;}//检查是否请求订单中
  65. if(id==='appleiap'){
  66. outSet('应用内支付');
  67. clicked('payment_iap.html');
  68. return;
  69. }
  70. outSet('----- 请求支付 -----');
  71. var url=PAYSERVER;
  72. if(id=='alipay'||id=='wxpay'){
  73. url+=id;
  74. }else{
  75. plus.nativeUI.alert('当前环境不支持此支付通道!', null, '捐赠');
  76. return;
  77. }
  78. var appid=plus.runtime.appid;
  79. if(navigator.userAgent.indexOf('StreamApp')>=0){
  80. appid='Stream';
  81. }
  82. url+='&appid='+appid+'&total=';
  83. w=plus.nativeUI.showWaiting();
  84. // 请求支付订单
  85. var amount = document.getElementById('total').value;
  86. var xhr=new plus.net.XMLHttpRequest();
  87. xhr.onreadystatechange=function(){
  88. switch(xhr.readyState){
  89. case 4:
  90. w.close();w=null;
  91. if(xhr.status==200){
  92. outLine('----- 请求订单成功 -----');
  93. outLine(xhr.responseText);
  94. var order=xhr.responseText;
  95. plus.payment.request(pays[id],order,function(result){
  96. outLine('----- 支付成功 -----');
  97. outLine(JSON.stringify(result));
  98. plus.nativeUI.alert('支付成功:感谢你的支持,我们会继续努力完善产品。',function(){
  99. back();
  100. },'捐赠');
  101. },function(e){
  102. outLine('----- 支付失败 -----');
  103. outLine('['+e.code+']:'+e.message);
  104. plus.nativeUI.alert('更多错误信息请参考支付(Payment)规范文档:http://www.html5plus.org/#specification#/specification/Payment.html', null, '支付失败:'+e.code);
  105. });
  106. }else{
  107. outLine('----- 请求订单失败 -----');
  108. outLine( xhr.status );
  109. plus.nativeUI.alert('获取订单信息失败!', null, '捐赠');
  110. }
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. xhr.open('GET',url+amount);
  117. outLine('请求支付订单:'+url+amount);
  118. xhr.send();
  119. }
  120. </script>
  121. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  122. <style type="text/css">
  123. #total{
  124. -webkit-user-select:text;
  125. text-align:right;
  126. padding:0 1em;
  127. border: 0px;
  128. border-bottom:1px solid #ECB100;
  129. border-radius: 0;
  130. font-size:16px;
  131. width:30%;
  132. outline:none;
  133. }
  134. </style>
  135. </head>
  136. <body>
  137. <br/>
  138. <p id="info" style="padding: 0 1em;text-align:left;">支付通道信息:</p>
  139. <div style="padding: 0.5em 1em;"><hr color="#EEE"/></div>
  140. <p style="padding: 0 1em;text-align:left;">为DCloud提供的免费软件进行赞助吧。</p>
  141. <br/>
  142. <div style="padding: 0 1em;text-align:left">
  143. 捐赠金额:<input id="total" type="number" value="1"/> 元
  144. </div>
  145. <br/>
  146. <div id="payment"></div>
  147. <!--<div class="button" onclick="clicked('payment_iap.html')">Apple IAP</div>-->
  148. <div id="outpos"/>
  149. <div id="output">
  150. Payment模块管理支付功能,可通过js调用第三方支付服务。通过plus.payment可获取支付管理对象。
  151. </div>
  152. </body>
  153. </html>