title: Payment GateWay integration in nodejs and mongodb
body: <p>Hi I am Integration Payment GateWay to my app but i am stuck. for cod(Cash On Delivery) mode of payment it is working fine.but while in integration online payment gateway it is giving bit pain like i am creating payment link using instamojo when link is created successful; i  return that payment link to client and redirect user to that link </p>
  
  <p>1 if user fills card details successfully and instamojo hits my provided webhook(post url) with payment details
  2 what if user cancels tab or doesn't pays</p>
  
  <p>question here is where shall in create order in database. if  it is to be created on placeorder url of my app then i need to set order status incomplete and run a cron job for second condition (because order is already created and webhook is not hit by intamojo). is it  right way to do or there is other better ways to handle all this </p>
  
  <pre><code>Promise.all([getUpdatedCart(userId), findUser(userId), getDiscount(userId,couponCode)])
                  .then(function(resultArray) {
                      var cart = resultArray[0];
                      var user = resultArray[1];
                      var discountAmount = resultArray[2];
                      var offerId=null;
                      if (!cart)
                          return sendResponse(response,400,"error","Cart Not Found");
                      if (discountAmount&gt;0) 
                          var offerId=getOfferId(couponCode);
                      var order = {
                          user: user._id,
                          cart: cart._id,
                          shippingAddress:shippingAddressId,
                          billingAddress:billingAddressId,
                          paymenMethod: paymentMethod,
                          offer:offerId,
                          deliveryNote:deliveryNote,
                          amount:cart.amount
                      };
  
                      var newOrder = Order(order);
                      if (paymentMethod==='cod') 
                          newOrder.save(function(error,order){
                              if (!error)
                                  if (order){
                                      Cart.expireCart(cart._id);
                                      return sendResponse(response,201,"success",order);
                                  }
  
                          });
                      else if(paymentMethod==='intamojo'){
                          var purpose='Order Number-'+ newOrder.id;
                          Instamojo.setHeaders(InstaConfig.test.API_KEY, InstaConfig.test.API_AUTH_TOKEN);
                          var amountPayable = cart.amount - discountAmount;
                          var data = generatePayload(user, purpose, amountPayable);
                          Instamojo.createPaymentLink(data, function(error, resultResponse, body) {
                              if (resultResponse &amp;&amp; body &amp;&amp; resultResponse.statusCode===201)
                                  return sendResponse(response,200,"success",body.longUrl+"?embed=form");
                          });
                      }
                      else if(paymentMethod==='payumoney'){
  
                      }
                      else
                          return sendResponse(response,400,"error","Invalid Mode of Payment");
  
                  })      
                  .catch(function(error) {
                      return sendResponse(response,400,"error",error);
                  });
  </code></pre>
  
  <p>Can anyone Please help if i need to write cron job kindly suggest library for that</p>