微信小程序支付-非常详细

首先在我们提交订单的时候,先创建订单

//提交订单
    submitOrder() {
        // console.log(this.data.goodsid);
        if(!this.data.hasDefaultAddress){
            wx.showToast({
              title: '请您先设置地址',
              icon:"error"
            })
        }else{
            //调取创建订单方法
            this.createOrder();
            this.setData({
                //支付弹出层
                playshow: true
            })
        }
        
    },

生成订单

//生成订单,注意这里有直播,根据需求看需要传什么参数来创建订单
    createOrder() {
        var that = this;
        let usercode = wx.getStorageSync('');//用户code
        let zhiId = wx.getStorageSync('LiveID');
        // console.log(zhiId);
        let goodid = that.data.goodsid; //商品id
        let sukid = that.data.skuid; //规格skuid
        let goodnum = that.data.nums //商品数量
        let ptOrderCode = that.data.ptOrderCode; //收获id
        let Remark = ''; //签名
        let ZbjCode = zhiId; //直播佳丽id
        let jialiid = that.data.jialiid
        console.log(jialiid);
        //如果收货地址为空
        if (ptOrderCode == '') {
            wx.showToast({
                title: '请选择收货地址',
                icon: 'none',
            })
            return false;
        }
        let data = {
            UsersCode: usercode,
            GoodsIds: goodid,
            Nums: goodnum,
            ReceiptId: ptOrderCode,
            ParaIds: sukid,
            Remark: Remark,
            ZbjCode: ZbjCode,
            JialiId:jialiid
        }
        console.log(data);
        “http请求”(data).then(res => {
            console.log(res);
            if (res.data.code == 1) {
                //订单号
                let orderCode = res.data.OrderCode;
                this.setData({
                    palyorder: orderCode
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
                icon: 'none',
            })
        })
    },

获取code,也就是微信给我们返回的一个标识

  //获取code
    getcode() {
        let that = this
        wx.login({
            success: function (res) {
                console.log(res)
                if (res.code) {
                    that.setData({
                        code: res.code
                    })
                    // 获取code成功后 获取OpenId,AccessToken
                    that.getOpenid();
                } else {
                    // console.log('获取用户登录态失败!' + res.errMsg)
                    wx.showToast({
                        title: '获取用户登录态失败!',
                        icon: 'none',
                    })
                }
            }
        });
    },

然后我们拿着code来,获取OpenID

  //获取openid
    getOpenid() {
        let that = this
        //获取openid接口
        let codesss = {
            code: this.data.code
        }
        // console.log(that);
        "http请求"(codesss).then((res) => {
            console.log(res);
            /*   console.log(res.data.openid);
              console.log(res.data.session_key); */
            if (res.data.code == 1) {
                that.setData({
                    //获取到openid
                    openid: res.data.openid
                })
                //调取微信预支付
                // console.log(that);
                that.wxPayment();
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                })
            }
        })
    },

然后吊起微信支付wx.requestPayment

 //获取微信支付
    wxPayment() {
        let that = this;
        let body = "购买商品" //根据需求传递参数
        let openid = this.data.openid;
        let payMoney = this.data.payMoney; //商品价格
        let order = this.data.palyorder //商品订单
        let datas = {
            OpenId: openid,
            total_fee: payMoney,
            body: body,
            out_trade_no: order
        }
        “发送http请求”(datas).then((res) => {
            // console.log("222:" + res.data.result);
            if (res.data.result == 0) {
                console.log(res);
                // console.log("111:" + res.data.noncestr);
                wx.requestPayment({
                    nonceStr: res.data.noncestr,
                    package: 'prepay_id=' + res.data.prepayid,
                    paySign: res.data.sign,
                    timeStamp: res.data.timestamp,
                    signType: 'MD5',
                    success(res) {
                        wx.showToast({
                            title: '支付成功!',
                            icon: 'none',
                            duration: 2000,
                            success: function () {
                                that.getUserInfo();
                                that.setData({
                                    rechargeMoney: '',
                                    payResult: '0'
                                })
                            }
                        })
                        //支付成功后让他返回当前订单页面
                        wx.navigateTo({
                            url: '../myorder/myorder',
                        })
                    },
                    fail(res) {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none',
                        })
                    }
                })
            }
        })
    },

最后搞定,关注小生快乐一生!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

S小生

感谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值