From 4f451025acb33af98fd77739959625313e1ae4f3 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Tue, 2 Jul 2024 17:59:11 +0100 Subject: [PATCH 1/7] feat: add discard on AuthenticationError --- app/jobs/invoices/payments/stripe_create_job.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/jobs/invoices/payments/stripe_create_job.rb b/app/jobs/invoices/payments/stripe_create_job.rb index e7c87bfd88d..a08e59e67f4 100644 --- a/app/jobs/invoices/payments/stripe_create_job.rb +++ b/app/jobs/invoices/payments/stripe_create_job.rb @@ -9,6 +9,7 @@ class StripeCreateJob < ApplicationJob retry_on Stripe::RateLimitError, wait: :polynomially_longer, attempts: 6 retry_on Stripe::APIConnectionError, wait: :polynomially_longer, attempts: 6 + discard_on Stripe::AuthenticationError def perform(invoice) result = Invoices::Payments::StripeService.new(invoice).create From 23cf10e577239c99e86bfca9d4026e96ff507072 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Wed, 3 Jul 2024 16:53:21 +0100 Subject: [PATCH 2/7] feat: added logger --- app/jobs/invoices/payments/stripe_create_job.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/jobs/invoices/payments/stripe_create_job.rb b/app/jobs/invoices/payments/stripe_create_job.rb index a08e59e67f4..9332d3b2e29 100644 --- a/app/jobs/invoices/payments/stripe_create_job.rb +++ b/app/jobs/invoices/payments/stripe_create_job.rb @@ -9,7 +9,9 @@ class StripeCreateJob < ApplicationJob retry_on Stripe::RateLimitError, wait: :polynomially_longer, attempts: 6 retry_on Stripe::APIConnectionError, wait: :polynomially_longer, attempts: 6 - discard_on Stripe::AuthenticationError + discard_on Stripe::AuthenticationError do |_, error| + Rails.logger.warn(error.message) + end def perform(invoice) result = Invoices::Payments::StripeService.new(invoice).create From d45c262a9c9ad5cd52d11f9df33c359dbbc03d0b Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Thu, 4 Jul 2024 16:14:45 +0100 Subject: [PATCH 3/7] feat: unified the rescue handler --- .../invoices/payments/stripe_create_job.rb | 3 --- .../invoices/payments/stripe_service.rb | 19 ++++++++----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/jobs/invoices/payments/stripe_create_job.rb b/app/jobs/invoices/payments/stripe_create_job.rb index 9332d3b2e29..e7c87bfd88d 100644 --- a/app/jobs/invoices/payments/stripe_create_job.rb +++ b/app/jobs/invoices/payments/stripe_create_job.rb @@ -9,9 +9,6 @@ class StripeCreateJob < ApplicationJob retry_on Stripe::RateLimitError, wait: :polynomially_longer, attempts: 6 retry_on Stripe::APIConnectionError, wait: :polynomially_longer, attempts: 6 - discard_on Stripe::AuthenticationError do |_, error| - Rails.logger.warn(error.message) - end def perform(invoice) result = Invoices::Payments::StripeService.new(invoice).create diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index a611b1d00e4..6cd644b55fb 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -51,6 +51,14 @@ def create result.payment = payment result + rescue Stripe::AuthenticationError, Stripe::CardError, Stripe::InvalidRequestError, Stripe::PermissionError => e + deliver_error_webhook(e) + # NOTE: Do not mark the invoice as failed if the amount is too small for Stripe + # For now we keep it as pending, the user can still update it manually + return if e.code == 'amount_too_small' + + update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) + nil end def update_payment_status(organization_id:, provider_payment_id:, status:, metadata: {}) @@ -175,9 +183,6 @@ def update_payment_method_id if (payment_method_id = result.invoice_settings.default_payment_method || result.default_source) customer.stripe_customer.update!(payment_method_id:) end - rescue Stripe::StripeError => e - deliver_error_webhook(e) - raise end def create_stripe_payment @@ -190,14 +195,6 @@ def create_stripe_payment idempotency_key: "#{invoice.id}/#{invoice.payment_attempts}" } ) - rescue Stripe::CardError, Stripe::InvalidRequestError, Stripe::PermissionError => e - # NOTE: Do not mark the invoice as failed if the amount is too small for Stripe - # For now we keep it as pending, the user can still update it manually - return if e.code == 'amount_too_small' - - deliver_error_webhook(e) - update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) - nil end def stripe_payment_payload From c4d5a43a9f8b8e59d0c869ce6630f5d215830d46 Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Thu, 4 Jul 2024 16:23:50 +0100 Subject: [PATCH 4/7] feat: unified the rescue handler --- app/services/invoices/payments/stripe_service.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index 6cd644b55fb..35c3ea22b99 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -59,6 +59,9 @@ def create update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) nil + rescue Stripe::RateLimitError, Stripe::APIConnectionError => e + deliver_error_webhook(e) + raise end def update_payment_status(organization_id:, provider_payment_id:, status:, metadata: {}) From f23714c18152d5d4f9a79d167881f1e37d68b6ce Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Thu, 4 Jul 2024 16:28:53 +0100 Subject: [PATCH 5/7] feat: unified the rescue handler --- app/services/invoices/payments/stripe_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index 35c3ea22b99..738a4bcc0cc 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -52,11 +52,11 @@ def create result.payment = payment result rescue Stripe::AuthenticationError, Stripe::CardError, Stripe::InvalidRequestError, Stripe::PermissionError => e - deliver_error_webhook(e) # NOTE: Do not mark the invoice as failed if the amount is too small for Stripe # For now we keep it as pending, the user can still update it manually return if e.code == 'amount_too_small' + deliver_error_webhook(e) update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) nil rescue Stripe::RateLimitError, Stripe::APIConnectionError => e From e2d391eeb27e6d541635ccdc3a145ef5898da52b Mon Sep 17 00:00:00 2001 From: brunomiguelpinto Date: Fri, 5 Jul 2024 08:58:20 +0100 Subject: [PATCH 6/7] Update stripe_service.rb Co-authored-by: Vincent Pochet --- app/services/invoices/payments/stripe_service.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index 738a4bcc0cc..358178d7163 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -60,8 +60,11 @@ def create update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) nil rescue Stripe::RateLimitError, Stripe::APIConnectionError => e + raise # Let the auto-retry process do its own job + rescue Stripe::StripeError => e deliver_error_webhook(e) raise + end end def update_payment_status(organization_id:, provider_payment_id:, status:, metadata: {}) From f977e2fdc6de65410c42866f8cb268d14ecb41ef Mon Sep 17 00:00:00 2001 From: Miguel Pinto Date: Fri, 5 Jul 2024 09:30:28 +0100 Subject: [PATCH 7/7] feat: fix typo --- app/services/invoices/payments/stripe_service.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/services/invoices/payments/stripe_service.rb b/app/services/invoices/payments/stripe_service.rb index 358178d7163..0d3bfb5b6ab 100644 --- a/app/services/invoices/payments/stripe_service.rb +++ b/app/services/invoices/payments/stripe_service.rb @@ -59,12 +59,11 @@ def create deliver_error_webhook(e) update_invoice_payment_status(payment_status: :failed, deliver_webhook: false) nil - rescue Stripe::RateLimitError, Stripe::APIConnectionError => e + rescue Stripe::RateLimitError, Stripe::APIConnectionError raise # Let the auto-retry process do its own job rescue Stripe::StripeError => e deliver_error_webhook(e) raise - end end def update_payment_status(organization_id:, provider_payment_id:, status:, metadata: {})