Class: Checkout

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, Ext::Callbacks::Checkout
Defined in:
app/models/checkout.rb

Direct Known Subclasses

BoxOffice::Checkout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart, payment) ⇒ Checkout

Returns a new instance of Checkout.



28
29
30
31
32
33
# File 'app/models/checkout.rb', line 28

def initialize(cart, payment)
  @cart = cart
  @payment = payment
  @customer = payment.customer
  @payment.amount = @cart.total
end

Instance Attribute Details

#cartObject

Returns the value of attribute cart.



8
9
10
# File 'app/models/checkout.rb', line 8

def cart
  @cart
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'app/models/checkout.rb', line 8

def error
  @error
end

#orderObject (readonly)

Returns the value of attribute order.



9
10
11
# File 'app/models/checkout.rb', line 9

def order
  @order
end

#paymentObject

Returns the value of attribute payment.



8
9
10
# File 'app/models/checkout.rb', line 8

def payment
  @payment
end

#personObject (readonly)

Returns the value of attribute person.



9
10
11
# File 'app/models/checkout.rb', line 9

def person
  @person
end

Class Method Details

.for(cart, payment) ⇒ Object



11
12
13
# File 'app/models/checkout.rb', line 11

def self.for(cart, payment)
  cart.checkout_class.new(cart, payment)
end

Instance Method Details

#finishObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/checkout.rb', line 48

def finish
  run_callbacks :payment do
    pay
  end
  
  if cart.approved?
    order_timestamp = Time.now
    
    run_callbacks :order do
      @created_orders = create_order(order_timestamp)
    end
    
    cart.finish
  end
  
  cart.approved?
end

#messageObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/checkout.rb', line 15

def message
  message = @payment.errors.full_messages.to_sentence.downcase
  message = message.gsub('customer', 'contact info')
  message = message.gsub('credit card is', 'payment details are')
  message = message[0].upcase + message[1..message.length] unless message.blank? #capitalize first word
  
  if message.blank?
    message = "We had a problem validating your payment.  Wait a few moments and try again or contact us to complete your purchase."
  end
  
  message
end

#payObject



66
67
68
69
# File 'app/models/checkout.rb', line 66

def pay
  @cart.pay_with(@payment)
  @cart.approved?
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/checkout.rb', line 35

def valid?
  unless (!!cart and !!payment and payment.valid?)
    return false
  end
  
  if cart.empty?
    @error = "Your tickets have expired.  Please select your tickets again."
    return false
  end
  
  true
end