Class: Spree::AffirmController

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

Instance Method Summary collapse

Instance Method Details

#cancelObject



74
75
76
# File 'app/controllers/spree/affirm_controller.rb', line 74

def cancel
  redirect_to checkout_state_path(current_order.state)
end

#confirmObject



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

def confirm
  order = current_order || raise(ActiveRecord::RecordNotFound)

  if !params[:checkout_token]
    flash[:notice] = "Invalid order confirmation data."
    return redirect_to checkout_state_path(current_order.state)
  end

  if order.complete?
    flash[:notice] = "Order already completed."
    return redirect_to completion_route order
  end

  _affirm_checkout = Spree::AffirmCheckout.new(
    order: order,
    token: params[:checkout_token],
    payment_method: payment_method
  )

  # check if data needs to be updated
  unless _affirm_checkout.valid?

    _affirm_checkout.errors.each do |field, error|
      case field
      when :billing_address
        # FIXME(brian): pass the phone number to the |order| in a better place
        phone = order.bill_address.phone
        order.bill_address = generate_spree_address(_affirm_checkout.details['billing'])
        order.bill_address.phone = phone

      when :shipping_address
        # FIXME(brian): pass the phone number to the |order| in a better place
        phone = order.shipping_address.phone
        order.ship_address = generate_spree_address(_affirm_checkout.details['shipping'])
        order.ship_address.phone = phone

      when :billing_email
        order.email = _affirm_checkout.details["billing"]["email"]

      end
    end

    order.save
  end

  _affirm_checkout.save

  _affirm_payment = order.payments.create!({
    payment_method: payment_method,
    amount: order.total,
    source: _affirm_checkout
  })

  # transition to confirm or complete
  while order.next; end

  if order.completed?
    session[:order_id] = nil
    flash.notice = Spree.t(:order_processed_successfully)
    flash[:commerce_tracking] = "nothing special"
    redirect_to completion_route order
  else
    redirect_to checkout_state_path(order.state)
  end
end