making stripe helper for creating grant a helper and transferring to specific connected accounts
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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,40 +15,53 @@ exports.grantCharge = async ({
|
|||||||
amount,
|
amount,
|
||||||
user
|
user
|
||||||
}) => {
|
}) => {
|
||||||
const grant_id = await grant.id;
|
return new Promise(async (resolve, reject) => {
|
||||||
|
const grant_id = await grant.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const donors = await Donor.findAll({ where: { id: user.id } });
|
const donors = await Donor.findAll({ where: { id: user.id } });
|
||||||
|
|
||||||
const stripeAmount = amount * 100;
|
const stripeAmount = amount * 100;
|
||||||
|
|
||||||
// charge if yes or no monthly
|
// charge if yes or no monthly
|
||||||
let charge = await stripe.charges.create({
|
let charge = await stripe.charges.create({
|
||||||
amount: stripeAmount,
|
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,
|
|
||||||
currency: 'usd',
|
currency: 'usd',
|
||||||
source_transaction: charge.id,
|
source: 'tok_visa',
|
||||||
destination: org.stripe_account_id
|
description: `Charity Bundle Payment`,
|
||||||
|
statement_descriptor: `charity bundle ${grant.id}`,
|
||||||
|
receipt_email: donors[0].email
|
||||||
});
|
});
|
||||||
return t;
|
|
||||||
});
|
|
||||||
|
|
||||||
return charge;
|
let transfers = await organizations.map(async (org, index) => {
|
||||||
} catch (error) {
|
let stripeOrgAmount = Math.round(org.amount * 100);
|
||||||
throw error;
|
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
|
// Delete grant's stripe plan under subscription
|
||||||
|
|||||||
Reference in New Issue
Block a user