changes to flow of payments for different fees shown

This commit is contained in:
Arjun Patel
2019-04-30 03:29:03 -07:00
parent 98d165eab1
commit db960ee554
6 changed files with 79 additions and 74 deletions
+13 -13
View File
@@ -48,19 +48,6 @@ exports.createGrant = async (req, res, next) => {
} }
); );
const grantsOrgs = organizations.map(org => {
return {
grant_id: grant.id,
organization_id: org.id,
amount: org.amount
};
});
// mapping between bundle and orgs
await GrantOrganization.bulkCreate(grantsOrgs, {
transaction
});
// one time charge // one time charge
const charge = await stripe.grantCharge({ const charge = await stripe.grantCharge({
grant, grant,
@@ -72,6 +59,19 @@ exports.createGrant = async (req, res, next) => {
user user
}); });
const grantsOrgs = organizations.map(org => {
return {
grant_id: grant.id,
organization_id: org.id,
amount: org.finalAmountToOrg
};
});
// mapping between bundle and orgs
await GrantOrganization.bulkCreate(grantsOrgs, {
transaction
});
await Charge.create( await Charge.create(
{ {
id: charge.id, id: charge.id,
+4 -4
View File
@@ -22,9 +22,9 @@ exports.paymentReceipt = async ({
<td align="left" width="75%" style="padding: 6px 12px;font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 24px;">${ <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 charity.name
}</td> }</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;">$${ <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.amountWithStripeFees.toFixed(
charity.amount 2
}</td> )}</td>
</tr>`; </tr>`;
}); });
@@ -41,7 +41,7 @@ exports.paymentReceipt = async ({
bundle_id: grant.id, bundle_id: grant.id,
charities_list: charitiesHtml, charities_list: charitiesHtml,
total_amount, total_amount,
transaction_fees, transaction_fees: transaction_fees.toFixed(2),
date: currDate, date: currDate,
subject: 'Your Charify Bundle Payment - Charify' subject: 'Your Charify Bundle Payment - Charify'
} }
+59 -54
View File
@@ -15,9 +15,7 @@ exports.grantCharge = async ({
amount, amount,
user user
}) => { }) => {
return new Promise(async (resolve, reject) => { return await 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 } });
@@ -33,74 +31,81 @@ exports.grantCharge = async ({
receipt_email: donors[0].email receipt_email: donors[0].email
}); });
let transfers = await organizations.map(async (org, index) => { let updatedOrgs = await Promise.all(
const selectedOrgAmount = org.amount; organizations.map(async (org, index) => {
// this determines how much Charify takes from each donation const selectedOrgAmount = org.amount;
const stripeFee = // this determines how much payment processing is covered
Math.round((selectedOrgAmount * 0.029 + 0.3) * 1e2) / 1e2; const stripeFee =
org.amount = org.amount - 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 = const applicationAndStripeFee = applicationFee + stripeFee;
Math.round(selectedOrgAmount * 0.025 * 1e2) / 1e2;
const applicationAndStripeFee = applicationFee + stripeFee; let finalAmountToOrg = 0;
console.log(applicationAndStripeFee);
let finalAmountToOrg = 0;
let currOrg = await sequelize.query( let currOrg = await sequelize.query(
` `
SELECT stripe_account_id, charify_credit FROM organizations SELECT stripe_account_id, charify_credit FROM organizations
WHERE id = :org_id`, WHERE id = :org_id`,
{ {
type: db.Sequelize.QueryTypes.SELECT, type: db.Sequelize.QueryTypes.SELECT,
replacements: { org_id: org.id } replacements: { org_id: org.id }
} }
); );
if (currOrg[0].charify_credit) { if (currOrg[0].charify_credit) {
let updated_amt = currOrg[0].charify_credit; let updated_amt = currOrg[0].charify_credit;
if (currOrg[0].charify_credit < applicationAndStripeFee) { if (currOrg[0].charify_credit < applicationAndStripeFee) {
updated_amt = 0; updated_amt = 0;
// use up credit and take remaining as middle man // use up credit and take remaining as middle man
finalAmountToOrg = finalAmountToOrg =
selectedOrgAmount - selectedOrgAmount -
applicationAndStripeFee + applicationAndStripeFee +
currOrg[0].charify_credit; currOrg[0].charify_credit;
} else { } else {
updated_amt = currOrg[0].charify_credit - applicationAndStripeFee; updated_amt = currOrg[0].charify_credit - applicationAndStripeFee;
// take nothing as the middle man // take nothing as the middle man
finalAmountToOrg = selectedOrgAmount; finalAmountToOrg = selectedOrgAmount;
} }
await sequelize.query( await sequelize.query(
` `
UPDATE organizations UPDATE organizations
SET charify_credit = :updated_amt SET charify_credit = :updated_amt
WHERE id = :org_id`, WHERE id = :org_id`,
{ {
type: db.Sequelize.QueryTypes.UPDATE, type: db.Sequelize.QueryTypes.UPDATE,
replacements: { replacements: {
org_id: org.id, org_id: org.id,
updated_amt updated_amt
}
} }
} );
); } else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee;
} else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee;
// now scale to match stripe standards // for record in grant org table
let t = await stripe.transfers.create({ org.finalAmountToOrg = finalAmountToOrg;
amount: finalAmountToOrg * 100, org.amountWithStripeFees = selectedOrgAmount - stripeFee;
currency: 'usd',
source_transaction: charge.id,
destination: currOrg[0].stripe_account_id
});
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 // to show the transaction fees in email to giver
charge.transaction_fees = Math.round((amount * 0.029 + 0.3) * 1e2) / 1e2; charge.transaction_fees = Math.round((amount * 0.029 + 0.3) * 1e2) / 1e2;
// orgs with updated amounts after fees
charge.updatedOrgs = updatedOrgs;
resolve(charge); resolve(charge);
} catch (error) { } catch (error) {
+1 -1
View File
@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => {
{ {
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false }, id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
description: DataTypes.STRING, description: DataTypes.STRING,
amount: DataTypes.INTEGER, amount: DataTypes.DOUBLE,
payment_status: { payment_status: {
type: DataTypes.STRING, type: DataTypes.STRING,
+1 -1
View File
@@ -14,7 +14,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false allowNull: false
}, },
amount: { amount: {
type: DataTypes.INTEGER, type: DataTypes.DOUBLE,
allowNull: false allowNull: false
}, },
monthly: { monthly: {
+1 -1
View File
@@ -27,7 +27,7 @@ module.exports = (sequelize, DataTypes) => {
} }
}, },
amount: { amount: {
type: DataTypes.INTEGER, type: DataTypes.DOUBLE,
allowNull: false allowNull: false
} }
}, },