fix(billing): reconcile invoice default payment method by currency, drop default_source shortcut

This commit is contained in:
Tanmay Deep Sharma
2026-06-15 16:38:17 +05:30
parent de1469d2ca
commit 3c5c9574a3
2 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -169,7 +169,7 @@ en:
invalid_option: Invalid topup option
plan_not_eligible: Top-ups are only available for paid plans. Please upgrade your plan first.
stripe_customer_not_configured: Stripe customer not configured
no_payment_method: No payment methods found. Please add a payment method before making a purchase.
no_payment_method: No payment method available for your billing currency. Please add a card before making a purchase.
billing:
currency_required: Currency is required
currency_switch_unavailable: Currency switching is not available for this account
@@ -1,16 +1,13 @@
# Ensures the customer's default Stripe payment method can actually bill the given currency. PIX/boleto
# are BRL-only, so when an account moves to a currency they can't pay we drop them as the default and
# pick a compatible method (a card) if one is attached. Incompatible methods stay attached for later.
# Keyed off invoice_settings.default_payment_method, since that's what Stripe Billing charges.
class Enterprise::Billing::DefaultPaymentMethodReconciler
pattr_initialize [:account!, :currency!]
# Returns the id of a currency-compatible default payment method, or nil if none is available.
def reconcile
customer = Stripe::Customer.retrieve(stripe_customer_id)
# Legacy card sources are currency-agnostic.
return customer.default_source if customer.default_source.present?
current_default = customer.invoice_settings.default_payment_method
current_default = current_default_payment_method
return current_default if compatible?(payment_methods.find { |method| method.id == current_default })
compatible = payment_methods.find { |method| compatible?(method) }
@@ -22,6 +19,10 @@ class Enterprise::Billing::DefaultPaymentMethodReconciler
private
def current_default_payment_method
Stripe::Customer.retrieve(stripe_customer_id).invoice_settings.default_payment_method
end
def payment_methods
@payment_methods ||= Stripe::PaymentMethod.list(customer: stripe_customer_id, limit: 100).data
end