Monday, 1 May 2017

Run multiple functions on backend and route to frontend on success

I have a stripe payment form that is being submitted (currently to the backend). Here's the flow:

form submits to /api/stripeAPI, which calls the first controller.

module.exports.stripePayment = function(req, res) {
  //some variables here
   if(!customer.stripeCustomer) {
    userStripeCustomer(customer, cart);
   } else {
     chargeCustomer(customer, cart);
   }
};

var userStripeCustomer = function(customer, cart) {
  //create stripe customer
  stripe.customers.create({
    email: customer.stripeEmail,
    source: customer.stripeToken
  }).then(function(stripecustomer){
      User.findOneAndUpdate({username: customer.username}, {$set:{stripeCustomer: stripecustomer.id }}, {new: true}, function(err, user){
        if(err) { 
          return; 
        } else { 
          customer.stripeCustomer = user.stripeCustomer;
          chargeCustomer(customer, cart);
        }
      });
  }).catch(function(error) {
    console.log("There was an error creating the stripe customer",error);
    return;
  });

};

var chargeCustomer = function(customer, cart) {
  stripe.charges.create({
        //stripe charge data
      }).then(function(charge) {
        userNewOrder(charge, cart);
    }).catch(function(err, res) {
      res.status(500).json(err);
  });
};

var userNewOrder = function(charge, cart, req, res) {
      Order
      .create({
        //cart info
      }, function(err, order) {
      if (err) {
        console.log("Error creating order",err);
        res.status(400).json(err);
      } else {
        saveUserRegistrations(order);
      }
    });
};

var saveUserRegistrations = function(order, req, res) {
    //define variables needed
    for (var i in registrations) {
        registrations[i].paymentId = paymentId;
        registrations[i].users = userobject;
    }
    saveRegistrations(registrations);
};

var generateRegistrationsArray = function(order) {
        var arr = [];
        for(var i=0; i < order.cart.items.length; i++) {
            arr.push({
              // registration info
            });
        }
      return arr;
    };
var saveRegistrations = function(registrations, req, res) {
  var reginfo = registrations;
    Registration
    .collection.insert(registrations)
    .then(function(r) {
      nodemailerConfirmation(reginfo);
    });
  };

var nodemailerConfirmation = function(registrations, req, res, $window) {
  transporter.sendMail({
    //mail config details 
}, function(err){
  if(!err){
    console.log("Sent successfully!");
    // GET BACK TO FRONTEND (page that uses angular-controller
    // OR GET TO angular-controller to clear cart and display info
    // res.sendFile("../../public/checkout-success.html", {root: __dirname}); //TRIED BUT FAILED
    //res.json({success: true, registrations: registrations});  //ALSO FAILS
  } else {
    console.log("There was an error sending the email", err.message);
    return;
  }
});
};

The last function to be called is nodemailerConfirmation(), which does what I want.

Is there something I can do to get back to the frontend from there, or is there a way to submit the form to an angular controller, then sending a response back when the code on the server is finished?



via user3561890

No comments:

Post a Comment