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, Project,
sequelize sequelize
} = db; } = db;
// Create a grant for certain donor // Create a grant for certain donor
exports.createGrant = async (req, res, next) => { exports.createGrant = async (req, res, next) => {
const user = req.user; const user = req.user;
var { name, monthly, organizations, amount, stripeToken } = req.body; 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; actual_total = Math.round(amount * 100) / 100;
if (!stripeToken) { if (!stripeToken) {
@@ -41,8 +39,8 @@ exports.createGrant = async (req, res, next) => {
name, name,
amount: actual_total, amount: actual_total,
monthly, monthly,
num_causes: causes.length, num_causes: organizations.length,
num_regions: regions.length num_regions: organizations.length
}, },
{ {
transaction transaction
@@ -100,7 +98,6 @@ exports.createGrant = async (req, res, next) => {
}); });
} catch (error) { } catch (error) {
if (error) await transaction.rollback(); if (error) await transaction.rollback();
console.log(error);
next(error); next(error);
} }
}; };
+18 -5
View File
@@ -4,7 +4,7 @@ const db = require('../../models'),
const stripe = require('stripe')(process.env.STRIPE_KEY); 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 // Subscribe user to plan or one time charge
exports.grantCharge = async ({ exports.grantCharge = async ({
@@ -15,6 +15,7 @@ exports.grantCharge = async ({
amount, amount,
user user
}) => { }) => {
return new Promise(async (resolve, reject) => {
const grant_id = await grant.id; const grant_id = await grant.id;
try { try {
@@ -32,23 +33,35 @@ exports.grantCharge = async ({
receipt_email: donors[0].email receipt_email: donors[0].email
}); });
let transfers = await organizations.map(async org => { let transfers = await organizations.map(async (org, index) => {
let stripeOrgAmount = Math.round(org.amount * 100); let stripeOrgAmount = Math.round(org.amount * 100);
const applicationStripeFee = stripeOrgAmount * 0.05; 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({ let t = await stripe.transfers.create({
amount: stripeOrgAmount - applicationStripeFee, amount: stripeOrgAmount - applicationStripeFee,
currency: 'usd', currency: 'usd',
source_transaction: charge.id, source_transaction: charge.id,
destination: org.stripe_account_id destination: orgs_stripe_acc[0].stripe_account_id
}); });
return t; return t;
}); });
return charge; resolve(charge);
} catch (error) { } catch (error) {
throw error; reject(error);
} }
});
}; };
// Delete grant's stripe plan under subscription // Delete grant's stripe plan under subscription