fixing charges input into db

This commit is contained in:
Arjun Patel
2019-03-26 02:05:25 -07:00
parent 2c26c7dd2c
commit e97239af79
3 changed files with 124 additions and 123 deletions
+12 -1
View File
@@ -6,6 +6,7 @@ const sendgrid = require("./sendgrid.controller");
const { const {
Grant, Grant,
Charge,
Cause, Cause,
Region, Region,
Organization, Organization,
@@ -62,7 +63,7 @@ exports.createGrant = async (req, res, next) => {
}); });
// one time charge // one time charge
await stripe.grantCharge({ const charge = await stripe.grantCharge({
grant, grant,
stripeToken, stripeToken,
stripeToken, stripeToken,
@@ -72,6 +73,16 @@ exports.createGrant = async (req, res, next) => {
user user
}); });
await Charge.create(
{
id: charge.id,
amount,
description: "One time payment for grant",
grant_id: grant.id
},
{ transaction }
);
// send email // send email
await sendgrid.paymentReceipt({ await sendgrid.paymentReceipt({
organizations, organizations,
+100 -110
View File
@@ -1,151 +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 } });
var result; const stripeAmount = amount * 100;
const stripeAmount = amount * 100; // charge if yes or no monthly
let charge = await stripe.charges.create({
amount: stripeAmount,
currency: "usd",
source: "tok_visa",
description: `Charity Bundle Payment`,
statement_descriptor: `charity bundle ${grant.id}`,
receipt_email: "[email protected]"
});
// charge if yes or no monthly let transfers = await organizations.map(async org => {
let charge = await stripe.charges.create({ let stripeOrgAmount = Math.round(org.amount * 100);
amount: stripeAmount, const applicationStripeFee = stripeOrgAmount * 0.05;
currency: 'usd',
source: 'tok_visa',
description: `Charity Bundle Payment`,
statement_descriptor: `charity bundle ${grant.id}`,
receipt_email: '[email protected]'
});
let transfers = await organizations.map(async org => { let t = await stripe.transfers.create({
let stripeOrgAmount = Math.round(org.amount * 100); amount: stripeOrgAmount - applicationStripeFee,
const applicationStripeFee = stripeOrgAmount * 0.05; currency: "usd",
source_transaction: charge.id,
destination: "acct_1EAxzUIVW1uo07uH"
});
return t;
});
let t = await stripe.transfers.create({ return charge;
amount: stripeOrgAmount - applicationStripeFee, } catch (error) {
currency: 'usd', throw error;
source_transaction: charge.id, }
destination: 'acct_1EAxzUIVW1uo07uH'
});
return t;
});
await Charge.create({
id: charge.id,
amount,
description: 'One time payment for grant'
});
result = charge;
return null;
} catch (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/banking");
} catch (error) { } catch (error) {
next(error); next(error);
} }
}; };
+12 -12
View File
@@ -1,21 +1,21 @@
const express = require('express'), const express = require("express"),
router = express.Router(), router = express.Router(),
checkAuth = require('../middleware/check-auth'), checkAuth = require("../middleware/check-auth"),
roles = require('../helpers/roles'); roles = require("../helpers/roles");
const { organization, cause, region, stripe } = require('../controllers'); const { organization, cause, region, stripe } = require("../controllers");
// Charity signup route // Charity signup route
router.post('/', organization.createOrganization); router.post("/", organization.createOrganization);
// GET Activate org with stripe // GET Activate org's stripe connected account
router.get('/stripe/connect', stripe.activateStripeAccount); router.get("/stripe/connect", stripe.activateStripeAccount);
// GET express ui account link // GET stripe express ui account link
router.get( router.get(
'/stripe/link', "/stripe/link",
checkAuth(roles.ORGANIZATION), checkAuth(roles.ORGANIZATION),
stripe.getExpressUILink stripe.getExpressUILink
); );
// // Add cause // // Add cause