Class: Spree::SolidusSixSaferpay::CheckoutController

Inherits:
StoreController
  • Object
show all
Defined in:
app/controllers/spree/solidus_six_saferpay/checkout_controller.rb

Instance Method Summary collapse

Instance Method Details

#failObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/spree/solidus_six_saferpay/checkout_controller.rb', line 107

def fail
  order_number = params[:order_number]
  @order = Spree::Order.find_by(number: order_number)

  if @order.nil?
    OrderNotFoundHandler.call(controller_context: self, order_number: order_number)

    flash[:error] = t('.error_while_processing_payment')
    @redirect_path ||= spree.cart_path

    render :iframe_breakout_redirect, layout: false
    return
  end

  saferpay_payment = Spree::SixSaferpayPayment.where(order_id: @order.id).order(:created_at).last

  if saferpay_payment.nil?
    PaymentNotFoundHandler.call(controller_context: self, order: @order)

    flash[:error] = t('.error_while_processing_payment')
    @redirect_path = spree.cart_path
    render :iframe_breakout_redirect, layout: false
    return
  end


  payment_inquiry = inquire_payment(saferpay_payment)
  flash[:error] = payment_inquiry.user_message

  @redirect_path = order_checkout_path(:payment)
  render :iframe_breakout_redirect, layout: false
end

#initObject



5
6
7
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
# File 'app/controllers/spree/solidus_six_saferpay/checkout_controller.rb', line 5

def init
  order_number = params[:order_number]
  @order = Spree::Order.find_by(number: order_number)

  # We must make sure that the order for which the user requests a
  # payment is still their `current_order`.
  # This can be false if the user tries to add something to the cart
  # after getting to the payment checkout step, and then switches back to
  # the payment step and starts the payment process by selecting a
  # payment method.
  # In that case, we redirect to the checkout path so the user needs to
  # go through the checkout process again to ensure that the real
  # `current_order` contains all necessary information.
  if @order.nil? || @order != current_order
    render json: {
      redirect_url: spree.cart_path,
      errors: t('.order_was_modified_after_confirmation')
    }, status: 422
    return
  end

  payment_method = Spree::PaymentMethod.find(params[:payment_method_id])
  initialized_payment = initialize_payment(@order, payment_method)

  if initialized_payment.success?
    redirect_url = initialized_payment.redirect_url
    render json: { redirect_url: redirect_url }
  else
    render json: {
      redirect_url: spree.cart_path,
      errors: t('.checkout_not_initialized')
    }, status: 422
  end
end

#successObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/spree/solidus_six_saferpay/checkout_controller.rb', line 40

def success
  order_number = params[:order_number]
  @order = Spree::Order.find_by(number: order_number)

  if @order.nil?
    OrderNotFoundHandler.call(controller_context: self, order_number: order_number)

    flash[:error] = t('.error_while_processing_payment')
    @redirect_path ||= spree.cart_path

    render :iframe_breakout_redirect, layout: false
    return
  end

  saferpay_payment = Spree::SixSaferpayPayment.where(order_id: @order.id).order(:created_at).last

  if saferpay_payment.nil?
    PaymentNotFoundHandler.call(controller_context: self, order: @order)

    flash[:error] = t('.error_while_processing_payment')
    @redirect_path ||= spree.cart_path

    render :iframe_breakout_redirect, layout: false
    return
  end

  # ensure that completed orders don't try to reprocess the
  # authorization. This could happen if a user presses the back button
  # after completing an order.
  # There is no error handling because it should look like you are simply
  # redirected to the cart page.
  if @order.completed?
    @redirect_path = spree.cart_path
    render :iframe_breakout_redirect, layout: false
    return
  end


  # NOTE: PaymentPage payments are authorized directly. Instead, we
  # perform an ASSERT here to gather the necessary details.
  # This might be confusing at first, but doing it this way makes sense
  # (and the code a LOT more readable) IMO. Feel free to disagree and PR
  # a better solution.
  # NOTE: Transaction payments are authorized here so that the money is
  # already allocated when the user is on the confirm page. If the user
  # then chooses another payment, the authorized payment is voided
  # (cancelled).
  payment_authorization = authorize_payment(saferpay_payment)

  if payment_authorization.success?

    processed_authorization = process_authorization(saferpay_payment)
    if processed_authorization.success?
      PaymentProcessingSuccessHandler.call(controller_context: self, order: @order)
    else
      flash[:error] = processed_authorization.user_message
    end

  else
    payment_inquiry = inquire_payment(saferpay_payment)
    flash[:error] = payment_inquiry.user_message
  end

  @redirect_path ||= order_checkout_path(@order.state)
  render :iframe_breakout_redirect, layout: false
end