solidusio / solidus_stripe

💳 Integrate Solidus with Stripe
https://stripe.com
BSD 3-Clause "New" or "Revised" License
36 stars 61 forks source link

Detach payment method associated to a setup intent on guest checkout #205

Closed waiting-for-dev closed 1 year ago

waiting-for-dev commented 1 year ago

When performing a guest checkout through the setup intent flow, creating another payment from the Stripe dashboard is possible.

A solution via a subscriber was attempted as part of #203. The code looked like the following, although it'd need refining after the final version is released:

# frozen-string-literal: true

module SolidusStripe
  class SetupIntentSubscriber
    include Omnes::Subscriber

    handle :order_finalized, with: :detach_setup_intent

    def detach_setup_intent(event)
      order = event[:order]
      payment_method = order.payments.find do |payment|
        payment.payment_method.type == "SolidusStripe::PaymentMethod"
      end&.payment_method
      return unless payment_method && payment_method.preferred_stripe_intents_flow == "setup" && order.user.nil?

      intent = payment_method.find_setup_intent_for_order(order)
      payment_method.gateway.request do
        Stripe::PaymentMethod.detach(intent.payment_method)
      end
    end
  end
end

Some considerations:

elia commented 1 year ago

Not needed since we're moving to only use PaymentIntents in #249