From c660b389a88cb2a7ab8aacdbfbb760c165f1f470 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Fri, 29 Nov 2019 21:11:31 +0100 Subject: [PATCH] fix(donate): handle missing stripe keys (#37847) * fix: gracefully handle missing stripe keys * fix: remove state updates from render (donate) --- client/gatsby-node.js | 13 +++++++++++++ .../components/DonateFormChildViewForHOC.js | 2 +- client/src/pages/donate.js | 11 ++++++----- config/env.js | 10 ++++++++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/client/gatsby-node.js b/client/gatsby-node.js index 7f9debc834..9ae1e65506 100644 --- a/client/gatsby-node.js +++ b/client/gatsby-node.js @@ -51,6 +51,19 @@ exports.createPages = function createPages({ graphql, actions, reporter }) { ); } } + + if (!env.stripePublicKey || !env.servicebotId) { + if (process.env.FREECODECAMP_NODE_ENV === 'production') { + throw new Error( + 'Stripe public key and Servicebot id are required to start the client!' + ); + } else { + reporter.info( + 'Stripe public key or Servicebot id missing or invalid. Required for' + + ' donations.' + ); + } + } const { createPage } = actions; return new Promise((resolve, reject) => { diff --git a/client/src/components/Donation/components/DonateFormChildViewForHOC.js b/client/src/components/Donation/components/DonateFormChildViewForHOC.js index 3d95dd2d7d..9be72873f8 100644 --- a/client/src/components/Donation/components/DonateFormChildViewForHOC.js +++ b/client/src/components/Donation/components/DonateFormChildViewForHOC.js @@ -60,6 +60,7 @@ class DonateFormChildViewForHOC extends Component { this.handleSubmit = this.handleSubmit.bind(this); this.postDonation = this.postDonation.bind(this); this.resetDonation = this.resetDonation.bind(this); + this.hideAmountOptions(false); } getUserEmail() { @@ -226,7 +227,6 @@ class DonateFormChildViewForHOC extends Component { reset: this.resetDonation }); } - this.hideAmountOptions(false); return this.renderDonateForm(); } } diff --git a/client/src/pages/donate.js b/client/src/pages/donate.js index c131a0c1d5..109e25451c 100644 --- a/client/src/pages/donate.js +++ b/client/src/pages/donate.js @@ -61,11 +61,12 @@ export class DonatePage extends Component { handleStripeLoad() { // Create Stripe instance once Stripe.js loads - console.info('stripe has loaded'); - this.setState(state => ({ - ...state, - stripe: window.Stripe(stripePublicKey) - })); + if (stripePublicKey) { + this.setState(state => ({ + ...state, + stripe: window.Stripe(stripePublicKey) + })); + } } enableDonationSettingsPage(enableSettings = true) { diff --git a/config/env.js b/config/env.js index 4d51068bf6..d98f22fd69 100644 --- a/config/env.js +++ b/config/env.js @@ -30,8 +30,14 @@ const locations = { module.exports = Object.assign(locations, { locale, - stripePublicKey, - servicebotId, + stripePublicKey: + !stripePublicKey || stripePublicKey === 'pk_from_stripe_dashboard' + ? null + : stripePublicKey, + servicebotId: + !servicebotId || servicebotId === 'servicebot_id_from_servicebot_dashboard' + ? null + : servicebotId, algoliaAppId: !algoliaAppId || algoliaAppId === 'Algolia app id from dashboard' ? null