From 044692e475dff80171cfe0802b67b8bc4ee0b9fe Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Thu, 4 Aug 2022 13:51:11 +0200 Subject: [PATCH] chore: Skip reprocessing invalid Stripe events (#5200) --- .../billing/handle_stripe_event_service.rb | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb index 0942bbe1f..43b03da8a 100644 --- a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb +++ b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb @@ -3,18 +3,9 @@ class Enterprise::Billing::HandleStripeEventService ensure_event_context(event) case @event.type when 'customer.subscription.updated' - plan = find_plan(subscription['plan']['product']) - account.update( - custom_attributes: { - stripe_customer_id: subscription.customer, - stripe_price_id: subscription['plan']['id'], - stripe_product_id: subscription['plan']['product'], - plan_name: plan['name'], - subscribed_quantity: subscription['quantity'] - } - ) + process_subscription_updated when 'customer.subscription.deleted' - Enterprise::Billing::CreateStripeCustomerService.new(account: account).perform + process_subscription_deleted else Rails.logger.debug { "Unhandled event type: #{event.type}" } end @@ -22,6 +13,29 @@ class Enterprise::Billing::HandleStripeEventService private + def process_subscription_updated + plan = find_plan(subscription['plan']['product']) + # skipping self hosted plan events + return if plan.blank? || account.blank? + + account.update( + custom_attributes: { + stripe_customer_id: subscription.customer, + stripe_price_id: subscription['plan']['id'], + stripe_product_id: subscription['plan']['product'], + plan_name: plan['name'], + subscribed_quantity: subscription['quantity'] + } + ) + end + + def process_subscription_deleted + # skipping self hosted plan events + return if account.blank? + + Enterprise::Billing::CreateStripeCustomerService.new(account: account).perform + end + def ensure_event_context(event) @event = event end