PassportJS 和条纹 [英] PassportJS and Stripe

查看:58
本文介绍了PassportJS 和条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户创建帐户时向他们收费.一切都在之前的代码中设置并完美运行.当用户注册网站的高级"部分时,他们会创建一个帐户并使用以下代码收费.

I am attempting to charge a user when they create an account. Everything is set up and working perfect with the code before. When a user sign's up for a "premium" section of the website they have an account created and are charged with the code below.

问题:如果用户的信用卡最终因各种原因被拒绝...他们的用户帐户仍会创建.如果信用卡失败,我将如何更改下面的代码以使其无法到达该部分代码?

The problem: If a user's credit card ends up getting declined for various reasons... their user account is STILL created. How would I change my code below to not reach that part of the code if the credit card fails?

注意:当用户尝试使用被占用的用户名创建帐户时,这确实有效.Web 应用程序将它们重定向到/buy 以选择新用户名.但是,它无法处理信用卡错误,因为首先创建了用户.

Note: this DOES work for when the user trys to create an account with a username that is taken. The web app redirects them to /buy to select a new username. However it does not work to handle the credit card errors because the user is created first.

感谢您的帮助!

user.save(function(err) {
        console.log('this is the problem' + ' ' + err)
        if(err){
        return res.redirect('/buy')
        }
        var token = req.body.stripeToken; // Using Express
        var charge = stripe.charges.create({
        amount: 749,
        currency: "usd",
        description: "Website.com Premium - One time Payment",
        source: token,

        }, function(err, charge) {
            if(err) {
              console.log(err);
              return res.redirect('/buy')
            }
            console.log('charged')
            req.logIn(user, function(err) {
              if(err) {
                console.log(err);
              }
              console.log('all looks good')
              res.redirect('/results');
            });
        });
      });
    });

推荐答案

问题是您在将用户保存到数据库后处理付款.有两种方法可以解决这个问题.如果付款失败,您可以从数据库中删除用户.或者您可以处理付款,然后将用户保存到数据库中.所以基本上是切换顺序,哪些回调嵌套在哪些回调中.

The problem is you are handling the payment after you save the user to your database. There are two ways you could solve this. Either you could delete the user from your database if the payment fails. Or you could handle the payment THEN save the user to the database. So basically switching the order and which callbacks are nested inside of which callbacks.

我个人建议使用第二种解决方案,因为您将减少对数据库的调用,从而减轻数据库的压力和负载.

I'd personally suggest the second solution because you will be making less calls to your database which will reduce the stress and load on your database.

以下是如何实现这一目标的基本示例.我不知道您代码的所有细节,因此您可能需要进行一些调整以适应您的工作方式,但基本思想就是这样.

Here is a basic example of how to achieve that. I don't know all of the in and outs of your code so you might have to make a few adjustments to fit how you are doing things but the basic idea is the there.

var token = req.body.stripeToken; // Using Express
var charge = stripe.charges.create({
    amount: 749,
    currency: "usd",
    description: "Website.com Premium - One time Payment",
    source: token,

}, function(err, charge) {
    if (err) {
        console.log(err);
        return res.redirect('/buy')
    }
    console.log('charged')
    user.save(function(err) {
        console.log('this is the problem' + ' ' + err);
        if (err) {
            return res.redirect('/buy')
        }
        req.logIn(user, function(err) {
            if (err) {
                console.log(err);
            }
            console.log('all looks good')
            res.redirect('/results');
        });
    });
});

因此,您基本上是在创建条带费用,如果成功,则在您的数据库中创建用户.

So you are basically creating the stripe charge, and if it is successful then you create the user in your database.

希望这会有所帮助!

这篇关于PassportJS 和条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆