Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Discard Handling for AuthenticationError #2239

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions app/services/invoices/payments/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def create

result.payment = payment
result
rescue Stripe::AuthenticationError, 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
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

def update_payment_status(organization_id:, provider_payment_id:, status:, metadata: {})
Expand Down Expand Up @@ -175,9 +188,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
Expand All @@ -190,14 +200,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
Expand Down