From e97239af7933630bec4861f823cf37291cfd833d Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Tue, 26 Mar 2019 02:05:25 -0700 Subject: [PATCH] fixing charges input into db --- app/controllers/grant.controller.js | 13 +- app/controllers/stripe.controller.js | 210 +++++++++++++-------------- app/routes/organization.route.js | 24 +-- 3 files changed, 124 insertions(+), 123 deletions(-) diff --git a/app/controllers/grant.controller.js b/app/controllers/grant.controller.js index 9f5d11d..7576237 100644 --- a/app/controllers/grant.controller.js +++ b/app/controllers/grant.controller.js @@ -6,6 +6,7 @@ const sendgrid = require("./sendgrid.controller"); const { Grant, + Charge, Cause, Region, Organization, @@ -62,7 +63,7 @@ exports.createGrant = async (req, res, next) => { }); // one time charge - await stripe.grantCharge({ + const charge = await stripe.grantCharge({ grant, stripeToken, stripeToken, @@ -72,6 +73,16 @@ exports.createGrant = async (req, res, next) => { user }); + await Charge.create( + { + id: charge.id, + amount, + description: "One time payment for grant", + grant_id: grant.id + }, + { transaction } + ); + // send email await sendgrid.paymentReceipt({ organizations, diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js index 0aa45a0..4b56693 100644 --- a/app/controllers/stripe.controller.js +++ b/app/controllers/stripe.controller.js @@ -1,151 +1,141 @@ -const db = require('../../models'), - errorMaker = require('../helpers/error.maker'), - requestPromise = require('request-promise'); +const db = require("../../models"), + errorMaker = require("../helpers/error.maker"), + 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; // Subscribe user to plan or one time charge exports.grantCharge = async ({ - grant, - stripeToken, - organizations, - monthly, - amount, - user + grant, + stripeToken, + organizations, + monthly, + amount, + user }) => { - const grant_id = await grant.id; + const grant_id = await grant.id; - try { - const donors = await Donor.findAll({ where: { id: user.id } }); + try { + 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: "arjun@ucharify.com" + }); - // 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: 'arjun@ucharify.com' - }); + let transfers = await organizations.map(async org => { + let stripeOrgAmount = Math.round(org.amount * 100); + const applicationStripeFee = stripeOrgAmount * 0.05; - let transfers = await organizations.map(async org => { - let stripeOrgAmount = Math.round(org.amount * 100); - const applicationStripeFee = stripeOrgAmount * 0.05; + let t = await stripe.transfers.create({ + amount: stripeOrgAmount - applicationStripeFee, + currency: "usd", + source_transaction: charge.id, + destination: "acct_1EAxzUIVW1uo07uH" + }); + return t; + }); - let t = await stripe.transfers.create({ - amount: stripeOrgAmount - applicationStripeFee, - currency: 'usd', - 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; - } + return charge; + } catch (error) { + throw error; + } }; // Delete grant's stripe plan under subscription exports.deleteGrant = async (req, res, next) => { - const { grant_id } = req.body; + const { grant_id } = req.body; - try { - const paymentPlans = await PaymentPlan.findAll({ where: { grant_id } }); + try { + const paymentPlans = await PaymentPlan.findAll({ where: { grant_id } }); - // check if there are plans for that grant - if (!paymentPlans.length) { - return res.status(400).json({ - message: 'Not a valid grant' - }); - } else { - const plan = paymentPlans[0]; + // check if there are plans for that grant + if (!paymentPlans.length) { + return res.status(400).json({ + message: "Not a valid grant" + }); + } else { + const plan = paymentPlans[0]; - //delete the plan/grant under the subscription for the user - await stripe.subscriptionItems.del(plan.sub_item_id); - //delete the plan from the main product 'Grants' - await stripe.plans.del(plan.plan_id); - } + //delete the plan/grant under the subscription for the user + await stripe.subscriptionItems.del(plan.sub_item_id); + //delete the plan from the main product 'Grants' + await stripe.plans.del(plan.plan_id); + } - next(); - } catch (error) { - next(error); - } + next(); + } catch (error) { + next(error); + } }; // allows the org to see their stripe account with their balance and payout to their bank exports.getExpressUILink = async (req, res, next) => { - // get connected stripe account id from db - // createLoginLink with stripe function - // return to frontend - const org_id = req.user.id; + // get connected stripe account id from db + // createLoginLink with stripe function + // return to frontend + const org_id = req.user.id; - try { - const orgs = await db.sequelize.query( - `SELECT stripe_account_id + try { + const orgs = await db.sequelize.query( + `SELECT stripe_account_id FROM organizations WHERE id = :org_id LIMIT 1`, - { replacements: { org_id }, type: db.sequelize.QueryTypes.SELECT } - ); + { replacements: { org_id }, type: db.sequelize.QueryTypes.SELECT } + ); - const stripeAccessRes = await stripe.accounts.createLoginLink( - orgs[0].stripe_account_id - ); + const stripeAccessRes = await stripe.accounts.createLoginLink( + orgs[0].stripe_account_id + ); - return res.status(200).json({ url: stripeAccessRes.url }); - } catch (error) { - next(error); - } + return res.status(200).json({ url: stripeAccessRes.url }); + } catch (error) { + next(error); + } }; // verify the charity after they filled out basic info for express ui exports.activateStripeAccount = async (req, res, next) => { - const { code, state } = req.query; - const org_id = state; + const { code, state } = req.query; + const org_id = state; - try { - var dataString = `client_secret=${ - process.env.STRIPE_KEY - }&code=${code}&grant_type=authorization_code`; + try { + var dataString = `client_secret=${ + process.env.STRIPE_KEY + }&code=${code}&grant_type=authorization_code`; - var options = { - uri: 'https://connect.stripe.com/oauth/token', - method: 'POST', - body: { - client_secret: process.env.STRIPE_KEY, - code, - grant_type: 'authorization_code' - }, - json: true - }; + var options = { + uri: "https://connect.stripe.com/oauth/token", + method: "POST", + body: { + client_secret: process.env.STRIPE_KEY, + code, + grant_type: "authorization_code" + }, + json: true + }; - // store for current user, get user_id from the state param maybe? - const stripeAccRes = await requestPromise(options); - const { stripe_user_id } = stripeAccRes; + // store for current user, get user_id from the state param maybe? + const stripeAccRes = await requestPromise(options); + const { stripe_user_id } = stripeAccRes; - const updateRes = await db.sequelize.query( - 'UPDATE organizations SET stripe_account_id = :stripe_user_id WHERE id = :org_id', - { replacements: { stripe_user_id, org_id } } - ); + const updateRes = await db.sequelize.query( + "UPDATE organizations SET stripe_account_id = :stripe_user_id WHERE id = :org_id", + { replacements: { stripe_user_id, org_id } } + ); - return res.status(302).redirect('http://localhost:8081/org/main/banking'); - } catch (error) { - next(error); - } + return res.status(302).redirect("http://localhost:8081/org/main/banking"); + } catch (error) { + next(error); + } }; diff --git a/app/routes/organization.route.js b/app/routes/organization.route.js index 26556b5..18fe4b5 100644 --- a/app/routes/organization.route.js +++ b/app/routes/organization.route.js @@ -1,21 +1,21 @@ -const express = require('express'), - router = express.Router(), - checkAuth = require('../middleware/check-auth'), - roles = require('../helpers/roles'); +const express = require("express"), + router = express.Router(), + checkAuth = require("../middleware/check-auth"), + roles = require("../helpers/roles"); -const { organization, cause, region, stripe } = require('../controllers'); +const { organization, cause, region, stripe } = require("../controllers"); // Charity signup route -router.post('/', organization.createOrganization); +router.post("/", organization.createOrganization); -// GET Activate org with stripe -router.get('/stripe/connect', stripe.activateStripeAccount); +// GET Activate org's stripe connected account +router.get("/stripe/connect", stripe.activateStripeAccount); -// GET express ui account link +// GET stripe express ui account link router.get( - '/stripe/link', - checkAuth(roles.ORGANIZATION), - stripe.getExpressUILink + "/stripe/link", + checkAuth(roles.ORGANIZATION), + stripe.getExpressUILink ); // // Add cause