Module: MuckCommerce::Models::MuckBillingInformation

Extended by:
ActiveSupport::Concern
Defined in:
lib/muck-commerce/models/billing_information.rb

Instance Method Summary collapse

Instance Method Details

#billing_valid?Boolean

Uses local checks to see if the billing information is valid

Returns:

  • (Boolean)


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
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/muck-commerce/models/billing_information.rb', line 47

def billing_valid?
  has_errors = false
  begin
    unless valid_expiry_year?(credit_card_expiration && credit_card_expiration.year) && valid_month?(credit_card_expiration && credit_card_expiration.month)
      has_errors = true
      errors.add(:credit_card_expiration, I18n.translate('muck.commerce.credit_card_expiration_invalid', :month => credit_card_expiration.month, :year => credit_card_expiration.year))
    end
    if !MuckCommerce.configuration.credit_card_types.map {|disp, value| value.downcase}.include?(self.credit_card_type)
      self.guess_card_type
      # guess the card type and try again
      if !MuckCommerce.configuration.credit_card_types.map {|disp, value| value.downcase}.include?(self.credit_card_type)
        has_errors = true
        errors.add(:credit_card_type, I18n.translate('muck.commerce.credit_card_type_invalid', :type => self.credit_card_type))
      end
    end
    if !self.class.valid_number?(credit_card_number)
      has_errors = true
      errors.add(:credit_card_number, I18n.translate('muck.commerce.credit_card_number_invalid', :number => self.credit_card.display_number))
    end
    
    cc = self.credit_card
    if !cc.valid?
      has_errors = true
      errors.add_to_base(I18n.translate('muck.commerce.credit_card_information_invalid', :errors => cc.errors.full_messages.to_sentence, 
                                                                                         :month => cc.month, 
                                                                                         :year => cc.year, 
                                                                                         :type => cc.type, 
                                                                                         :number => cc.display_number))
    end                                                                                      
  rescue => ex
    has_errors = true
    errors.add_to_base(I18n.translate('muck.commerce.credit_card_information_validation_exception', :error => ex))
  end
  
  !has_errors

end

#credit_cardObject



187
188
189
190
191
192
193
194
195
196
# File 'lib/muck-commerce/models/billing_information.rb', line 187

def credit_card
  card_options = {
    :first_name => self.get_first_name,
    :last_name => self.get_last_name,
    :number => self.credit_card_number,
    :type => self.credit_card_type }
  card_options[:month] = self.credit_card_expiration.month if self.credit_card_expiration
  card_options[:year] = self.credit_card_expiration.year if self.credit_card_expiration
  has_cvv? ? build_card_with_cvv(card_options) : build_card_without_cvv(card_options)
end

#credit_card?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/muck-commerce/models/billing_information.rb', line 144

def credit_card?
  'CC' == self.payment_method
end

#credit_card_for_cimObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/muck-commerce/models/billing_information.rb', line 104

def credit_card_for_cim
  if self.credit_card_number_available?
    self.credit_card
  else
    # if we don't have a credit card we don't want to try to update the cim with just 4 digits
    # passing this mask will tell the cim not to update the card at all
    OpenStruct.new(:number => "XXXX#{self.credit_card_number}", 
                   :year => self.credit_card_expiration.year, 
                   :month => self.credit_card_expiration.month)
  end
end

#credit_card_numberObject



95
96
97
# File 'lib/muck-commerce/models/billing_information.rb', line 95

def credit_card_number
  @credit_card_number_value || "XXXX-XXXX-XXXX-#{read_attribute(:credit_card_last_digits)}"
end

#credit_card_number=(val) ⇒ Object

don’t store the credit card number



90
91
92
93
# File 'lib/muck-commerce/models/billing_information.rb', line 90

def credit_card_number=(val)
  @credit_card_number_value = val
  write_attribute(:credit_card_last_digits, self.class.last_digits(@credit_card_number_value)) # only store the last 4 digits for customer reference    
end

#credit_card_number_available?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/muck-commerce/models/billing_information.rb', line 99

def credit_card_number_available?
  return false if self.credit_card_number.blank?
  self.credit_card_number.to_s.length > 4
end

#credit_card_type=(val) ⇒ Object



85
86
87
# File 'lib/muck-commerce/models/billing_information.rb', line 85

def credit_card_type=(val)
  write_attribute(:credit_card_type, val.downcase) unless val.nil?
end

#get_first_nameObject



163
164
165
166
167
168
169
# File 'lib/muck-commerce/models/billing_information.rb', line 163

def get_first_name
  if !self.first_name.blank?
    self.first_name
  else
    self.parse_name(self.name)[0]
  end
end

#get_last_nameObject



171
172
173
174
175
176
177
# File 'lib/muck-commerce/models/billing_information.rb', line 171

def get_last_name
  if !self.last_name.blank?
    self.last_name
  else
    self.parse_name(self.name)[1]
  end
end

#guess_card_typeObject



152
153
154
# File 'lib/muck-commerce/models/billing_information.rb', line 152

def guess_card_type
  self.credit_card_type = ActiveMerchant::Billing::CreditCard.type?(self.credit_card_number)
end

#has_billing_profile?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/muck-commerce/models/billing_information.rb', line 198

def has_billing_profile? 
  !self.billable.customer_profile_id.blank? && !self.customer_payment_profile_id.blank?
end

#has_cvv?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/muck-commerce/models/billing_information.rb', line 136

def has_cvv?
  !self.verification_value.blank?
end

#is_ach?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/muck-commerce/models/billing_information.rb', line 148

def is_ach?
  'ACH' == self.payment_method
end

#is_authorized?Boolean

Returns:

  • (Boolean)

Raises:



156
157
158
159
160
161
# File 'lib/muck-commerce/models/billing_information.rb', line 156

def is_authorized?
  amount = 100 # run a $1 authorization on their credit card
  authorization = OrderTransaction.authorize(amount, self)
  raise MuckCommerce::Exceptions::PaymentGatewayError, I18n.translate('muck.commerce.payment_information_problem', :message => authorization.message) if !authorization.success?
  authorization.success?
end

#parse_name(name) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/muck-commerce/models/billing_information.rb', line 179

def parse_name(name)
  return '' if name.blank?
  names = name.split(' ')
  return '' if names.length <= 0
  return [names[0], names[0]] if names.length == 1
  [names[0], names.slice(1, names.length).join(' ')]
end

#require_cvv_value?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/muck-commerce/models/billing_information.rb', line 140

def require_cvv_value?
  MuckCommerce.configuration.require_cvv_value
end

#secure_displayObject



132
133
134
# File 'lib/muck-commerce/models/billing_information.rb', line 132

def secure_display
  "xxxx-xxxx-xxxx-#{self.credit_card_last_digits}"
end

#update_remote_storageObject

Updates billing information in remote storage such as Authorize.net CIM or Braintree vault



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/muck-commerce/models/billing_information.rb', line 117

def update_remote_storage
  response = nil
  message = ''
  if MuckCommerce.configuration.cim_enabled?
    # Update CIM if billing information has changed
    response = OrderTransaction.cim_create_update(@billing_information.billable, @billing_information, @billing_information.credit_card_number_available?)
    if response.success?
      message = translate('muck.commerce.billing_information_updated')
    else
      message = translate('muck.commerce.billing_information_update_error', :error => response.message)
    end
  end
  [response, message]
end