微信小程序支付前端源碼
本文實例為大家分享了微信小程序支付前端源碼,供大家參考,具體內(nèi)容如下
//index.js Page({ data: { }, //點擊支付按鈕進行支付 payclick: function () { var t = this; wx.login({ //獲取code換取openID success: function (res) { //code = res.code //返回code console.log("獲取code"); console.log(res.code); var opid = t.getOpenId(res.code); } }) }, //獲取openID getOpenId: function (code) { var that = this; wx.request({ url: "https://api.weixin.qq.com/sns/jscode2session?appid=你的appid&secret=AppSecret(小程序密鑰)&js_code=" + code + "&grant_type=authorization_code", data: {}, method: 'GET', success: function (res) { console.log("獲取openid") console.log(res) that.setData({ openid: res.data.openid, session_key: res.data.session_key }) that.generateOrder(res.data.openid) }, fail: function () { // fail }, complete: function () { // complete } }) }, //生成商戶訂單 generateOrder: function (openid) { var that = this wx.request({ url: 'http://localhost:25492/wx/getda',//后臺請求地址 method: 'GET', data: { gfee: '商品價錢', gname: '商品名稱', openId: openid //(商品價錢和商品名稱根據(jù)自身需要是否傳值, openid為必傳) }, success: function (res) { console.log("后臺獲取數(shù)據(jù)成功"); console.log(res); var param = { "timeStamp": res.data.timeStamp, "package": res.data.package, "paySign": res.data.paySign, "signType": "MD5", "nonceStr": res.data.nonceStr }; //發(fā)起支付 that.pay(param); }, fail: function (res) { console.log("向后臺發(fā)送數(shù)據(jù)失敗") } }) }, //支付 pay: function (param) { var that = this; console.log("發(fā)起支付") console.log(param) wx.requestPayment({ timeStamp: param.timeStamp, nonceStr: param.nonceStr, package: param.package, signType: param.signType, paySign: param.paySign, success: function (res) { console.log("success"); console.log(res); }, fail: function (res) { console.log("fail") console.log(res); }, complete: function (res) { console.log("complete"); console.log(res) } }) } })
本地調(diào)試如過出現(xiàn)請求失敗請將 微信開發(fā)者工具 > 詳情(右上角) > 不校驗合法域名、web-view(業(yè)務域名)、TLS 版本以及 HTTPS 證書 勾上即可
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript?error瀏覽器端錯誤捕獲處理方法筆記解決示例
這篇文章主要為大家介紹了JavaScript?error瀏覽器端錯誤捕獲處理方法筆記解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06解讀TypeScript與JavaScript的區(qū)別
這篇文章主要介紹了TypeScript與JavaScript的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

layui數(shù)據(jù)表格實現(xiàn)重載數(shù)據(jù)表格功能(搜索功能)

javascript面向?qū)ο笾畉his關(guān)鍵詞用法分析

JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之棧詳解