MUI APP微信支付前端程式碼示例
阿新 • • 發佈:2022-03-25
MUI APP微信支付前端程式碼示例
開發工具:HbuilderX
manifest.json檔案配置:勾選OAuth登入許可權,填寫appid和appsecret的值;在Payment支付中勾選微信支付,填寫appic值
釋出頁面中:填寫包名稱,私鑰密碼(微信支付建議用私鑰)
mui 支付頁面:zf.html程式碼如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>支付</title> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <!--標準mui.css--> <link rel="stylesheet" href="css/mui.min.css"> <link rel="stylesheet" type="text/css" href="css/swiper.css" /> <link rel="stylesheet"type="text/css" href="css/reset.css"> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/swiper.min.js"></script> <link rel="stylesheet" type="text/css"href="css/icons-extra.css" /> <link rel="stylesheet" type="text/css" href="css/iconfont.css" /> <!-- <link rel="stylesheet" type="text/css" href="css/app.css" /> --> <script src="js/mui.min.js"></script> <script type="text/javascript" src="js/config.js"></script> <script> $(document).ready(function(){ Date.prototype.Format = function (fmt) { // author: meizz var o = { "M+": this.getMonth() + 1, // 月份 "d+": this.getDate(), // 日 "h+": this.getHours(), // 小時 "m+": this.getMinutes(), // 分 "s+": this.getSeconds(), // 秒 "q+": Math.floor((this.getMonth() + 3) / 3), // 季度 "S": this.getMilliseconds() // 毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } var time2 = new Date().Format("yyyyMMddhhmmss"); var x = 100,y = 999; var rdomstr=Math.round(Math.random()*(y-x)+x); var ddnomstr="cz"+time2+rdomstr; document.getElementById('Ddnom').innerText=ddnomstr; //上述程式碼獲生成訂單號,充值訂單號組成:cz+時間+3位隨機碼 }); </script> <style type="text/css"> .top { margin-top: 40px; } .weixin { width: 200px; height: 50px; margin-left: 50px; background: url(../images/icon-weixin.png); } .zhifubao { width: 200px; height: 50px; margin-left: 50px; background: url(../images/alipay.jpg); } #jine{ -webkit-user-select:text; text-align:right; padding:0 1em; border: 0px; border-bottom:1px solid #ECB100; border-radius: 0; font-size:16px; width:30%; outline:none; text-align:center; } </style> </head> <body> <hrader class="mui-bar mui-bar-nav"> <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a> <h1 class="mui-title">第三方支付</h1> </hrader> <div class="mui-content"> <div class="wyPay-df-title"> <ul> <li> 支付訂單號: <span id="Ddnom"></span> </li> </ul> </div> <div> 捐贈金額:<input id="jine" type="text" style="width: 200px; color: #0033FF;" value="12500" /> 元 </div> <div class="top" id="testLogin" > <input type="button" class="weixin" id="weixin1" value="微信支付" /> <input type="button" class="zhifubao" id="zhifubao" value="支付寶支付" /> </div> </div> <script> var wxChannel = null; // 微信支付 var aliChannel = null; // 支付寶支付 var channel = null; //支付通道 mui.init({ swipeBack:true //啟用右滑關閉功能 }); mui.plusReady(function() { // 獲取支付通道 plus.payment.getChannels(function(channels){ for (var i in channels) { if (channels[i].id == "wxpay") { wxChannel=channels[i]; }else{ aliChannel=channels[i]; } } },function(e){ alert("獲取支付通道失敗:"+e.message); }); }) document.getElementById('weixin1').addEventListener('tap',function() { // console.log("微信"); pay('wxpay'); }) document.getElementById('zhifubao').addEventListener('tap',function() { // console.log("zhifubao"); pay('alipay'); }) var ALIPAYSERVER='http://127.0.0.1:999/AppServer.aspx?TotalAmount='; var WXPAYSERVER='http://127.0.0.1:999/AppServer.aspx?TotalAmount='; // 2. 發起支付請求 function pay(id){ // 從伺服器請求支付訂單 var PAYSERVER=''; if(id=='alipay'){ PAYSERVER=ALIPAYSERVER; channel = aliChannel; }else if(id=='wxpay'){ PAYSERVER=WXPAYSERVER; channel = wxChannel; }else{ plus.nativeUI.alert("不支援此支付通道!",null,"捐贈"); return; } var xhr=new XMLHttpRequest(); var amount = document.getElementById('jine').value; var ddnoms=document.getElementById('Ddnom').innerText; alert(ddnoms); xhr.onreadystatechange=function(){ switch(xhr.readyState){ case 4: if(xhr.status==200){ plus.payment.request(channel,xhr.responseText,function(result){ plus.nativeUI.alert("支付成功!",function(){ back(); }); },function(error){ plus.nativeUI.alert("支付失敗:" + error.code); }); }else{ alert("獲取訂單資訊失敗!"); } break; default: break; } } xhr.open('GET',PAYSERVER+amount+"&PayDdnom="+ddnoms); xhr.send(); } </script> <script type="text/javascript" src="js/immersed.js" ></script> </body> </html>