From c03b71ebd3dbdcef1a99967ef47f0aad01e7822f Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Thu, 21 Mar 2019 19:47:12 -0700 Subject: [PATCH] promise request attempt --- .sequelizerc | 2 +- app/controllers/stripe.controller.js | 62 +++++++++++++++++++++++++++- app/routes/organization.route.js | 5 ++- models/Organization.js | 4 ++ package.json | 1 + server.js | 2 - 6 files changed, 71 insertions(+), 5 deletions(-) diff --git a/.sequelizerc b/.sequelizerc index 445766c..b261aee 100644 --- a/.sequelizerc +++ b/.sequelizerc @@ -1,7 +1,7 @@ const path = require('path'); module.exports = { - 'config': path.resolve('./app/config', 'migrations.config.js'), + 'config': path.resolve('./app/config', 'db.config.js'), 'models-path': path.resolve('./', 'models'), 'seeders-path': path.resolve('./', 'seeders'), 'migrations-path': path.resolve('./', 'migrations') diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js index e73ee4f..4e85324 100644 --- a/app/controllers/stripe.controller.js +++ b/app/controllers/stripe.controller.js @@ -1,5 +1,6 @@ const db = require('../../models'), - errorMaker = require('../helpers/error.maker'); + errorMaker = require('../helpers/error.maker'), + https = require('https'); const stripe = require('stripe')(process.env.STRIPE_KEY); @@ -86,3 +87,62 @@ exports.deleteGrant = async (req, res, next) => { 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 +}; + +// verify the charity after they filled out basic info for express ui +exports.activateStripeAccount = async (req, res, next) => { + const { code } = req.query; + console.log(req.query); + + try { + var dataString = `client_secret=${ + process.env.STRIPE_KEY + }&code=${code}&grant_type=authorization_code`; + + var options = { + host: 'connect.stripe.com', + path: '/oauth/token', + method: 'POST', + body: { + client_secret: process.env.STRIPE_KEY, + code, + grant_type: 'authorization_code' + } + }; + + // Set up the request + const postRequest = new Promise((resolve, reject) => { + https + .request(options, function(res) { + res.setEncoding('utf8'); + let body = ''; + res.on('data', data => { + body += data; + }); + res.on('end', () => { + body = JSON.parse(body); + }); + + resolve(body); + }) + .on('error', err => { + console.log('Error: ' + err.message); + reject(err); + }); + }); + + // store for current user, get user_id from the state param maybe? + const stripeAccRes = await Promise.resolve(postRequest); + console.log(stripeAccRes); + + 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 f470b16..235b2ea 100644 --- a/app/routes/organization.route.js +++ b/app/routes/organization.route.js @@ -3,11 +3,14 @@ const express = require('express'), checkAuth = require('../middleware/check-auth'), roles = require('../helpers/roles'); -const { organization, cause, region } = require('../controllers'); +const { organization, cause, region, stripe } = require('../controllers'); // Charity signup route router.post('/', organization.createOrganization); +// Activate org with stripe +router.get('/stripe/connect', stripe.activateStripeAccount); + // // Add cause // router.post('/cause', checkAuth(roles.ORGANIZATION), cause.createCause); diff --git a/models/Organization.js b/models/Organization.js index 06baada..6597d6e 100644 --- a/models/Organization.js +++ b/models/Organization.js @@ -18,6 +18,10 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.INTEGER, defaultValue: 0 }, + stripe_account_id: { + type: DataTypes.UUID, + defaultValue: null + }, primary_contact_email: { type: DataTypes.STRING, diff --git a/package.json b/package.json index 4690f4d..e0fb696 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "jsonwebtoken": "^8.4.0", "morgan": "^1.9.1", "mysql2": "^1.6.4", + "request": "^2.88.0", "sequelize": "^4.42.0", "sequelize-cli": "^5.4.0", "stripe": "^6.22.0", diff --git a/server.js b/server.js index d643221..1bf5698 100644 --- a/server.js +++ b/server.js @@ -6,8 +6,6 @@ const express = require('express'), db = require('./models'), morgan = require('morgan'); -console.log(db); - app.use(morgan('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true }));