Home > other >  how to send mails using nodemailer after a sucessful purchase in stripe
how to send mails using nodemailer after a sucessful purchase in stripe

Time:08-10

i'm trying to send an email after successfully making a payment , but i'm find it hard to add my nodemailer code in stripe,

this is my stripe code


router.post("/pay", verifyToken, (req, res) => {
  let totalPrice = Math.round(req.body.totalPrice * 100);
  stripe.customers
    .create({
      email: req.decoded.email
    })
    .then(customer => {
      return stripe.customers.createSource(customer.id, {
        source: "tok_visa"
      });
    })
    .then(source => {
      return stripe.charges.create({
        amount: totalPrice,
        currency: "usd",
        customer: source.customer
      });
    })
    .then(async charge => {
      console.log("charge>", charge);
      let order = new Order();
      let cart = req.body.cart;

      cart.map(product => {
        order.products.push({
          productID: product._id,
          quantity: parseInt(product.quantity),
          price: product.price
        });
      });

      order.owner = req.decoded._id;
      order.estimatedDelivery = req.body.estimatedDelivery;
      await order.save();

      res.json({
        success: true,
        message: "Successfully made a payment"
      });
    })
    .catch(err => {
      res.status(500).json({
        success: false,
        message: err.message
      });
    });
});

this is my email template

 var emailTemplate = `Hello ${req.decoded.name}, \n
        thank you for your order! \n
        Engraving: ${newCharge.description} \n
        Amount: ${newCharge.amount / 100 } \n
        Your full order details are available at ecart.io/#/order-complete/${
          charge.id
        } \n
        For questions contact [email protected] \n 
        Thank you!`;
        let mailTransporter = nodemailer.createTransport({
          service: "gmail",
          auth: {
            user: "[email protected]",
            pass: "sq"
          }
        });

        let details = {
          from: "[email protected]",
          to: `${req.decoded.email}`,
          subject: "shipping",
          text: emailTemplate
        };
        mailTransporter.sendMail(details, err => {
          if (err) {
            console.log(err);
          } else {
            console.log("email sent");
          }
        });

tried adding it in my .then function but i'm not getting a good response I expected either an error message or the email sent to be logged but that is not happening.

CodePudding user response:

do these

 .then(source => {
      return stripe.charges.create({
        amount: totalPrice,
        currency: "usd",
        customer: source.customer
      },
function(err, charge) {
          if (err) {
            console.log(err);
          } else {
            var emailTemplate = `Hello ${req.decoded.name}, \n
          thank you for your order! \n
        
          Amount: ${charge.amount / 100} \n
          Your full order details are available at ecart.io/#/order-complete/${
            charge.id
          } \n
          For questions contact [email protected] \n 
          Thank you!`;
            let mailTransporter = nodemailer.createTransport({
              service: "gmail",
              auth: {
                user: "[email protected]",
                pass: "sq"
              }
            });

            let details = {
              from: "[email protected]",
              to: `${req.decoded.email}`,
              subject: "shipping",
              text: emailTemplate
            };
            mailTransporter.sendMail(details, err => {
              if (err) {
                console.log(err);
              } else {
                console.log("email sent");
              }
            });
          }
        },
);
    })
  • Related