Class: Spree::CieloDebtController

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

Instance Method Summary collapse

Instance Method Details

#confirmObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/spree/cielo_debt_controller.rb', line 36

def confirm
  order = current_order || raise(ActiveRecord::RecordNotFound)
  order.payments.create!({
    source: @credit_card,
    amount: order.total,
    payment_method_id: @payment_method.id
  })
  order.temporary_credit_card = true
  order.next
  if order.complete?
    flash.notice = Spree.t(:order_processed_successfully)
    flash[:order_completed] = true
    session[:order_id] = nil
    redirect_to order_path(order)
  else
    flash[:error] = order.errors.full_messages.join("\n")
    redirect_to checkout_state_path(order.state) and return
  end
end

#createObject



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

def create
  if current_order.guest_token != params[:guest_token]
    render status: 500, json: {error: Spree.t('errors.invalid_guest_token')} and return
  end
  if Spree::CieloConfig.debt_cards.include?(params[:source][:cc_type])
    credit_card = Spree::CreditCard.new(credit_card_params)
    if credit_card.save
      payment_method = Spree::PaymentMethod.find params[:payment_method_id]
      amount = current_order.display_total.money.cents
      response = payment_method.create amount, credit_card

      if response.has_key? :url_auth
        credit_card.update_attributes(gateway_payment_profile_id: response[:tid])
        render json: response
      else
        credit_card.destroy
        render status: 500, json: response
      end
    else
      errors = credit_card.errors.full_messages.join('<br/>')
      render status: 500, json: {error: errors}
    end
  else
    supported_cards = Spree::CieloConfig.debt_cards.collect { |i| "<li>#{Spree.t("cielo_#{i}")}</li>" }
    message = "<label style='font-weight: normal;'>#{Spree.t('errors.cielo_card_fail')}<ul>#{supported_cards.join('')}</ul></label>"
    render status: 500, inline: message.html_safe
  end
end