Class: Spree::Billing::PaymentsController

Inherits:
BaseController
  • Object
show all
Includes:
Admin::OrderConcern, OrderParentsConcern, PaymentCreatable, PaymentFireable
Defined in:
app/controllers/spree/billing/payments_controller.rb

Instance Method Summary collapse

Methods included from PaymentFireable

#fire, #fire_action, #supported_events

Methods included from PaymentCreatable

#create

Methods inherited from BaseController

#current_vendor, #default_url_options, #edit_object_url, #handle_unauthorized_vendor, #page, #per_page, #required_vendor_user!, #set_locale, #switch_vendor, #vendors

Methods included from SpreeCmCommissioner::Billing::RoleAuthorization

#auth_action, #auth_entry, #auth_user, #authorize!, #authorize?, #authorize_admin, #authorize_role!, #redirect_unauthorized_access

Instance Method Details

#check_payment_sourceObject



92
93
94
95
96
# File 'app/controllers/spree/billing/payments_controller.rb', line 92

def check_payment_source
  return unless @payment.payment_source.nil? || @payment.payment_source.blank?

  raise SpreeCmCommissioner::PaymentSourceMissingError
end

#collection_url(options = {}) ⇒ Object



128
129
130
# File 'app/controllers/spree/billing/payments_controller.rb', line 128

def collection_url(options = {})
  billing_order_payments_url(options)
end

#handle_payment_creation_errorObject



110
111
112
113
114
# File 'app/controllers/spree/billing/payments_controller.rb', line 110

def handle_payment_creation_error
  flash[:error] = Spree.t(:payment_creation_error)

  redirect_to request.referer || spree.billing_order_payments_path(@order)
end

#handle_payment_source_missingObject



98
99
100
101
102
# File 'app/controllers/spree/billing/payments_controller.rb', line 98

def handle_payment_source_missing
  flash[:error] = Spree.t(:payment_source_missing)

  redirect_to request.referer || spree.billing_order_payments_path(@order)
end

#handle_spree_core_gateway_error(error) ⇒ Object



104
105
106
107
108
# File 'app/controllers/spree/billing/payments_controller.rb', line 104

def handle_spree_core_gateway_error(error)
  flash[:error] = error.message.to_s

  redirect_to request.referer || spree.billing_order_payments_path(@order)
end

#indexObject



37
38
39
40
41
42
# File 'app/controllers/spree/billing/payments_controller.rb', line 37

def index
  @payments = @order.payments.includes(refunds: :reason)
  @refunds = @payments.flat_map(&:refunds)

  redirect_to new_billing_order_payment_url(@order) if @payments.empty?
end

#load_dataObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/spree/billing/payments_controller.rb', line 22

def load_data
  @amount = params[:amount] || @order.total
  @payment_methods = @order.collect_backend_payment_methods
  @penalty_in_day = SpreeCmCommissioner::PenaltyCalculator.calculate_penalty_in_days(Time.zone.now, @order.line_items.first.due_date)

  if @payment&.payment_method
    @payment_method = @payment.payment_method
  else
    @payment_method = @payment_methods.find { |p| p.id == params[:payment][:payment_method_id].to_i } if params[:payment]
    @payment_method ||= @payment_methods.first
  end
end

#load_resource_instanceObject



86
87
88
89
90
# File 'app/controllers/spree/billing/payments_controller.rb', line 86

def load_resource_instance
  return order.payments.build if new_actions.include?(action)

  order.payments.find_by!(number: params[:id])
end

#model_classObject



120
121
122
# File 'app/controllers/spree/billing/payments_controller.rb', line 120

def model_class
  Spree::Payment
end

#object_nameObject



124
125
126
# File 'app/controllers/spree/billing/payments_controller.rb', line 124

def object_name
  'payment'
end

#orderObject



116
117
118
# File 'app/controllers/spree/billing/payments_controller.rb', line 116

def order
  @order ||= Spree::Order.find_by!(number: params[:order_id])
end

#set_current_user_instanceObject



18
19
20
# File 'app/controllers/spree/billing/payments_controller.rb', line 18

def set_current_user_instance
  @payment.current_user_instance = spree_current_user
end

#showObject



44
45
46
# File 'app/controllers/spree/billing/payments_controller.rb', line 44

def show
  @payment = @object
end

#updateObject



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
# File 'app/controllers/spree/billing/payments_controller.rb', line 48

def update
  check_payment_source
  update_payment_amount

  unless @payment.save
    flash[:error] = @payment.errors.full_messages.join('. ')
    return redirect_to request.referer || spree.billing_order_payments_path(@order)
  end

  unless @payment.send("#{params[:e]}!")
    flash[:error] = Spree.t(:cannot_perform_operation)
    raise Spree::Core::GatewayError
  end

  flash[:success] = Spree.t(:payment_updated)

  return if params[:e] == 'void_transaction'

  return unless @order.outstanding_balance.positive?

  @payment = @order.payments.build(amount: @order.outstanding_balance, payment_method: @order.collect_backend_payment_methods.first)

  raise SpreeCmCommissioner::PaymentCreationError unless @payment.save

  redirect_to request.referer || spree.billing_order_payments_path(@order)
end

#verify_eventObject

supported_events is a method from Spree::Billing::PaymentFireable

Raises:

  • (Spree::Core::GatewayError)


76
77
78
79
80
81
82
83
84
# File 'app/controllers/spree/billing/payments_controller.rb', line 76

def verify_event
  # Because we have a transition method also called void, we do this to avoid conflicts.
  params[:e] = 'void_transaction' if params[:e] == 'void'

  return if supported_events.include?(params[:e])

  flash[:error] = Spree.t(:unsupported_event)
  raise Spree::Core::GatewayError
end