您好,登錄后才能下訂單哦!
微信小程序 支付功能(前端)的實(shí)現(xiàn)
只提供微信小程序端代碼:
var app = getApp(); Page({ data: {}, onLoad: function (options) { // 頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(shù) var that = this //登陸獲取code wx.login({ success: function (res) { console.log(res.code) //獲取openid that.getOpenId(res.code) } }); }, getOpenId: function (code) { var that = this; wx.request({ url: "https://api.weixin.qq.com/sns/jscode2session?appid=小程序appid&secret=小程序應(yīng)用密鑰&js_code=" + code + "&grant_type=authorization_code", data: {}, method: 'GET', success: function (res) { that.generateOrder(res.data.openid) }, fail: function () { // fail }, complete: function () { // complete } }) }, /**生成商戶訂單 */ generateOrder: function (openid) { var that = this //統(tǒng)一支付 wx.request({ url: '后臺(tái)路徑', method: 'GET', data: { gfee: '商品價(jià)錢', gname: '商品名稱', openId:openid (商品價(jià)錢和商品名稱根據(jù)自身需要是否傳值,openid為必傳) }, success: function (res) { var pay = res.data //發(fā)起支付 var timeStamp = pay[0].timeStamp; var packages = pay[0].package; var paySign = pay[0].paySign; var nonceStr = pay[0].nonceStr; var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; that.pay(param) }, }) }, /* 支付 */ pay: function (param) { console.log("支付") console.log(param) wx.requestPayment({ timeStamp: param.timeStamp, nonceStr: param.nonceStr, package: param.package, signType: param.signType, paySign: param.paySign, success: function (res) { // success wx.navigateBack({ delta: 1, // 回退前 delta(默認(rèn)為1) 頁面 success: function (res) { wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 }) }, fail: function () { // fail }, complete: function () { // complete } }) }, fail: function (res) { // fail }, complete: function () { // complete } }) } })
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。