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

Code before:

  async function createStripeDonation(req, res) {
    const { body } = req;
    const { amount, duration, email, subscriptionId } = body;
    try {
      const subscription = await stripe.subscriptions.retrieve(subscriptionId);
      const isSubscriptionActive = subscription.status === 'active';
      const productId = subscription.items.data[0].plan.product;
      const isSubscribedInMinutes = isWithinFiveMinutes(
        subscription.current_period_start
      );
      const isProductIdValid = allStripeProductIdsArray.includes(productId);

      if (isSubscriptionActive && isProductIdValid && isSubscribedInMinutes) {
        const [donatingUser] = await User.findOrCreate(
          { where: { email } },
          { email }
        );
        const donation = {
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: subscription.customer,
          startDate: new Date(Date.now()).toISOString()
        };
        await donatingUser.createDonation(donation);
        return res.status(200).send({ isDonating: true });
      } else {
        throw new Error('Donation failed due to a server error.');
      }
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }

  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({

Review comment (suggestion):
```suggestion
          startDate: new Date().toISOString()
```
Same thing.

Code after:

  async function createStripeDonation(req, res) {
    const { body } = req;
    const { amount, duration, email, subscriptionId } = body;
    try {
      const subscription = await stripe.subscriptions.retrieve(subscriptionId);
      const isSubscriptionActive = subscription.status === 'active';
      const productId = subscription.items.data[0].plan.product;
      const isStartedRecently = inLastFiveMinutes(
        subscription.current_period_start
      );
      const isProductIdValid = allStripeProductIdsArray.includes(productId);

      if (isSubscriptionActive && isProductIdValid && isStartedRecently) {
        const [donatingUser] = await User.findOrCreate(
          { where: { email } },
          { email }
        );
        const donation = {
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: subscription.customer,
          startDate: new Date().toISOString()
        };
        await donatingUser.createDonation(donation);
        return res.status(200).send({ isDonating: true });
      } else {
        throw new Error('Donation failed due to a server error.');
      }
    } catch (err) {
      return res
        .status(500)
        .send({ error: 'Donation failed due to a server error.' });
    }
  }

  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({