Class: SpreeStripe::PaymentIntentsController

Inherits:
Spree::StoreController
  • Object
show all
Includes:
Spree::CheckoutAnalyticsHelper
Defined in:
app/controllers/spree_stripe/payment_intents_controller.rb

Instance Method Summary collapse

Instance Method Details

#showObject

GET /spree/payment_intents/:id



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/spree_stripe/payment_intents_controller.rb', line 8

def show
  # fetch the payment intent active record
  @payment_intent_record = SpreeStripe::PaymentIntent.find(params[:id])

  # fetch the stripe payment intent
  @stripe_payment_intent = @payment_intent_record.stripe_payment_intent

  @order = @payment_intent_record.order

  # if somehow order was canceled, we need to redirect the customer to the cart
  # this is a rare case, but we need to handle it
  if @order.canceled?
    flash[:error] = Spree.t(:order_canceled)
    redirect_to spree.cart_path, status: :see_other
  # if the order is already completed (race condition)
  # we need to redirect the customer to the complete checkout page
  # but we need to make sure not to set the session flag to indicate that the order was placed now
  # because we don't know if the order was actually placed or not
  elsif @order.completed?
    redirect_to spree.checkout_complete_path(@order.token), status: :see_other
  # if the payment intent is successful, we need to process the payment and complete the order
  elsif @payment_intent_record.accepted?
    @order = SpreeStripe::CompleteOrder.new(payment_intent: @payment_intent_record).call

    # set the session flag to indicate that the order was placed now
    track_checkout_completed if @order.completed?

    # redirect the customer to the complete checkout page
    redirect_to spree.checkout_complete_path(@order.token), status: :see_other
  else
    handle_failure(Spree.t("stripe.payment_intent_errors.#{@stripe_payment_intent.status}"))
  end
rescue Spree::Core::GatewayError => e
  handle_failure(e.message)
end