Thursday 4 May 2017

PassportJS and Stripe

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



via AndrewLeonardi

No comments:

Post a Comment