fix: removed unnecessary strip call from boot sequence (#17526)

fix: Updated boot/donate.js to connect to stripe by default in production mode

fix: removed whites spaces from donate.js and added extra check for api keys

fix: added a name to boot/donate.js export function
This commit is contained in:
Charles Moss
2018-06-18 08:47:10 -06:00
committed by Stuart Taylor
parent 412980c403
commit ee64ab97c1

View File

@@ -1,10 +1,13 @@
import Stripe from 'stripe'; import Stripe from 'stripe';
import keys from '../../config/secrets'; import keys from '../../config/secrets';
const stripe = Stripe(keys.stripe.secret); export default function donateBoot(app, done) {
const subscriptionPlans = [500, 1000, 3500, 5000, 25000].reduce( let stripe = false;
const { User } = app.models;
const api = app.loopback.Router();
const donateRouter = app.loopback.Router();
const subscriptionPlans = [500, 1000, 3500, 5000, 25000].reduce(
(accu, current) => ({ (accu, current) => ({
...accu, ...accu,
[current]: { [current]: {
@@ -18,22 +21,15 @@ const subscriptionPlans = [500, 1000, 3500, 5000, 25000].reduce(
currency: 'usd', currency: 'usd',
id: `monthly-donation-${current}` id: `monthly-donation-${current}`
} }
}), }), {}
{} );
);
function createStripePlan(plan) { function connectToStripe() {
stripe.plans.create(plan, function(err) { return new Promise(function(resolve) {
if (err) { // connect to stripe API
console.log(err); stripe = Stripe(keys.stripe.secret);
throw err; // parse stripe plans
} stripe.plans.list({}, function(err, plans) {
console.log(`${plan.id} created`);
return;
});
}
stripe.plans.list({}, function(err, plans) {
if (err) { if (err) {
throw err; throw err;
} }
@@ -47,12 +43,21 @@ stripe.plans.list({}, function(err, plans) {
createStripePlan(subscriptionPlans[key]); createStripePlan(subscriptionPlans[key]);
} }
}); });
}); });
resolve();
});
}
export default function donateBoot(app) { function createStripePlan(plan) {
const { User } = app.models; stripe.plans.create(plan, function(err) {
const api = app.loopback.Router(); if (err) {
const donateRouter = app.loopback.Router(); console.log(err);
throw err;
}
console.log(`${plan.id} created`);
return;
});
}
function createStripeDonation(req, res) { function createStripeDonation(req, res) {
const { user, body } = req; const { user, body } = req;
@@ -113,9 +118,22 @@ export default function donateBoot(app) {
}); });
} }
api.post('/charge-stripe', createStripeDonation); const pubKey = keys.stripe.public;
const secKey = keys.stripe.secret;
const secretInvalid = !secKey || secKey === 'sk_from_stipe_dashboard';
const publicInvalid = !pubKey || pubKey === 'pk_from_stipe_dashboard';
if (secretInvalid || publicInvalid) {
if (process.env.NODE_ENV === 'production') {
throw new Error('Stripe API keys are required to boot the server!');
}
console.info('No Stripe API keys were found, moving on...');
done();
} else {
api.post('/charge-stripe', createStripeDonation);
donateRouter.use('/donate', api); donateRouter.use('/donate', api);
app.use(donateRouter); app.use(donateRouter);
app.use('/external', donateRouter); app.use('/external', donateRouter);
connectToStripe().then(done);
}
} }