Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/donate.ts (TypeScript)

Code before:

        try {
          const {
            id: subscription_id,
            latest_invoice: {
              // For older api versions, @ts-ignore is recommended by Stripe.
              // More info: https://github.com/stripe/stripe-node/blob/fe81d1f28064c9b468c7b380ab09f7a93054ba63/README.md?plain=1#L91
              // eslint-disable-next-line @typescript-eslint/ban-ts-comment
              // @ts-ignore stripe-version-2019-10-17
              payment_intent: { client_secret, status }
            }
          } = await stripe.subscriptions.create({
            customer: customerId,
            payment_behavior: 'allow_incomplete',
            items: [{ plan }],
            expand: ['latest_invoice.payment_intent']
          });
          if (status === 'requires_source_action') {
            void reply.code(402);
            return {
              type: 'UserActionRequired',
              message: 'Payment requires user action',
              // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
              client_secret
            } as const;
          } else if (status === 'requires_source') {
            void reply.code(402);
            return {
              type: 'PaymentMethodRequired',
              message: 'Card has been declined'
            } as const;
          }
          subscriptionId = subscription_id;
        } catch (err) {
          throw {
            type: 'SubscriptionCreationFailed',
            message: 'Failed to create stripe subscription'
          };
        }

        // update record in database
        const donation = {
          userId: id,
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: id,
          startDate: {
            date: new Date().toISOString(),

Review comment (suggestion):
```suggestion
          // TODO(Post-MVP) migrate to startDate: new Date()
          startDate: {
            date: new Date().toISOString(),
            when: new Date().toISOString().replace(/.$/, '+00:00')
          }
```
This is nasty, so I think it's worth a comment to remind us to get rid of it.

Code after:
            message: 'Payment requires user action',
            // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
            client_secret
          } as const;
        } else if (status === 'requires_source') {
          void reply.code(402);
          return {
            type: 'PaymentMethodRequired',
            message: 'Card has been declined'
          } as const;
        }

        // update record in database
        const donation = {
          userId: id,
          email,
          amount,
          duration,
          provider: 'stripe',
          subscriptionId,
          customerId: id,
          // TODO(Post-MVP) migrate to startDate: new Date()
          startDate: {
            date: new Date().toISOString(),
            when: new Date().toISOString().replace(/.$/, '+00:00')
          }
        };

        await fastify.prisma.donation.create({
          data: donation
        });

        await fastify.prisma.user.update({
          where: { id },
          data: {
            isDonating: true
          }
        });

        return {
          type: 'success',
          isDonating: true
        } as const;
      } catch (error) {
        fastify.log.error(error);
        void reply.code(500);
        return {
          type: 'danger',
          message: 'Donation failed due to a server error.'
        } as const;
      }