changes to flow of payments for different fees shown
This commit is contained in:
@@ -15,9 +15,7 @@ exports.grantCharge = async ({
|
||||
amount,
|
||||
user
|
||||
}) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const grant_id = await grant.id;
|
||||
|
||||
return await new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const donors = await Donor.findAll({ where: { id: user.id } });
|
||||
|
||||
@@ -33,74 +31,81 @@ exports.grantCharge = async ({
|
||||
receipt_email: donors[0].email
|
||||
});
|
||||
|
||||
let transfers = await organizations.map(async (org, index) => {
|
||||
const selectedOrgAmount = org.amount;
|
||||
// this determines how much Charify takes from each donation
|
||||
const stripeFee =
|
||||
Math.round((selectedOrgAmount * 0.029 + 0.3) * 1e2) / 1e2;
|
||||
org.amount = org.amount - stripeFee;
|
||||
let updatedOrgs = await Promise.all(
|
||||
organizations.map(async (org, index) => {
|
||||
const selectedOrgAmount = org.amount;
|
||||
// this determines how much payment processing is covered
|
||||
const stripeFee =
|
||||
Math.round((selectedOrgAmount * 0.029 + 0.3) * 1e2) / 1e2;
|
||||
// this determines how much Charify takes from each donation
|
||||
const applicationFee =
|
||||
Math.round(selectedOrgAmount * 0.025 * 1e2) / 1e2;
|
||||
|
||||
const applicationFee =
|
||||
Math.round(selectedOrgAmount * 0.025 * 1e2) / 1e2;
|
||||
const applicationAndStripeFee = applicationFee + stripeFee;
|
||||
|
||||
const applicationAndStripeFee = applicationFee + stripeFee;
|
||||
console.log(applicationAndStripeFee);
|
||||
let finalAmountToOrg = 0;
|
||||
let finalAmountToOrg = 0;
|
||||
|
||||
let currOrg = await sequelize.query(
|
||||
`
|
||||
let currOrg = await sequelize.query(
|
||||
`
|
||||
SELECT stripe_account_id, charify_credit FROM organizations
|
||||
WHERE id = :org_id`,
|
||||
{
|
||||
type: db.Sequelize.QueryTypes.SELECT,
|
||||
replacements: { org_id: org.id }
|
||||
}
|
||||
);
|
||||
{
|
||||
type: db.Sequelize.QueryTypes.SELECT,
|
||||
replacements: { org_id: org.id }
|
||||
}
|
||||
);
|
||||
|
||||
if (currOrg[0].charify_credit) {
|
||||
let updated_amt = currOrg[0].charify_credit;
|
||||
if (currOrg[0].charify_credit) {
|
||||
let updated_amt = currOrg[0].charify_credit;
|
||||
|
||||
if (currOrg[0].charify_credit < applicationAndStripeFee) {
|
||||
updated_amt = 0;
|
||||
// use up credit and take remaining as middle man
|
||||
finalAmountToOrg =
|
||||
selectedOrgAmount -
|
||||
applicationAndStripeFee +
|
||||
currOrg[0].charify_credit;
|
||||
} else {
|
||||
updated_amt = currOrg[0].charify_credit - applicationAndStripeFee;
|
||||
// take nothing as the middle man
|
||||
finalAmountToOrg = selectedOrgAmount;
|
||||
}
|
||||
if (currOrg[0].charify_credit < applicationAndStripeFee) {
|
||||
updated_amt = 0;
|
||||
// use up credit and take remaining as middle man
|
||||
finalAmountToOrg =
|
||||
selectedOrgAmount -
|
||||
applicationAndStripeFee +
|
||||
currOrg[0].charify_credit;
|
||||
} else {
|
||||
updated_amt = currOrg[0].charify_credit - applicationAndStripeFee;
|
||||
// take nothing as the middle man
|
||||
finalAmountToOrg = selectedOrgAmount;
|
||||
}
|
||||
|
||||
await sequelize.query(
|
||||
`
|
||||
await sequelize.query(
|
||||
`
|
||||
UPDATE organizations
|
||||
SET charify_credit = :updated_amt
|
||||
WHERE id = :org_id`,
|
||||
{
|
||||
type: db.Sequelize.QueryTypes.UPDATE,
|
||||
replacements: {
|
||||
org_id: org.id,
|
||||
updated_amt
|
||||
{
|
||||
type: db.Sequelize.QueryTypes.UPDATE,
|
||||
replacements: {
|
||||
org_id: org.id,
|
||||
updated_amt
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
} else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee;
|
||||
);
|
||||
} else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee;
|
||||
|
||||
// now scale to match stripe standards
|
||||
let t = await stripe.transfers.create({
|
||||
amount: finalAmountToOrg * 100,
|
||||
currency: 'usd',
|
||||
source_transaction: charge.id,
|
||||
destination: currOrg[0].stripe_account_id
|
||||
});
|
||||
// for record in grant org table
|
||||
org.finalAmountToOrg = finalAmountToOrg;
|
||||
org.amountWithStripeFees = selectedOrgAmount - stripeFee;
|
||||
|
||||
return t;
|
||||
});
|
||||
// now scale to match stripe standards
|
||||
let t = await stripe.transfers.create({
|
||||
amount: finalAmountToOrg * 100,
|
||||
currency: 'usd',
|
||||
source_transaction: charge.id,
|
||||
destination: currOrg[0].stripe_account_id
|
||||
});
|
||||
|
||||
return org;
|
||||
})
|
||||
);
|
||||
|
||||
// to show the transaction fees in email to giver
|
||||
charge.transaction_fees = Math.round((amount * 0.029 + 0.3) * 1e2) / 1e2;
|
||||
// orgs with updated amounts after fees
|
||||
charge.updatedOrgs = updatedOrgs;
|
||||
|
||||
resolve(charge);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user