Class: Ashmont::Customer

Inherits:
Object
  • Object
show all
Defined in:
lib/ashmont/customer.rb

Direct Known Subclasses

SubscribedCustomer

Constant Summary collapse

CREDIT_CARD_ATTRIBUTES =
[:cardholder_name, :number, :cvv, :expiration_month, :expiration_year].freeze
ADDRESS_ATTRIBUTES =
[:street_address, :extended_address, :locality, :region, :postal_code, :country_name].freeze
BILLING_ATTRIBUTES =
(CREDIT_CARD_ATTRIBUTES + ADDRESS_ATTRIBUTES).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil) ⇒ Customer

Returns a new instance of Customer.



9
10
11
12
# File 'lib/ashmont/customer.rb', line 9

def initialize(token = nil)
  @token = token
  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/ashmont/customer.rb', line 7

def errors
  @errors
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/ashmont/customer.rb', line 7

def token
  @token
end

Instance Method Details

#billing_emailObject



64
65
66
# File 'lib/ashmont/customer.rb', line 64

def billing_email
  remote_customer.email if persisted?
end

#cardholder_nameObject



80
81
82
# File 'lib/ashmont/customer.rb', line 80

def cardholder_name
  credit_card.cardholder_name if credit_card
end

#confirm(query_string) ⇒ Object



116
117
118
# File 'lib/ashmont/customer.rb', line 116

def confirm(query_string)
  handle_result Braintree::TransparentRedirect.confirm(query_string)
end

#country_nameObject



112
113
114
# File 'lib/ashmont/customer.rb', line 112

def country_name
  credit_card.billing_address.country_name if credit_card
end

#credit_cardObject



14
15
16
17
18
# File 'lib/ashmont/customer.rb', line 14

def credit_card
  # old way
  credit_cards[0]
  # primary_payment_account # too general
end

#credit_cardsObject



20
21
22
23
24
25
26
# File 'lib/ashmont/customer.rb', line 20

def credit_cards
  if persisted?
    remote_customer.credit_cards
  else
    []
  end
end

#deleteObject



72
73
74
# File 'lib/ashmont/customer.rb', line 72

def delete
  Braintree::Customer.delete(@token)
end

#expiration_monthObject



84
85
86
# File 'lib/ashmont/customer.rb', line 84

def expiration_month
  credit_card.expiration_month if credit_card
end

#expiration_yearObject



88
89
90
# File 'lib/ashmont/customer.rb', line 88

def expiration_year
  credit_card.expiration_year if credit_card
end

#extended_addressObject



96
97
98
# File 'lib/ashmont/customer.rb', line 96

def extended_address
  credit_card.billing_address.extended_address if credit_card
end

#has_billing_info?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/ashmont/customer.rb', line 56

def has_billing_info?
  credit_card.present?
end

#last_4Object



76
77
78
# File 'lib/ashmont/customer.rb', line 76

def last_4
  credit_card.last_4 if credit_card
end

#localityObject



100
101
102
# File 'lib/ashmont/customer.rb', line 100

def locality
  credit_card.billing_address.locality if credit_card
end

#payment_method_tokenObject



60
61
62
# File 'lib/ashmont/customer.rb', line 60

def payment_method_token
  credit_card.token if credit_card
end

#paypal_accountObject



29
30
31
# File 'lib/ashmont/customer.rb', line 29

def 
  paypal_accounts[0]
end

#paypal_accountsObject



32
33
34
35
36
37
38
# File 'lib/ashmont/customer.rb', line 32

def paypal_accounts
  if persisted?
    remote_customer.paypal_accounts
  else
    []
  end
end

#postal_codeObject



108
109
110
# File 'lib/ashmont/customer.rb', line 108

def postal_code
  credit_card.billing_address.postal_code if credit_card
end

#primary_payment_accountObject



52
53
54
# File 'lib/ashmont/customer.rb', line 52

def 
    primary_payment_method[0]
end

#primary_payment_methodObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ashmont/customer.rb', line 40

def primary_payment_method
  #currently, usually there is only one payment method, paypal or credit_card
  if persisted?
    payment_methods = { credit_cards.count => :credit_cards, 
                        paypal_accounts.count => :paypal_accounts }
    chosen_method = (payment_methods[payment_methods.keys.max])
    remote_customer.send(chosen_method)
  else
    []
  end
end

#regionObject



104
105
106
# File 'lib/ashmont/customer.rb', line 104

def region
  credit_card.billing_address.region if credit_card
end

#save(attributes) ⇒ Object



68
69
70
# File 'lib/ashmont/customer.rb', line 68

def save(attributes)
  handle_result create_or_update(attributes)
end

#street_addressObject



92
93
94
# File 'lib/ashmont/customer.rb', line 92

def street_address
  credit_card.billing_address.street_address if credit_card
end