Class: Spree::CreditCard
- Defined in:
- app/models/spree/credit_card.rb
Constant Summary collapse
- CARD_TYPES =
{ visa: /^4[0-9]{12}(?:[0-9]{3})?$/, master: /(^5[1-5][0-9]{14}$)|(^6759[0-9]{2}([0-9]{10})$)|(^6759[0-9]{2}([0-9]{12})$)|(^6759[0-9]{2}([0-9]{13})$)/, diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, american_express: /^3[47][0-9]{13}$/, discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/, jcb: /^(?:2131|1800|35\d{3})\d{11}$/ }
Instance Attribute Summary collapse
-
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
-
#imported ⇒ Object
Returns the value of attribute imported.
-
#number ⇒ Object
Returns the value of attribute number.
-
#verification_value ⇒ Object
Returns the value of attribute verification_value.
Instance Method Summary collapse
-
#actions ⇒ Array<String>
The actions available on this credit card.
- #address_attributes=(attributes) ⇒ Object
-
#can_capture?(payment) ⇒ Boolean
True when the payment is in the pending or checkout states.
-
#can_credit?(payment) ⇒ Boolean
Indicates whether its possible to credit the payment.
-
#can_void?(payment) ⇒ Boolean
True when the payment is not failed or voided.
-
#cc_type=(type) ⇒ Object
Sets the credit card type, converting it to the preferred internal representation from jquery.payment’s representation when appropriate.
-
#display_number ⇒ String
The card number, with all but last 4 numbers replace with “X”, as in “XXXX-XXXX-XXXX-4338”.
-
#expiry=(expiry) ⇒ Object
Sets the expiry date on this credit card.
-
#first_name ⇒ String
The first name on this credit card.
-
#has_payment_profile? ⇒ Boolean
True when there is a gateway customer or payment profile id present.
-
#last_name ⇒ String
The last name on this credit card.
-
#set_last_digits ⇒ Object
Sets the last digits field based on the assigned credit card number.
-
#to_active_merchant ⇒ ActiveMerchant::Billing::CreditCard
An ActiveMerchant credit card that represents this credit card.
-
#try_type_from_number ⇒ String
The credit card type if it can be determined from the number, otherwise the empty string.
-
#verification_value? ⇒ Boolean
True when a verification value is present.
Methods inherited from Base
Methods included from Preferences::Preferable
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
14 15 16 |
# File 'app/models/spree/credit_card.rb', line 14 def encrypted_data @encrypted_data end |
#imported ⇒ Object
Returns the value of attribute imported.
14 15 16 |
# File 'app/models/spree/credit_card.rb', line 14 def imported @imported end |
#number ⇒ Object
Returns the value of attribute number.
14 15 16 |
# File 'app/models/spree/credit_card.rb', line 14 def number @number end |
#verification_value ⇒ Object
Returns the value of attribute verification_value.
14 15 16 |
# File 'app/models/spree/credit_card.rb', line 14 def verification_value @verification_value end |
Instance Method Details
#actions ⇒ Array<String>
Returns the actions available on this credit card.
112 113 114 |
# File 'app/models/spree/credit_card.rb', line 112 def actions %w{capture void credit} end |
#address_attributes=(attributes) ⇒ Object
39 40 41 |
# File 'app/models/spree/credit_card.rb', line 39 def address_attributes=(attributes) self.address = Address.immutable_merge(address, attributes) end |
#can_capture?(payment) ⇒ Boolean
Returns true when the payment is in the pending or checkout states.
118 119 120 |
# File 'app/models/spree/credit_card.rb', line 118 def can_capture?(payment) payment.pending? || payment.checkout? end |
#can_credit?(payment) ⇒ Boolean
Indicates whether its possible to credit the payment. Note that most gateways require that the payment be settled first which generally happens within 12-24 hours of the transaction.
134 135 136 |
# File 'app/models/spree/credit_card.rb', line 134 def can_credit?(payment) payment.completed? && payment.credit_allowed > 0 end |
#can_void?(payment) ⇒ Boolean
Returns true when the payment is not failed or voided.
124 125 126 |
# File 'app/models/spree/credit_card.rb', line 124 def can_void?(payment) !payment.failed? && !payment.void? end |
#cc_type=(type) ⇒ Object
Sets the credit card type, converting it to the preferred internal representation from jquery.payment’s representation when appropriate.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/spree/credit_card.rb', line 74 def cc_type=(type) # cc_type is set by jquery.payment, which helpfully provides different # types from Active Merchant. Converting them is necessary. self[:cc_type] = case type when 'mastercard', 'maestro' then 'master' when 'amex' then 'american_express' when 'dinersclub' then 'diners_club' when '' then try_type_from_number else type end end |
#display_number ⇒ String
Returns the card number, with all but last 4 numbers replace with “X”, as in “XXXX-XXXX-XXXX-4338”.
107 108 109 |
# File 'app/models/spree/credit_card.rb', line 107 def display_number "XXXX-XXXX-XXXX-#{last_digits}" end |
#expiry=(expiry) ⇒ Object
Sets the expiry date on this credit card.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/spree/credit_card.rb', line 47 def expiry=(expiry) return unless expiry.present? self[:month], self[:year] = if expiry.match(/\d{2}\s?\/\s?\d{2,4}/) # will match mm/yy and mm / yyyy expiry.delete(' ').split('/') elsif match = expiry.match(/(\d{2})(\d{2,4})/) # will match mmyy and mmyyyy [match[1], match[2]] end if self[:year] self[:year] = "20" + self[:year] if self[:year].length == 2 self[:year] = self[:year].to_i end self[:month] = self[:month].to_i if self[:month] end |
#first_name ⇒ String
We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.
ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.
Returns the first name on this credit card.
149 150 151 |
# File 'app/models/spree/credit_card.rb', line 149 def first_name name.to_s.split(/[[:space:]]/, 2)[0] end |
#has_payment_profile? ⇒ Boolean
Returns true when there is a gateway customer or payment profile id present.
140 141 142 |
# File 'app/models/spree/credit_card.rb', line 140 def has_payment_profile? gateway_customer_profile_id.present? || gateway_payment_profile_id.present? end |
#last_name ⇒ String
We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.
ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.
Returns the last name on this credit card.
158 159 160 |
# File 'app/models/spree/credit_card.rb', line 158 def last_name name.to_s.split(/[[:space:]]/, 2)[1] end |
#set_last_digits ⇒ Object
Sets the last digits field based on the assigned credit card number.
87 88 89 90 91 |
# File 'app/models/spree/credit_card.rb', line 87 def set_last_digits number.to_s.gsub!(/\s/,'') verification_value.to_s.gsub!(/\s/,'') self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1) end |
#to_active_merchant ⇒ ActiveMerchant::Billing::CreditCard
Returns an ActiveMerchant credit card that represents this credit card.
164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/spree/credit_card.rb', line 164 def to_active_merchant ActiveMerchant::Billing::CreditCard.new( :number => number, :month => month, :year => year, :verification_value => verification_value, :first_name => first_name, :last_name => last_name, ) end |
#try_type_from_number ⇒ String
Returns the credit card type if it can be determined from the number, otherwise the empty string.
95 96 97 98 |
# File 'app/models/spree/credit_card.rb', line 95 def try_type_from_number numbers = number.delete(' ') if number CARD_TYPES.find{|type, pattern| return type.to_s if numbers =~ pattern}.to_s end |
#verification_value? ⇒ Boolean
Returns true when a verification value is present.
101 102 103 |
# File 'app/models/spree/credit_card.rb', line 101 def verification_value? verification_value.present? end |