重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文实例为大家分享了微信小程序支付前端源码,供大家参考,具体内容如下
创新互联建站公司2013年成立,是专业互联网技术服务公司,拥有项目网站设计制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元莒县做网站,已为上家服务,为莒县各地企业和个人服务,联系电话:028-86922220//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 //(商品价钱和商品名称根据自身需要是否传值, openid为必传) }, success: function (res) { console.log("后台获取数据成功"); console.log(res); var param = { "timeStamp": res.data.timeStamp, "package": res.data.package, "paySign": res.data.paySign, "signType": "MD5", "nonceStr": res.data.nonceStr }; //发起支付 that.pay(param); }, fail: function (res) { console.log("向后台发送数据失败") } }) }, //支付 pay: function (param) { var that = this; 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) { console.log("success"); console.log(res); }, fail: function (res) { console.log("fail") console.log(res); }, complete: function (res) { console.log("complete"); console.log(res) } }) } })