#ben-obringer_best-practices

1 messages ยท Page 1 of 1 (latest)

uneven tigerBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1216752007969243238

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

fair elbow
#
      case 'invoice.payment_failed':
      case 'invoice.paid': {
        const parsedIds = parseStripeIds(event.data.object);
        if (!parsedIds) {
          respondWithWebhookError(res, 400, 'customer or subscription missing');
          return;
        }
        const { customerId, subscriptionId } = parsedIds;

        respondWithWebhookSuccess(res);

        const user = await userRepo.findByStripeCustomerId(customerId);
        if (!user) {
          /**
           * We should have a user for this customer, but it could be the case
           * that this event was fired before the one-time checkout.session.completed
           * event in which case we can ignore it because the user will be
           * provisioned when that event is fired and the details will be the
           * same as this associated event.
           */
          logPostResponseError('user not found - ignoring');
          return;
        }

        const subscriptionResult = await stripeApi.retrieveSubscription(
          subscriptionId,
        );
        if (subscriptionResult.err) {
          logPostResponseError('subscription retrieval failed');
          return;
        }

        try {
          user.updateSubscriptionStatus(subscriptionResult.val.status);
          await userRepo.save(user);
        } catch (e) {
          logPostResponseError(
            `updating subscription status failed - ${getErrorMessage(e)}`,
          );
        }

        break;
      }
merry helmBOT
worldly bridge
#

Hi ๐Ÿ‘‹ if only the Subscription related Events cover your business needs, then you should be able to drop to just those. I don't have context about what you're using the Invoice related Events for, so I'm not sure offhand if the Subscription related Events would also have those details.

fair elbow
#

thanks for the quick response! ๐Ÿ™‚ the only reason i use them is to make sure the subscription status is up to date. i have stripe send emails for failed payments and paid invoices

worldly bridge
#

Gotcha, doesn't sound like you need the Invoice related Events then as they don't contain the Subscription status. But I'd recommend testing the revised flow in testmode first to ensure everything continues to behave as you're expecting.

fair elbow
#

will do. thank you and have a good day ๐Ÿ™‚