Repository: freeCodeCamp/freeCodeCamp
File: api-server/src/server/boot/donate.js (JavaScript)

Code before:
    }
  }

  async function createStripePaymentIntent(req, res) {
    const { body } = req;
    const { amount, duration, email, name } = body;

    if (!validStripeForm(amount, duration, email)) {
      return res.status(500).send({
        error: 'The donation form had invalid values for this submission.'
      });
    }

    try {
      const stripeCustomer = await stripe.customers.create({
        email,
        name
      });

      const stripeSubscription = await stripe.subscriptions.create({
        customer: stripeCustomer.id,
        items: [
          {
            plan: `${donationSubscriptionConfig.duration[
              duration
            ].toLowerCase()}-donation-${amount}`
          }
        ],
        payment_behavior: 'default_incomplete',
        payment_settings: { save_default_payment_method: 'on_subscription' },
        expand: ['latest_invoice.payment_intent']
      });

      res.status(200).send({
        subscriptionId: stripeSubscription.id,
        clientSecret:
          stripeSubscription.latest_invoice.payment_intent.client_secret
      });
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }

  function addDonation(req, res) {
    const { user, body } = req;
    if (!user || !body) {
      return res
        .status(500)
        .json({ error: 'User must be signed in for this request.' });

Review comment (suggestion):
```suggestion
            ]}-donation-${amount}`
```
Bit of a code smell. It implies to me that this might be doing two things 1) acting as a key and 2) being displayed to humans.

As far as I can see it's just used as a key, which is good, but means it can be lower case everywhere.

Code after:
    }
  }

  async function createStripePaymentIntent(req, res) {
    const { body } = req;
    const { amount, duration, email, name } = body;

    if (!validStripeForm(amount, duration, email)) {
      return res.status(400).send({
        error: 'The donation form had invalid values for this submission.'
      });
    }

    try {
      const stripeCustomer = await stripe.customers.create({
        email,
        name
      });

      const stripeSubscription = await stripe.subscriptions.create({
        customer: stripeCustomer.id,
        items: [
          {
            plan: `${donationSubscriptionConfig.duration[duration]}-donation-${amount}`
          }
        ],
        payment_behavior: 'default_incomplete',
        payment_settings: { save_default_payment_method: 'on_subscription' },
        expand: ['latest_invoice.payment_intent']
      });

      res.status(200).send({
        subscriptionId: stripeSubscription.id,
        clientSecret:
          stripeSubscription.latest_invoice.payment_intent.client_secret
      });
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }

  function addDonation(req, res) {
    const { user, body } = req;
    if (!user || !body) {
      return res
        .status(500)
        .json({ error: 'User must be signed in for this request.' });
    }
    return Promise.resolve(req)