lib/pay/stripe/subscription.rb in pay-6.2.4 vs lib/pay/stripe/subscription.rb in pay-6.3.0

- old
+ new

@@ -299,14 +299,27 @@ def upcoming_invoice(**options) ::Stripe::Invoice.upcoming(options.merge(subscription: processor_id), stripe_options) end # Retries the latest invoice for a Past Due subscription and attempts to pay it - def retry_failed_payment - payment_intent = ::Stripe::PaymentIntent.confirm(subscription.latest_invoice.payment_intent.id) + def retry_failed_payment(payment_intent_id: nil) + payment_intent_id ||= subscription.latest_invoice.payment_intent.id + payment_intent = ::Stripe::PaymentIntent.retrieve({id: payment_intent_id}, stripe_options) + + payment_intent = if payment_intent.status == "requires_payment_method" + ::Stripe::PaymentIntent.confirm(payment_intent_id, {payment_method: pay_subscription.customer.default_payment_method.processor_id}, stripe_options) + else + ::Stripe::PaymentIntent.confirm(payment_intent_id, stripe_options) + end Pay::Payment.new(payment_intent).validate rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e + end + + def pay_open_invoices + ::Stripe::Invoice.list({subscription: processor_id, status: :open}, stripe_options).auto_paging_each do |invoice| + retry_failed_payment(payment_intent_id: invoice.payment_intent) + end end private # Options for Stripe requests