cleaning stripe and grant creation

This commit is contained in:
Arjun Patel
2019-03-15 21:10:36 -07:00
parent 8f35151338
commit 1568a99457
3 changed files with 80 additions and 54 deletions
+25 -4
View File
@@ -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);
+9 -25
View File
@@ -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;
}
+46 -25
View File
@@ -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 += `<tr>
<td align="left" width="75%" style="padding: 6px 12px;font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 24px;">${
charity.name
}</td>
<td align="left" width="25%" style="padding: 6px 12px;font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 24px;">$${
charity.amount
}</td>
</tr>`;
});
const msg = {
// personalizations: [
// {
// to: {
// email: 'patelarjun50@gmail.com',
// name: 'Arjun Patel'
// },
// dynamic_template_data: {
// donor_name: 'Arjun Patel',
// charities_list:
// '<div><p>Petes Animals $100</p><p>Goshalala $100</p></div>',
// 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:
'<div><p>Petes Animals $100</p><p>Goshalala $100</p></div>',
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'
}