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,9 +1,12 @@
|
|||||||
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) {
|
||||||
|
|
||||||
|
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(
|
const subscriptionPlans = [500, 1000, 3500, 5000, 25000].reduce(
|
||||||
(accu, current) => ({
|
(accu, current) => ({
|
||||||
...accu,
|
...accu,
|
||||||
@ -18,21 +21,14 @@ 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
|
||||||
}
|
|
||||||
console.log(`${plan.id} created`);
|
|
||||||
return;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
stripe.plans.list({}, function(err, plans) {
|
stripe.plans.list({}, function(err, plans) {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err;
|
throw err;
|
||||||
@ -48,11 +44,20 @@ stripe.plans.list({}, function(err, plans) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user