欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 支付功能(前端)的實現(xiàn)

 更新時間:2017年05月24日 14:24:36   投稿:lqh  
這篇文章主要介紹了微信小程序 支付功能(前端)的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下

微信小程序 支付功能(前端)的實現(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: '后臺路徑',
      method: 'GET',
      data: {
        gfee: '商品價錢',
        gname: '商品名稱',
        openId:openid
        (商品價錢和商品名稱根據(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
      }
    })
  }
})

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論