properly updating the db and also promises call
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
const db = require('../../models'),
|
const db = require('../../models'),
|
||||||
errorMaker = require('../helpers/error.maker'),
|
errorMaker = require('../helpers/error.maker'),
|
||||||
https = require('https');
|
requestPromise = require('request-promise');
|
||||||
|
|
||||||
const stripe = require('stripe')(process.env.STRIPE_KEY);
|
const stripe = require('stripe')(process.env.STRIPE_KEY);
|
||||||
|
|
||||||
const { Donor, 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 ({
|
||||||
@@ -97,8 +97,8 @@ exports.getExpressUILink = async (req, res, next) => {
|
|||||||
|
|
||||||
// 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 } = req.query;
|
const { code, state } = req.query;
|
||||||
console.log(req.query);
|
const org_id = state;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var dataString = `client_secret=${
|
var dataString = `client_secret=${
|
||||||
@@ -106,40 +106,24 @@ exports.activateStripeAccount = async (req, res, next) => {
|
|||||||
}&code=${code}&grant_type=authorization_code`;
|
}&code=${code}&grant_type=authorization_code`;
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
host: 'connect.stripe.com',
|
uri: 'https://connect.stripe.com/oauth/token',
|
||||||
path: '/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
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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?
|
// store for current user, get user_id from the state param maybe?
|
||||||
const stripeAccRes = await Promise.resolve(postRequest);
|
const stripeAccRes = await requestPromise(options);
|
||||||
console.log(stripeAccRes);
|
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 } }
|
||||||
|
);
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
Generated
+24
@@ -1443,6 +1443,25 @@
|
|||||||
"uuid": "^3.3.2"
|
"uuid": "^3.3.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"request-promise": {
|
||||||
|
"version": "4.2.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/request-promise/-/request-promise-4.2.4.tgz",
|
||||||
|
"integrity": "sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==",
|
||||||
|
"requires": {
|
||||||
|
"bluebird": "^3.5.0",
|
||||||
|
"request-promise-core": "1.1.2",
|
||||||
|
"stealthy-require": "^1.1.1",
|
||||||
|
"tough-cookie": "^2.3.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"request-promise-core": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/request-promise-core/-/request-promise-core-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-UHYyq1MO8GsefGEt7EprS8UrXsm1TxEvFUX1IMTuSLU2Rh7fTIdFtl8xD7JiEYiWU2dl+NYAjCTksTehQUxPag==",
|
||||||
|
"requires": {
|
||||||
|
"lodash": "^4.17.11"
|
||||||
|
}
|
||||||
|
},
|
||||||
"require-directory": {
|
"require-directory": {
|
||||||
"version": "2.1.1",
|
"version": "2.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
|
||||||
@@ -1639,6 +1658,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz",
|
||||||
"integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew=="
|
"integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew=="
|
||||||
},
|
},
|
||||||
|
"stealthy-require": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz",
|
||||||
|
"integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks="
|
||||||
|
},
|
||||||
"string-width": {
|
"string-width": {
|
||||||
"version": "2.1.1",
|
"version": "2.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
"morgan": "^1.9.1",
|
"morgan": "^1.9.1",
|
||||||
"mysql2": "^1.6.4",
|
"mysql2": "^1.6.4",
|
||||||
"request": "^2.88.0",
|
"request": "^2.88.0",
|
||||||
|
"request-promise": "^4.2.4",
|
||||||
"sequelize": "^4.42.0",
|
"sequelize": "^4.42.0",
|
||||||
"sequelize-cli": "^5.4.0",
|
"sequelize-cli": "^5.4.0",
|
||||||
"stripe": "^6.22.0",
|
"stripe": "^6.22.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user