Guest User

Untitled

a guest
Jul 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. });
  2.  
  3. //payment route
  4. router.post('/billing/:plan_name', function(req, res, next) {
  5. var plan = req.params.plan_name;
  6. var stripeToken = req.body.stripeToken;
  7. console.log(stripeToken);
  8.  
  9. if (!stripeToken) {
  10. req.flash('errors', { msg: 'Please provide a valid card.' });
  11. return res.redirect('/awesome');
  12. }
  13.  
  14. User.findById({ _id: req.user._id}, function(err, user) {
  15. if (err) return next(err);
  16.  
  17. stripe.customers.create({
  18. source: stripeToken, // obtained with Stripe.js
  19. plan: plan,
  20. email: user.email
  21. }).then(function(customer) {
  22. user.stripe.plan = customer.plan;
  23. user.stripe.customerId = customer.id;
  24. console.log(customer);
  25. user.save(function(err) {
  26. console.log("Success");
  27. if (err) return next(err);
  28. return next(null);
  29. });
  30. }).catch(function(err) {
  31. // Deal with an error
  32. });
  33.  
  34. return res.redirect('/');
  35.  
  36. });
  37. });
  38.  
  39. router.post('/billing/catch_paid_invoice', function(req, res) {
  40. // Here you parse JSON data from Stripe
  41. }):
  42.  
  43. If today's date <= user.active_until
  44. allow them access
  45.  
  46. Else
  47. show them an account expired message
  48.  
  49. if event type is "invoice.payment_succeeded"
  50. then update user.active_until to be equal to today's date + 1 month
Add Comment
Please, Sign In to add comment