allowing charities to add ribbons for

This commit is contained in:
Arjun Patel
2019-04-12 14:11:33 -07:00
parent 3756f88cc7
commit 66df6bb576
3 changed files with 106 additions and 106 deletions
+4 -1
View File
@@ -12,7 +12,9 @@ exports.getStatusUpdatesByOrg = async (req, res, next) => {
const posts = await sequelize.query( const posts = await sequelize.query(
` `
SELECT * FROM posts SELECT * FROM posts
WHERE organization_id = :organization_id`, WHERE organization_id = :organization_id
ORDER BY created_at DESC
LIMIT 6`,
{ {
type: db.Sequelize.QueryTypes.SELECT, type: db.Sequelize.QueryTypes.SELECT,
replacements: { organization_id } replacements: { organization_id }
@@ -70,6 +72,7 @@ exports.addRibbon = async (req, res, next) => {
message: 'Successfully added ribbon to post!' message: 'Successfully added ribbon to post!'
}); });
} catch (error) { } catch (error) {
console.log(error);
next(error); next(error);
} }
}; };
+100 -100
View File
@@ -1,141 +1,141 @@
const db = require("../../models"), const db = require('../../models'),
errorMaker = require("../helpers/error.maker"), errorMaker = require('../helpers/error.maker'),
requestPromise = require("request-promise"); requestPromise = require('request-promise');
const stripe = require("stripe")(process.env.STRIPE_KEY); const stripe = require('stripe')(process.env.STRIPE_KEY);
const { Donor, Organization, Charge, PaymentPlan } = db; const { Donor, Organization, Charge, PaymentPlan } = db;
// Subscribe user to plan or one time charge // Subscribe user to plan or one time charge
exports.grantCharge = async ({ exports.grantCharge = async ({
grant, grant,
stripeToken, stripeToken,
organizations, organizations,
monthly, monthly,
amount, amount,
user user
}) => { }) => {
const grant_id = await grant.id; const grant_id = await grant.id;
try { try {
const donors = await Donor.findAll({ where: { id: user.id } }); const donors = await Donor.findAll({ where: { id: user.id } });
const stripeAmount = amount * 100; const stripeAmount = amount * 100;
// charge if yes or no monthly // charge if yes or no monthly
let charge = await stripe.charges.create({ let charge = await stripe.charges.create({
amount: stripeAmount, amount: stripeAmount,
currency: "usd", currency: 'usd',
source: "tok_visa", source: 'tok_visa',
description: `Charity Bundle Payment`, description: `Charity Bundle Payment`,
statement_descriptor: `charity bundle ${grant.id}`, statement_descriptor: `charity bundle ${grant.id}`,
receipt_email: "arjun@ucharify.com" receipt_email: 'arjun@ucharify.com'
}); });
let transfers = await organizations.map(async org => { let transfers = await organizations.map(async org => {
let stripeOrgAmount = Math.round(org.amount * 100); let stripeOrgAmount = Math.round(org.amount * 100);
const applicationStripeFee = stripeOrgAmount * 0.05; const applicationStripeFee = stripeOrgAmount * 0.05;
let t = await stripe.transfers.create({ let t = await stripe.transfers.create({
amount: stripeOrgAmount - applicationStripeFee, amount: stripeOrgAmount - applicationStripeFee,
currency: "usd", currency: 'usd',
source_transaction: charge.id, source_transaction: charge.id,
destination: "acct_1EAxzUIVW1uo07uH" destination: 'acct_1EAxzUIVW1uo07uH'
}); });
return t; return t;
}); });
return charge; return charge;
} catch (error) { } catch (error) {
throw error; throw error;
} }
}; };
// Delete grant's stripe plan under subscription // Delete grant's stripe plan under subscription
exports.deleteGrant = async (req, res, next) => { exports.deleteGrant = async (req, res, next) => {
const { grant_id } = req.body; const { grant_id } = req.body;
try { try {
const paymentPlans = await PaymentPlan.findAll({ where: { grant_id } }); const paymentPlans = await PaymentPlan.findAll({ where: { grant_id } });
// check if there are plans for that grant // check if there are plans for that grant
if (!paymentPlans.length) { if (!paymentPlans.length) {
return res.status(400).json({ return res.status(400).json({
message: "Not a valid grant" message: 'Not a valid grant'
}); });
} else { } else {
const plan = paymentPlans[0]; const plan = paymentPlans[0];
//delete the plan/grant under the subscription for the user //delete the plan/grant under the subscription for the user
await stripe.subscriptionItems.del(plan.sub_item_id); await stripe.subscriptionItems.del(plan.sub_item_id);
//delete the plan from the main product 'Grants' //delete the plan from the main product 'Grants'
await stripe.plans.del(plan.plan_id); await stripe.plans.del(plan.plan_id);
} }
next(); next();
} catch (error) { } catch (error) {
next(error); next(error);
} }
}; };
// allows the org to see their stripe account with their balance and payout to their bank // allows the org to see their stripe account with their balance and payout to their bank
exports.getExpressUILink = async (req, res, next) => { exports.getExpressUILink = async (req, res, next) => {
// get connected stripe account id from db // get connected stripe account id from db
// createLoginLink with stripe function // createLoginLink with stripe function
// return to frontend // return to frontend
const org_id = req.user.id; const org_id = req.user.id;
try { try {
const orgs = await db.sequelize.query( const orgs = await db.sequelize.query(
`SELECT stripe_account_id `SELECT stripe_account_id
FROM organizations FROM organizations
WHERE id = :org_id WHERE id = :org_id
LIMIT 1`, LIMIT 1`,
{ replacements: { org_id }, type: db.sequelize.QueryTypes.SELECT } { replacements: { org_id }, type: db.sequelize.QueryTypes.SELECT }
); );
const stripeAccessRes = await stripe.accounts.createLoginLink( const stripeAccessRes = await stripe.accounts.createLoginLink(
orgs[0].stripe_account_id orgs[0].stripe_account_id
); );
return res.status(200).json({ url: stripeAccessRes.url }); return res.status(200).json({ url: stripeAccessRes.url });
} catch (error) { } catch (error) {
next(error); next(error);
} }
}; };
// verify the charity after they filled out basic info for express ui // verify the charity after they filled out basic info for express ui
exports.activateStripeAccount = async (req, res, next) => { exports.activateStripeAccount = async (req, res, next) => {
const { code, state } = req.query; const { code, state } = req.query;
const org_id = state; const org_id = state;
try { try {
var dataString = `client_secret=${ var dataString = `client_secret=${
process.env.STRIPE_KEY process.env.STRIPE_KEY
}&code=${code}&grant_type=authorization_code`; }&code=${code}&grant_type=authorization_code`;
var options = { var options = {
uri: "https://connect.stripe.com/oauth/token", uri: 'https://connect.stripe.com/oauth/token',
method: "POST", method: 'POST',
body: { body: {
client_secret: process.env.STRIPE_KEY, client_secret: process.env.STRIPE_KEY,
code, code,
grant_type: "authorization_code" grant_type: 'authorization_code'
}, },
json: true json: true
}; };
// store for current user, get user_id from the state param maybe? // store for current user, get user_id from the state param maybe?
const stripeAccRes = await requestPromise(options); const stripeAccRes = await requestPromise(options);
const { stripe_user_id } = stripeAccRes; const { stripe_user_id } = stripeAccRes;
const updateRes = await db.sequelize.query( const updateRes = await db.sequelize.query(
"UPDATE organizations SET stripe_account_id = :stripe_user_id WHERE id = :org_id", 'UPDATE organizations SET stripe_account_id = :stripe_user_id WHERE id = :org_id',
{ replacements: { stripe_user_id, org_id } } { replacements: { stripe_user_id, org_id } }
); );
return res.status(302).redirect("http://localhost:8081/org/main/banking"); return res.status(302).redirect('http://localhost:8081/org/main/');
} catch (error) { } catch (error) {
next(error); next(error);
} }
}; };
+2 -5
View File
@@ -55,11 +55,8 @@ router.get('/dashboard', checkAuth(roles.DONOR), donor.getDashboardData);
router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor); router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor);
router.put( //TODO: checkAuth so only donors can add ribbons
'/statusupdate/ribbon/:update_id', router.put('/statusupdate/ribbon/:update_id', post.addRibbon);
checkAuth(roles.DONOR),
post.addRibbon
);
// // Retrieve a single Donor by Id // // Retrieve a single Donor by Id
// router.get('/:donor_id', donor.findById); // router.get('/:donor_id', donor.findById);