微信小程序微信支付功能

前端做的东西很少,大部分都需要后端来做

前端完整代码


      wx.login({
            success(res) {
                console.log(res.code);
                wx.request({
                    url: '服务器的URL',
                    data: {
                        "appid":  小程序appid,
                        "secret":  小程序密钥,
                        "js_code": res.code,
                        "grant_type": "authorization_code"
                    },
                    method: "POST",
                    header: {
                        'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                        if (res.data.openid) {
                           //获取openid
                            wx.request({
                                url: '服务器的URL',
                                data: {
                                    "appid": 小程序appid,
                                    "openid":openid,
                                    "key": 商户密钥,//在商户平台设置
                                    "mch_id": 商户号,
                                    "total_fee": 支付金额
                                },
                                method: "POST",
                                header: {
                                    'content-type': 'application/json' // 默认值
                                },
                                success: function (res) {
                                    console.log(res.data);
                                    //请求微信号支付接口
                                    wx.requestPayment({
                                        'timeStamp': res.data.timeStamp,
                                        'nonceStr': res.data.nonceStr,
                                        'package': res.data.package,
                                        'signType': 'MD5',
                                        'paySign': res.data.paySign,
                                        'success': function (res) {
                                            console.log(res);
                                            wx.showToast({
                                                title: '支付成功',
                                                icon: 'success',
                                                duration: 2000
                                            })
                                        },
                                        'fail': function (res) {
                                            wx.showToast({
                                                title: '支付失败',
                                                icon: 'success',
                                                duration: 2000
                                            })
                                        }
                                    })
                                }
                            })
                        }
                    }
                })
            }
        })

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前些日子从@张鑫旭微博处得一份推荐(Front-end-tutorial),号称最全的资源教程-前端涉及的所有知识...
    谷子多阅读 4,267评论 0 44
  • 前端知识体系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋风喵阅读 12,523评论 7 163
  • 努力奋进勇猛精进的人,自然是美艳动人的,人在做天在看,结果差不了的。 即使差了,那结果是结果,过程是过程,不交缠!...
    纵情嬉戏天地间阅读 259评论 0 0
  • 题目描述喷水装置(二)时间限制:3000 ms | 内存限制:65535 KB难度:4 描述有一块草坪,横向长...
    意浅离殇阅读 236评论 0 0
  • 心路成长初期 学习很多知识 尝试正心正念正能量 就误以为有负面情绪是不应该的 当别人有情绪 我们急于提醒或者说教 ...
    Jenny_79c5阅读 205评论 0 0