From 98d165eab12afb9d24e3ddcfd1a1f3aef6b67e98 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Tue, 30 Apr 2019 01:42:58 -0700 Subject: [PATCH] properly showing transaction fees with stripe in email and deductions --- app/controllers/grant.controller.js | 1 + app/controllers/sendgrid.controller.js | 2 ++ app/controllers/stripe.controller.js | 22 ++++++++++++++++------ models/Organization.js | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/controllers/grant.controller.js b/app/controllers/grant.controller.js index 5d5b0e4..2cb9c06 100644 --- a/app/controllers/grant.controller.js +++ b/app/controllers/grant.controller.js @@ -100,6 +100,7 @@ exports.createGrant = async (req, res, next) => { grant, organizations, total_amount: actual_total, + transaction_fees: charge.transaction_fees, receiver: donors[0] }); diff --git a/app/controllers/sendgrid.controller.js b/app/controllers/sendgrid.controller.js index c36dd0b..a61ecdb 100644 --- a/app/controllers/sendgrid.controller.js +++ b/app/controllers/sendgrid.controller.js @@ -11,6 +11,7 @@ exports.paymentReceipt = async ({ grant, organizations, total_amount, + transaction_fees, receiver }) => { try { @@ -40,6 +41,7 @@ exports.paymentReceipt = async ({ bundle_id: grant.id, charities_list: charitiesHtml, total_amount, + transaction_fees, date: currDate, subject: 'Your Charify Bundle Payment - Charify' } diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js index 97fae1d..cd36dd6 100644 --- a/app/controllers/stripe.controller.js +++ b/app/controllers/stripe.controller.js @@ -34,10 +34,17 @@ exports.grantCharge = async ({ }); let transfers = await organizations.map(async (org, index) => { - let selectedOrgAmount = org.amount; + const selectedOrgAmount = org.amount; // this determines how much Charify takes from each donation - const applicationStripeFee = selectedOrgAmount * 0.05; + const stripeFee = + Math.round((selectedOrgAmount * 0.029 + 0.3) * 1e2) / 1e2; + org.amount = org.amount - stripeFee; + const applicationFee = + Math.round(selectedOrgAmount * 0.025 * 1e2) / 1e2; + + const applicationAndStripeFee = applicationFee + stripeFee; + console.log(applicationAndStripeFee); let finalAmountToOrg = 0; let currOrg = await sequelize.query( @@ -53,15 +60,15 @@ exports.grantCharge = async ({ if (currOrg[0].charify_credit) { let updated_amt = currOrg[0].charify_credit; - if (currOrg[0].charify_credit < applicationStripeFee) { + if (currOrg[0].charify_credit < applicationAndStripeFee) { updated_amt = 0; // use up credit and take remaining as middle man finalAmountToOrg = selectedOrgAmount - - applicationStripeFee + + applicationAndStripeFee + currOrg[0].charify_credit; } else { - updated_amt = currOrg[0].charify_credit - applicationStripeFee; + updated_amt = currOrg[0].charify_credit - applicationAndStripeFee; // take nothing as the middle man finalAmountToOrg = selectedOrgAmount; } @@ -79,7 +86,7 @@ exports.grantCharge = async ({ } } ); - } else finalAmountToOrg = selectedOrgAmount - applicationStripeFee; + } else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee; // now scale to match stripe standards let t = await stripe.transfers.create({ @@ -92,6 +99,9 @@ exports.grantCharge = async ({ return t; }); + // to show the transaction fees in email to giver + charge.transaction_fees = Math.round((amount * 0.029 + 0.3) * 1e2) / 1e2; + resolve(charge); } catch (error) { reject(error); diff --git a/models/Organization.js b/models/Organization.js index 0f5425e..92a06b9 100644 --- a/models/Organization.js +++ b/models/Organization.js @@ -121,7 +121,7 @@ module.exports = (sequelize, DataTypes) => { }, charify_credit: { - type: DataTypes.INTEGER, + type: DataTypes.DOUBLE, defaultValue: 0 } },