making stripe helper for creating grant a helper and transferring to specific connected accounts

This commit is contained in:
Arjun Patel
2019-04-13 14:43:50 -07:00
parent 7b38620605
commit 9ed96806fb
2 changed files with 45 additions and 35 deletions
+3 -6
View File
@@ -14,15 +14,13 @@ const {
Project,
sequelize
} = db;
// Create a grant for certain donor
exports.createGrant = async (req, res, next) => {
const user = req.user;
var { name, monthly, organizations, amount, stripeToken } = req.body;
const causes = organizations.map(org => org.primary_cause);
const regions = organizations.map(org => org.primary_region);
actual_total = Math.round(amount * 100) / 100;
if (!stripeToken) {
@@ -41,8 +39,8 @@ exports.createGrant = async (req, res, next) => {
name,
amount: actual_total,
monthly,
num_causes: causes.length,
num_regions: regions.length
num_causes: organizations.length,
num_regions: organizations.length
},
{
transaction
@@ -100,7 +98,6 @@ exports.createGrant = async (req, res, next) => {
});
} catch (error) {
if (error) await transaction.rollback();
console.log(error);
next(error);
}
};
+42 -29
View File
@@ -4,7 +4,7 @@ const db = require('../../models'),
const stripe = require('stripe')(process.env.STRIPE_KEY);
const { Donor, Organization, Charge, PaymentPlan } = db;
const { Donor, Organization, Charge, PaymentPlan, sequelize } = db;
// Subscribe user to plan or one time charge
exports.grantCharge = async ({
@@ -15,40 +15,53 @@ exports.grantCharge = async ({
amount,
user
}) => {
const grant_id = await grant.id;
return new Promise(async (resolve, reject) => {
const grant_id = await grant.id;
try {
const donors = await Donor.findAll({ where: { id: user.id } });
try {
const donors = await Donor.findAll({ where: { id: user.id } });
const stripeAmount = amount * 100;
const stripeAmount = amount * 100;
// charge if yes or no monthly
let charge = await stripe.charges.create({
amount: stripeAmount,
currency: 'usd',
source: 'tok_visa',
description: `Charity Bundle Payment`,
statement_descriptor: `charity bundle ${grant.id}`,
receipt_email: donors[0].email
});
let transfers = await organizations.map(async org => {
let stripeOrgAmount = Math.round(org.amount * 100);
const applicationStripeFee = stripeOrgAmount * 0.05;
let t = await stripe.transfers.create({
amount: stripeOrgAmount - applicationStripeFee,
// charge if yes or no monthly
let charge = await stripe.charges.create({
amount: stripeAmount,
currency: 'usd',
source_transaction: charge.id,
destination: org.stripe_account_id
source: 'tok_visa',
description: `Charity Bundle Payment`,
statement_descriptor: `charity bundle ${grant.id}`,
receipt_email: donors[0].email
});
return t;
});
return charge;
} catch (error) {
throw error;
}
let transfers = await organizations.map(async (org, index) => {
let stripeOrgAmount = Math.round(org.amount * 100);
const applicationStripeFee = stripeOrgAmount * 0.05;
let orgs_stripe_acc = await sequelize.query(
`
SELECT stripe_account_id FROM organizations
WHERE id = :org_id`,
{
type: db.Sequelize.QueryTypes.SELECT,
replacements: { org_id: org.id }
}
);
let t = await stripe.transfers.create({
amount: stripeOrgAmount - applicationStripeFee,
currency: 'usd',
source_transaction: charge.id,
destination: orgs_stripe_acc[0].stripe_account_id
});
return t;
});
resolve(charge);
} catch (error) {
reject(error);
}
});
};
// Delete grant's stripe plan under subscription