diff --git a/app/controllers/donor/grant.controller.js b/app/controllers/donor/grant.controller.js index 586be46..1305ce0 100644 --- a/app/controllers/donor/grant.controller.js +++ b/app/controllers/donor/grant.controller.js @@ -2,12 +2,14 @@ const db = require('../../config/db.config.js'), errorMaker = require('../../helpers/error.maker'); const stripe = require('./stripe.controller'); +const sendgrid = require('../sendgrid.controller'); const { Grant, Cause, Region, Organization, sequelize } = db; // Create a grant for certain donor exports.create = (req, res, next) => { - const donor_id = req.user.id; + const user = req.user; + const { name, monthly, @@ -26,7 +28,7 @@ exports.create = (req, res, next) => { .transaction(function(t) { return Grant.create( { - donor_id, + donor_id: user.id, name, amount, monthly, @@ -51,8 +53,27 @@ exports.create = (req, res, next) => { }) .then(function(grants) { // transaction committed - stripe.grantCharge({ grant: grants.dataValues }, req, res); - return null; + stripe.grantCharge({ + grant: grants.dataValues, + stripeToken, + grant, + stripeToken, + organizations, + monthly, + amount, + user + }); + + sendgrid.paymentReceipt({ + organizations, + total_amount, + receiver: user.email + }); + + return res.status(200).json({ + message: 'Successfully charged or subscribed', + grant + }); }) .catch(function(error) { console.log(error); diff --git a/app/controllers/donor/stripe.controller.js b/app/controllers/donor/stripe.controller.js index 329ab86..02df688 100644 --- a/app/controllers/donor/stripe.controller.js +++ b/app/controllers/donor/stripe.controller.js @@ -6,32 +6,19 @@ const stripe = require('stripe')(process.env.STRIPE_KEY); const { Donor, Charge, PaymentPlan } = db; // Subscribe user to plan or one time charge -exports.grantCharge = async (data, req, res) => { - const { stripeToken, organizations, monthly, amount } = req.body; - const { grant } = await data; // passed from grant controller +exports.grantCharge = async ({ + grant, + stripeToken, + organizations, + monthly, + amount, + user +}) => { const grant_id = await grant.id; - const user = req.user; - const product_id = 'prod_ER3jOog6QMX1GP', - default_plan_id = 'default_plan'; // main Grants product - try { const donors = await Donor.findAll({ where: { id: user.id } }); - // check if already a stripe customer - let { stripe_id, subscription_id } = donors[0]; - - // if (!stripe_id) { - // const customer = await stripe.customers.create({ - // source: stripeToken, - // email: user.email - // }); - // stripe_id = await customer.id; - - // await Donor.update({ stripe_id }, { where: { id: user.id } }); - // } - // TODO: if there is, then update with the given source - var result; // charge if yes or no monthly @@ -65,10 +52,7 @@ exports.grantCharge = async (data, req, res) => { result = charge; - return res.status(200).json({ - message: 'Successfully charged or subscribed', - grant - }); + return null; } catch (error) { throw error; } diff --git a/app/controllers/sendgrid.controller.js b/app/controllers/sendgrid.controller.js index cbb1dda..0271769 100644 --- a/app/controllers/sendgrid.controller.js +++ b/app/controllers/sendgrid.controller.js @@ -6,38 +6,59 @@ sgMail.setApiKey(process.env.SENDGRID_KEY); const {} = db; -exports.testEmail = async (req, res) => { +exports.paymentReceipt = async ({ organizations, total_amount, receiver }) => { try { + var charitiesHtml = ''; + + organizations.forEach(charity => { + charitiesHtml += ` + ${ + charity.name + } + $${ + charity.amount + } + `; + }); + const msg = { - // personalizations: [ - // { - // to: { - // email: 'patelarjun50@gmail.com', - // name: 'Arjun Patel' - // }, - // dynamic_template_data: { - // donor_name: 'Arjun Patel', - // charities_list: - // '

Petes Animals $100

Goshalala $100

', - // date: 'April 1st, 2018' - // } - // } - // ], - // from: { email: 'no-reply@charify.com', name: 'Charify' }, - // template_id: 'd-569f804f5e7749cba66bac1994607280', - - // subject: 'Your Charity Bundle Payment - Charify', - // json: true - - to: 'patelarjun50@gmail.com', - from: 'no-reply@charify.com', + to: receiver, + from: { email: 'no-reply@charify.com', name: 'Charify Payments' }, template_id: 'd-569f804f5e7749cba66bac1994607280', substitutionWrappers: ['{{', '}}'], dynamic_template_data: { donor_name: 'Arjun Patel', - charities_html: - '

Petes Animals $100

Goshalala $100

', + charities_list: charitiesHtml, + total_amount, + date: 'April 1st, 2018', + subject: 'Your Charity Bundle Payment - Charify' + } + }; + const sentRes = await sgMail.send(msg); + + return res.status(200).json({ + message: 'Email sent' + }); + } catch (error) { + throw error; + } +}; + +exports.testEmail = async (req, res) => { + try { + var charitiesHtml = ''; + const total_amount = 320; + + const msg = { + to: 'patelarjun50@gmail.com', + from: { email: 'no-reply@charify.com', name: 'Charify Payments' }, + template_id: 'd-569f804f5e7749cba66bac1994607280', + + substitutionWrappers: ['{{', '}}'], + dynamic_template_data: { + donor_name: 'Arjun Patel', + total_amount, date: 'April 1st, 2018', subject: 'Your Charity Bundle Payment - Charify' }