diff --git a/app/controllers/grant.controller.js b/app/controllers/grant.controller.js
index 2cb9c06..d5985b9 100644
--- a/app/controllers/grant.controller.js
+++ b/app/controllers/grant.controller.js
@@ -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
const charge = await stripe.grantCharge({
grant,
@@ -72,6 +59,19 @@ exports.createGrant = async (req, res, next) => {
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(
{
id: charge.id,
diff --git a/app/controllers/sendgrid.controller.js b/app/controllers/sendgrid.controller.js
index a61ecdb..0e27d55 100644
--- a/app/controllers/sendgrid.controller.js
+++ b/app/controllers/sendgrid.controller.js
@@ -22,9 +22,9 @@ exports.paymentReceipt = async ({
${
charity.name
} |
- $${
- charity.amount
- } |
+ $${charity.amountWithStripeFees.toFixed(
+ 2
+ )} |
`;
});
@@ -41,7 +41,7 @@ exports.paymentReceipt = async ({
bundle_id: grant.id,
charities_list: charitiesHtml,
total_amount,
- transaction_fees,
+ transaction_fees: transaction_fees.toFixed(2),
date: currDate,
subject: 'Your Charify Bundle Payment - Charify'
}
diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js
index cd36dd6..fb85594 100644
--- a/app/controllers/stripe.controller.js
+++ b/app/controllers/stripe.controller.js
@@ -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) {
diff --git a/models/Charge.js b/models/Charge.js
index 2af2f85..4aad483 100644
--- a/models/Charge.js
+++ b/models/Charge.js
@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => {
{
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
description: DataTypes.STRING,
- amount: DataTypes.INTEGER,
+ amount: DataTypes.DOUBLE,
payment_status: {
type: DataTypes.STRING,
diff --git a/models/Grant.js b/models/Grant.js
index e03bfe5..0cdfa37 100644
--- a/models/Grant.js
+++ b/models/Grant.js
@@ -14,7 +14,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
amount: {
- type: DataTypes.INTEGER,
+ type: DataTypes.DOUBLE,
allowNull: false
},
monthly: {
diff --git a/models/Grant_Organization.js b/models/Grant_Organization.js
index 25b48a4..0077f91 100644
--- a/models/Grant_Organization.js
+++ b/models/Grant_Organization.js
@@ -27,7 +27,7 @@ module.exports = (sequelize, DataTypes) => {
}
},
amount: {
- type: DataTypes.INTEGER,
+ type: DataTypes.DOUBLE,
allowNull: false
}
},