taking in proper amounts and distribution...call stripe methods instead of middleware

This commit is contained in:
Arjun Patel
2019-03-06 14:52:40 -08:00
parent 0a1ce8ee5f
commit e9dd0b3b25
3 changed files with 24 additions and 22 deletions
+6 -8
View File
@@ -1,14 +1,17 @@
const db = require('../../config/db.config.js'), const db = require('../../config/db.config.js'),
errorMaker = require('../../helpers/error.maker'); errorMaker = require('../../helpers/error.maker');
const stripe = require('./stripe.controller');
const { Grant, Cause, Region, Organization, sequelize } = db; const { Grant, Cause, Region, Organization, sequelize } = db;
// Create a grant for certain donor // Create a grant for certain donor
exports.create = (req, res, next) => { exports.create = (req, res, next) => {
const donor_id = req.user.id; const donor_id = req.user.id;
const { name, amounts, monthly, causes, regions, organizations } = req.body; const { name, monthly, causes, regions, organizations } = req.body;
const total_amount = 100; var total_amount = organizations.map((org) => org.amount).reduce((partial_sum, a) => partial_sum + a);
var total_amount = total_amount * 100;
return sequelize return sequelize
.transaction(function(t) { .transaction(function(t) {
@@ -39,12 +42,7 @@ exports.create = (req, res, next) => {
}) })
.then(function(grants) { .then(function(grants) {
// transaction committed // transaction committed
// res.status(201).json({ stripe.grantCharge({grant: grants.dataValues, total_amount}, req, res);
// grant,
// message: 'Grant Created'
// });
next(grants.dataValues);
return null; return null;
}) })
.catch(function(error) { .catch(function(error) {
+17 -12
View File
@@ -6,12 +6,11 @@ const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj');
const { Donor, Charge, PaymentPlan } = db; const { Donor, Charge, PaymentPlan } = db;
// Subscribe user to plan or one time charge // Subscribe user to plan or one time charge
exports.grantCharge = async (grant, req, res, next) => { exports.grantCharge = async (data, req, res) => {
const { stripeToken, monthly } = req.body; const { stripeToken, organizations, monthly } = req.body;
const { total_amount, grant } = await data; // passed from grant controller
const grant_id = await grant.id; const grant_id = await grant.id;
const amount = 100 * 100; //stripe standards
const user = req.user; const user = req.user;
const product_id = 'prod_ER3jOog6QMX1GP', const product_id = 'prod_ER3jOog6QMX1GP',
default_plan_id = 'default_plan'; // main Grants product default_plan_id = 'default_plan'; // main Grants product
@@ -38,23 +37,29 @@ exports.grantCharge = async (grant, req, res, next) => {
// charge if not monthly, plan if it is // charge if not monthly, plan if it is
if (!monthly) { if (!monthly) {
let charge = await stripe.charges.create({ let charge = await stripe.charges.create({
amount, amount: total_amount,
currency: 'usd', currency: 'usd',
source: 'tok_visa', source: 'tok_visa',
description: 'One time payment for grant', description: 'One time payment for grant',
statement_descriptor: 'one-time-grant' statement_descriptor: 'one-time-grant'
}); });
await stripe.transfers.create({ let transfers = await organizations.map(async (org) => {
amount: amount - 1000, org.amount = org.amount * 100;
currency: 'usd', const applicationStripeFee = org.amount * 0.05;
source_transaction: charge.id,
destination: 'acct_1EAxzUIVW1uo07uH' let t = await stripe.transfers.create({
amount: org.amount - applicationStripeFee,
currency: 'usd',
source_transaction: charge.id,
destination: 'acct_1EAxzUIVW1uo07uH'
});
return t;
}); });
await Charge.create({ await Charge.create({
id: charge.id, id: charge.id,
amount, amount: total_amount,
description: 'One time payment for grant' description: 'One time payment for grant'
}); });
@@ -113,7 +118,7 @@ exports.grantCharge = async (grant, req, res, next) => {
grant grant
}); });
} catch (error) { } catch (error) {
next(error); throw error;
} }
}; };
+1 -2
View File
@@ -22,8 +22,7 @@ router.get('/grants/', checkAuth(roles.DONOR), controllers.grant.findByDonorId);
router.post( router.post(
'/grants/', '/grants/',
checkAuth(roles.DONOR), checkAuth(roles.DONOR),
controllers.grant.create, controllers.grant.create
controllers.stripe.grantCharge
); );
// DELETE a grant of a donor // DELETE a grant of a donor