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'),
errorMaker = require('../../helpers/error.maker');
const stripe = require('./stripe.controller');
const { Grant, Cause, Region, Organization, sequelize } = db;
// Create a grant for certain donor
exports.create = (req, res, next) => {
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
.transaction(function(t) {
@@ -39,12 +42,7 @@ exports.create = (req, res, next) => {
})
.then(function(grants) {
// transaction committed
// res.status(201).json({
// grant,
// message: 'Grant Created'
// });
next(grants.dataValues);
stripe.grantCharge({grant: grants.dataValues, total_amount}, req, res);
return null;
})
.catch(function(error) {
+17 -12
View File
@@ -6,12 +6,11 @@ const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj');
const { Donor, Charge, PaymentPlan } = db;
// Subscribe user to plan or one time charge
exports.grantCharge = async (grant, req, res, next) => {
const { stripeToken, monthly } = req.body;
exports.grantCharge = async (data, req, res) => {
const { stripeToken, organizations, monthly } = req.body;
const { total_amount, grant } = await data; // passed from grant controller
const grant_id = await grant.id;
const amount = 100 * 100; //stripe standards
const user = req.user;
const product_id = 'prod_ER3jOog6QMX1GP',
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
if (!monthly) {
let charge = await stripe.charges.create({
amount,
amount: total_amount,
currency: 'usd',
source: 'tok_visa',
description: 'One time payment for grant',
statement_descriptor: 'one-time-grant'
});
let transfers = await organizations.map(async (org) => {
org.amount = org.amount * 100;
const applicationStripeFee = org.amount * 0.05;
await stripe.transfers.create({
amount: amount - 1000,
currency: 'usd',
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({
id: charge.id,
amount,
amount: total_amount,
description: 'One time payment for grant'
});
@@ -113,7 +118,7 @@ exports.grantCharge = async (grant, req, res, next) => {
grant
});
} 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(
'/grants/',
checkAuth(roles.DONOR),
controllers.grant.create,
controllers.stripe.grantCharge
controllers.grant.create
);
// DELETE a grant of a donor