PassportJS and Stripe

时间秒杀一切 提交于 2019-12-13 07:34:28

问题


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?

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.

Thank you for the help!

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');
            });
        });
      });
    });

回答1:


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.

Hopefully this helps!



来源:https://stackoverflow.com/questions/43788021/passportjs-and-stripe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!