You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix: Handle empty payments array in Stripe subscription swap
**Problem:**
A `NoMethodError: undefined method 'payment' for nil:NilClass` was occurring in `Pay::Stripe::Subscription#swap` when a user changed their subscription plan, and the change resulted in a credit or a $0 invoice (e.g., due to proration).
**Root Cause:**
When a Stripe subscription update results in a credit or a $0 invoice, the `latest_invoice` object returned by the Stripe API has an empty `payments` array. This is because no actual payment is processed, and therefore no `payment_intent` is generated for such invoices. The existing code was attempting to access `payments.first.payment.payment_intent` without checking if `payments.first` was `nil`.
**Solution:**
This commit updates the `swap` method to use safe navigation operators (`&.`) when accessing `latest_invoice.payments.first.payment.payment_intent`.
```ruby
# Before
# if (payment_intent_id = @api_record.latest_invoice.payments.first.payment.payment_intent)
# After
if (payment_intent_id = @api_record.latest_invoice.payments.first&.payment&.payment_intent)
0 commit comments