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
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,
+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;">${
charity.name
}</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;">$${
charity.amount
}</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;">$${charity.amountWithStripeFees.toFixed(
2
)}</td>
</tr>`;
});
@@ -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'
}
+59 -54
View File
@@ -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) {
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -14,7 +14,7 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false
},
amount: {
type: DataTypes.INTEGER,
type: DataTypes.DOUBLE,
allowNull: false
},
monthly: {
+1 -1
View File
@@ -27,7 +27,7 @@ module.exports = (sequelize, DataTypes) => {
}
},
amount: {
type: DataTypes.INTEGER,
type: DataTypes.DOUBLE,
allowNull: false
}
},