本文實(shí)例為大家分享了微信小程序支付前端源碼,供大家參考,具體內(nèi)容如下
//index.js Page({ data: { }, //點(diǎn)擊支付按鈕進(jìn)行支付 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',//后臺(tái)請(qǐng)求地址 method: 'GET', data: { gfee: '商品價(jià)錢(qián)', gname: '商品名稱', openId: openid //(商品價(jià)錢(qián)和商品名稱根據(jù)自身需要是否傳值, openid為必傳) }, success: function (res) { console.log("后臺(tái)獲取數(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("向后臺(tái)發(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) } }) } })
分享名稱:微信小程序支付前端源碼-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://m.kartarina.com/article7/hsgoj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、軟件開(kāi)發(fā)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容