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:
committed by
Stuart Taylor
parent
412980c403
commit
ee64ab97c1
@@ -1,10 +1,13 @@
|
||||
import Stripe from 'stripe';
|
||||
|
||||
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]: {
|
||||
@@ -18,22 +21,15 @@ const subscriptionPlans = [500, 1000, 3500, 5000, 25000].reduce(
|
||||
currency: 'usd',
|
||||
id: `monthly-donation-${current}`
|
||||
}
|
||||
}),
|
||||
{}
|
||||
);
|
||||
}), {}
|
||||
);
|
||||
|
||||
function createStripePlan(plan) {
|
||||
stripe.plans.create(plan, function(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
}
|
||||
console.log(`${plan.id} created`);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
stripe.plans.list({}, function(err, plans) {
|
||||
function connectToStripe() {
|
||||
return new Promise(function(resolve) {
|
||||
// connect to stripe API
|
||||
stripe = Stripe(keys.stripe.secret);
|
||||
// parse stripe plans
|
||||
stripe.plans.list({}, function(err, plans) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
@@ -47,12 +43,21 @@ stripe.plans.list({}, function(err, plans) {
|
||||
createStripePlan(subscriptionPlans[key]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
export default function donateBoot(app) {
|
||||
const { User } = app.models;
|
||||
const api = app.loopback.Router();
|
||||
const donateRouter = app.loopback.Router();
|
||||
function createStripePlan(plan) {
|
||||
stripe.plans.create(plan, function(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
throw err;
|
||||
}
|
||||
console.log(`${plan.id} created`);
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
function createStripeDonation(req, res) {
|
||||
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);
|
||||
app.use(donateRouter);
|
||||
app.use('/external', donateRouter);
|
||||
connectToStripe().then(done);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user