微信小程序與webview交互實(shí)現(xiàn)支付功能
實(shí)現(xiàn)原理:點(diǎn)擊h5網(wǎng)頁的支付按鈕——(跳轉(zhuǎn))——>嵌套改h5的小程序的支付頁面——(處理支付)——>跳轉(zhuǎn)至支付完成后的頁面
注意:(1)網(wǎng)頁h5中,引入微信的jssdk
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
(2)小程序嵌套h5頁面后,需要在微信公眾平臺(tái)配置h5網(wǎng)頁的業(yè)務(wù)邏輯名,否則無法訪問(且配置業(yè)務(wù)邏輯名的小程序只能是企業(yè)小程序,個(gè)人小程序暫時(shí)無法實(shí)現(xiàn))。
操作:登錄微信公眾平臺(tái)————開發(fā)——————開發(fā)設(shè)置

小程序目錄

<!--webview中(小程序page)-->
//pages/lnyc2019/index.wxml
<web-view class='page_web' src="{{url}}"></web-view>
//pages/lnyc2019/index.js
Page({
data: {
url:'https://xxxxxxxx/wxmini/index.html'//h5地址
}
})
<!--wxPay中(小程序page)-->
// pages/wxPay/index.js
Page({
data: {
payTempcode:'',
ordercode:'',
payParam:{}
},
onLoad: function (options) {
console.log('支付開始');
console.log(options);
this.setData({
ordercode: options.ordercode
});
this.getTempcode();
},
// 換取支付臨時(shí)code
getTempcode:function(){
wx.login({
success: res => {
// 發(fā)送 res.code 到后臺(tái)換取 openId, sessionKey, unionId
this.setData({
payTempcode:res.code
});
console.log('支付code:', this.data.payTempcode);
this.getPayinfo();
}
})
},
// 換取支付參數(shù)
getPayinfo:function(){
var self=this;
wx.request({
url: 'https://xxxxxx/pay/xcxpay',//后臺(tái)接口地址
data: {
'wxcode': self.data.payTempcode,
'ordercode': self.data.ordercode,
'gid': x,
},
method: 'POST',
success: function (res) {
console.log(res.data.data.payinfo);
self.setData({
payParam: res.data.data.payinfo
});
console.log('支付的訂單====',self.data.ordercode);
// 調(diào)起支付
wx.requestPayment({
'timeStamp': self.data.payParam.time,//為字符串,否則報(bào)錯(cuò)
'nonceStr': self.data.payParam.nonce_str,
'package': `prepay_id=${self.data.payParam.prepay_id}`,
'signType': 'MD5',
'paySign': self.data.payParam.paysign,
'success': function (res) {
console.log(res)
console.log('=======支付成功==========');
wx.navigateTo({
url: `/pages/lnyc2019/detail?ordercode=${self.data.ordercode}`
})
},
'fail': function (res) {
console.log(res)
console.log('=======支付失敗==========')
wx.navigateBack({
delta: 1//返回1個(gè)頁面
})
}
})
}
})
}
})
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
相關(guān)文章
html+css+js實(shí)現(xiàn)別踩白板小游戲
大家好,本篇文章主要的講的是html+css+js實(shí)現(xiàn)別踩白板小游戲,感興趣的同學(xué)趕快來看一看吧,覺得不錯(cuò)的話可以收藏一下哦,方便下次瀏覽2021-11-11
bootstarp modal框居中顯示的實(shí)現(xiàn)代碼
這篇文章主要介紹了bootstarp modal框居中顯示的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02
BootStrap點(diǎn)擊下拉菜單項(xiàng)后顯示一個(gè)新的輸入框?qū)崿F(xiàn)代碼
這篇文章主要介紹了BootStrap點(diǎn)擊下拉菜單項(xiàng)后顯示一個(gè)新的輸入框?qū)崿F(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-05-05
深入認(rèn)識(shí)JavaScript中的函數(shù)
深入認(rèn)識(shí)JavaScript中的函數(shù)...2007-01-01
JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的區(qū)別和應(yīng)用場景簡述
隨著web2.0的彪悍發(fā)展,以及瀏覽器端所承載的工作越來越大(在不是很影響性能的情況下,開發(fā)者都習(xí)慣把能用瀏覽器做的事兒都讓瀏覽器做,以減輕服務(wù)器的壓力和帶寬費(fèi)用等)。2010-04-04
深入理解javascript動(dòng)態(tài)插入技術(shù)
這篇文章介紹了javascript動(dòng)態(tài)插入技術(shù),有需要的朋友可以參考一下2013-11-11

