cleaning stripe and grant creation
This commit is contained in:
@@ -2,12 +2,14 @@ const db = require('../../config/db.config.js'),
|
|||||||
errorMaker = require('../../helpers/error.maker');
|
errorMaker = require('../../helpers/error.maker');
|
||||||
|
|
||||||
const stripe = require('./stripe.controller');
|
const stripe = require('./stripe.controller');
|
||||||
|
const sendgrid = require('../sendgrid.controller');
|
||||||
|
|
||||||
const { Grant, Cause, Region, Organization, sequelize } = db;
|
const { Grant, Cause, Region, Organization, sequelize } = db;
|
||||||
|
|
||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.create = (req, res, next) => {
|
exports.create = (req, res, next) => {
|
||||||
const donor_id = req.user.id;
|
const user = req.user;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
monthly,
|
monthly,
|
||||||
@@ -26,7 +28,7 @@ exports.create = (req, res, next) => {
|
|||||||
.transaction(function(t) {
|
.transaction(function(t) {
|
||||||
return Grant.create(
|
return Grant.create(
|
||||||
{
|
{
|
||||||
donor_id,
|
donor_id: user.id,
|
||||||
name,
|
name,
|
||||||
amount,
|
amount,
|
||||||
monthly,
|
monthly,
|
||||||
@@ -51,8 +53,27 @@ exports.create = (req, res, next) => {
|
|||||||
})
|
})
|
||||||
.then(function(grants) {
|
.then(function(grants) {
|
||||||
// transaction committed
|
// transaction committed
|
||||||
stripe.grantCharge({ grant: grants.dataValues }, req, res);
|
stripe.grantCharge({
|
||||||
return null;
|
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) {
|
.catch(function(error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
|||||||
@@ -6,32 +6,19 @@ const stripe = require('stripe')(process.env.STRIPE_KEY);
|
|||||||
const { Donor, Charge, PaymentPlan } = db;
|
const { Donor, Charge, PaymentPlan } = db;
|
||||||
|
|
||||||
// Subscribe user to plan or one time charge
|
// Subscribe user to plan or one time charge
|
||||||
exports.grantCharge = async (data, req, res) => {
|
exports.grantCharge = async ({
|
||||||
const { stripeToken, organizations, monthly, amount } = req.body;
|
grant,
|
||||||
const { grant } = await data; // passed from grant controller
|
stripeToken,
|
||||||
|
organizations,
|
||||||
|
monthly,
|
||||||
|
amount,
|
||||||
|
user
|
||||||
|
}) => {
|
||||||
const grant_id = await grant.id;
|
const grant_id = await grant.id;
|
||||||
|
|
||||||
const user = req.user;
|
|
||||||
const product_id = 'prod_ER3jOog6QMX1GP',
|
|
||||||
default_plan_id = 'default_plan'; // main Grants product
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const donors = await Donor.findAll({ where: { id: user.id } });
|
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;
|
var result;
|
||||||
|
|
||||||
// charge if yes or no monthly
|
// charge if yes or no monthly
|
||||||
@@ -65,10 +52,7 @@ exports.grantCharge = async (data, req, res) => {
|
|||||||
|
|
||||||
result = charge;
|
result = charge;
|
||||||
|
|
||||||
return res.status(200).json({
|
return null;
|
||||||
message: 'Successfully charged or subscribed',
|
|
||||||
grant
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,38 +6,59 @@ sgMail.setApiKey(process.env.SENDGRID_KEY);
|
|||||||
|
|
||||||
const {} = db;
|
const {} = db;
|
||||||
|
|
||||||
exports.testEmail = async (req, res) => {
|
exports.paymentReceipt = async ({ organizations, total_amount, receiver }) => {
|
||||||
try {
|
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 = {
|
const msg = {
|
||||||
// personalizations: [
|
to: receiver,
|
||||||
// {
|
from: { email: 'no-reply@charify.com', name: 'Charify Payments' },
|
||||||
// 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',
|
|
||||||
template_id: 'd-569f804f5e7749cba66bac1994607280',
|
template_id: 'd-569f804f5e7749cba66bac1994607280',
|
||||||
|
|
||||||
substitutionWrappers: ['{{', '}}'],
|
substitutionWrappers: ['{{', '}}'],
|
||||||
dynamic_template_data: {
|
dynamic_template_data: {
|
||||||
donor_name: 'Arjun Patel',
|
donor_name: 'Arjun Patel',
|
||||||
charities_html:
|
charities_list: charitiesHtml,
|
||||||
'<div><p>Petes Animals $100</p><p>Goshalala $100</p></div>',
|
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',
|
date: 'April 1st, 2018',
|
||||||
subject: 'Your Charity Bundle Payment - Charify'
|
subject: 'Your Charity Bundle Payment - Charify'
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user