温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序支付前端源码

发布时间:2020-10-20 00:27:20 来源:脚本之家 阅读:177 作者:oneall 栏目:web开发

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

//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#后端源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI