当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序支付前端源码

微信小程序支付前端源码

2018年08月29日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了微信小程序支付前端源码,供大家参考,具体内容如下

//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)
  }
 })
 }
})

本地调试如过出现请求失败请将    微信开发者工具  >    详情(右上角) > 不校验合法域名、web-view(业务域名)、tls 版本以及 https 证书   勾上即可

微信小程序支付c#后端源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网