Class: Checkout

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::Preprocessor

#preprocess

Constructor Details

#initialize(cart, payment, notes = nil) ⇒ Checkout

Returns a new instance of Checkout.



33
34
35
36
37
38
39
# File 'app/models/checkout.rb', line 33

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

Instance Attribute Details

#cartObject

Returns the value of attribute cart.



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

def cart
  @cart
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#notesObject

Returns the value of attribute notes.



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

def notes
  @notes
end

#orderObject (readonly)

Returns the value of attribute order.



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

def order
  @order
end

#paymentObject

Returns the value of attribute payment.



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

def payment
  @payment
end

#personObject (readonly)

Returns the value of attribute person.



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

def person
  @person
end

Class Method Details

.for(cart, payment) ⇒ Object



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

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

Instance Method Details

#capture_paymentObject



100
101
102
103
104
# File 'app/models/checkout.rb', line 100

def capture_payment
  log "capturing payment [#{@transaction_id}]"
  payment.capture(@transaction_id, {})
  log "payment captured"
end

#checkout_nameObject



112
113
114
# File 'app/models/checkout.rb', line 112

def checkout_name
  "checkout"
end

#finishObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/checkout.rb', line 56

def finish
  log "started"
  begin
    log "preprocess"
    if preprocess && pay
      ActiveRecord::Base.transaction do
        log "approving"
        cart.approve!
        log "approved"
        run_callbacks :order do
          log "creating orders"
          @created_orders = create_order(Time.now)
          log "created orders [#{@created_orders.collect(&:id)}]"
        end
      end
    else
      log "authorization failed"
    end
  rescue Exception => e
    log "threw exception"
    log e
    log e.backtrace.inspect
    void_payment if @payment_approved
    raise e
  end

  capture_payment if @payment_approved
  @payment_approved || false
end

#log(message) ⇒ Object



86
87
88
# File 'app/models/checkout.rb', line 86

def log(message)
  Rails.logger.info "CHECKOUT: cart [#{cart.id}] #{message}"
end

#messageObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/checkout.rb', line 16

def message
  message = @error || @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?
    if @fafs_success == true
      message = "We've processed your donation but could not reserve your tickets. Please check your information and try again or contact us to complete your purchase."
    else
      message = "We had a problem validating your payment.  Wait a few moments and try again or contact us to complete your purchase."
    end
  end

  message
end

#payObject



90
91
92
93
94
95
96
97
98
# File 'app/models/checkout.rb', line 90

def pay
  log "authorizing"
  options = {}
  options[:service_fee] = cart.fee_in_cents
  @payment_approved = payment.authorize(options)
  @transaction_id = payment.transaction_id
  log "authorized with [#{@transaction_id}]"
  @payment_approved
end

#valid?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/checkout.rb', line 41

def valid?
  return false if cart.nil?

  if cart.empty?
    @error = "Your tickets have expired.  Please select your tickets again."
    return false
  end

  unless (!!cart and !!payment and payment.valid?)
    return false
  end

  true
end

#void_paymentObject



106
107
108
109
110
# File 'app/models/checkout.rb', line 106

def void_payment
  log "voiding [#{@transaction_id}]"
  payment.void(@transaction_id, {})
  log "payment voided"
end