小程序支付(前端)

前端只需要调用 wx.requestPayment(Object object)
文档

参考代码

      const openId = wx.getStorageSync('openId')
        payOrder({
            payId: this.data.resData.payId,
            openId
        }).then((res) => {
            console.log(2222, res);
            try {
                const data = JSON.parse(res.res)
                console.log(22, data)
                const {
                    timeStamp,
                    nonceStr,
                    signType,
                    paySign
                } = data

                wx.requestPayment({
                    timeStamp,
                    nonceStr,
                    package: data.package,
                    signType,
                    paySign,
                    success() {
                        console.log('支付成功');
                        wx.showToast({
                            title: '支付成功',
                            icon: 'success',
                            duration: 1500,
                        });
                        setTimeout(() => {
                            wx.redirectTo({
                                url: '/pages/home/home',
                            })
                        }, 1500)
                    },
                    fail() {
                        console.log('支付失败');
                        wx.showToast({
                            title: '支付失败',
                            icon: 'error',
                            duration: 1500,
                        });
                    },
                })
            } catch (err) {
                console.log(err)
                wx.showToast({
                    title: err,
                    icon: 'error',
                    duration: 1500,
                });

                // Dialog.alert({
                //     title: "请求错误",
                //     message: err,
                //     confirmButtonColor: "#F16E30",
                    
                //     context: this // 添加this指向
                // });
            }

        }).catch((err) => {
            console.log(err, 10)
        }).finally(() => {
            // this.setData({
            //     btnLoading: false
            // })
            wx.hideLoading()
            // resolve()
        })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的微信小程序支付前端代码示例: ```javascript // 小程序页面中的支付按钮点击事件 pay: function() { // 调用后台接口获取预支付订单信息 wx.request({ url: '后台接口地址', method: 'POST', data: { // 传递给后台的参数,如商品信息、订单金额等 }, success: function(res) { // 获取后台返回的预支付订单信息 const orderInfo = res.data; // 调用微信支付接口 wx.requestPayment({ timeStamp: orderInfo.timeStamp, nonceStr: orderInfo.nonceStr, package: orderInfo.package, signType: 'MD5', paySign: orderInfo.paySign, success: function(res) { // 支付成功的处理逻辑 console.log('支付成功'); }, fail: function(res) { // 支付失败的处理逻辑 console.log('支付失败'); } }) }, fail: function(res) { // 请求失败的处理逻辑 console.log('请求失败'); } }) } ``` 在上述代码中,首先通过调用小程序内置的 `wx.request` 方法向后台发送请求,获取预支付订单信息。然后,使用 `wx.requestPayment` 方法调用微信支付接口,传入预支付订单信息进行支付操作。 在支付成功或失败的回调函数中,可以根据实际需求进行相应的处理,如更新订单状态、跳转到支付成功/失败页面等。 请注意,上述代码中的部分参数和接口地址需要根据实际情况进行替换和配置。同时,为了确保支付的安全性,需要在小程序后台服务器中进行签名验证和订单处理等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值