Class: Spree::MercadoPagoController

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

Instance Method Summary collapse

Instance Method Details

#checkoutObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/spree/mercado_pago_controller.rb', line 6

def checkout
  current_order.state_name == :payment || raise(ActiveRecord::RecordNotFound)
  payment_method = PaymentMethod::MercadoPago.find(params[:payment_method_id])
  payment = current_order.payments.
    create!({amount: current_order.total, payment_method: payment_method})
  payment.started_processing!

  preferences = ::MercadoPago::OrderPreferencesBuilder.
    new(current_order, payment, callback_urls).
    preferences_hash

  provider = payment_method.provider
  provider.create_preferences(preferences)

  redirect_to provider.redirect_url
end

#failureObject



33
34
35
36
37
38
# File 'app/controllers/spree/mercado_pago_controller.rb', line 33

def failure
  payment.failure!
  flash.notice = Spree.t(:payment_processing_failed)
  flash['order_completed'] = true
  redirect_to spree.checkout_state_path(state: :payment)
end

#ipnObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/spree/mercado_pago_controller.rb', line 40

def ipn
  notification = MercadoPago::Notification.
    new(operation_id: params[:id], topic: params[:topic])

  if notification.save
    MercadoPago::HandleReceivedNotification.new(notification).process!
    status = :ok
  else
    status = :bad_request
  end

  render nothing: true, status: status
end

#successObject

Success/pending callbacks are currently aliases, this may change if required.



25
26
27
28
29
30
31
# File 'app/controllers/spree/mercado_pago_controller.rb', line 25

def success
  payment.pend!
  payment.order.next
  flash.notice = Spree.t(:order_processed_successfully)
  flash['order_completed'] = true
  redirect_to spree.order_path(payment.order)
end