Class: SpookAndPay::CreditCard
- Inherits:
-
Object
- Object
- SpookAndPay::CreditCard
- Extended by:
- ErroringReader
- Defined in:
- lib/spook_and_pay/credit_card.rb
Overview
A simple, generic class which wraps the card details retrieved from a provider. This class is entirely read only, since it is only used to as part of inspecting a payment or handling errors.
Defined Under Namespace
Classes: InvalidCardError
Constant Summary collapse
- FIELDS =
The fields required for a credit card
[:number, :expiration_month, :expiration_year, :cvv, :card_type, :name, :valid, :expired].freeze
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
The basic attributes of the credit card.
-
#provider ⇒ Object
readonly
The basic attributes of the credit card.
-
#raw ⇒ Object
readonly
The basic attributes of the credit card.
Instance Method Summary collapse
-
#authorize!(amount) ⇒ Object
Authorizes a payment of the specified amount.
-
#can_authorize? ⇒ true, false
Checks to see if this card can be authorized against the specified gateway.
-
#can_credit? ⇒ true, false
Checks to see if funds can be credited to a card.
-
#can_delete? ⇒ true, false
Checks to see if the provider/gateway supports the deletion of credit card details.
-
#can_purchase? ⇒ true, false
Checks to see if this card can be used for a purchase against the underlying gateway.
-
#credit!(amount) ⇒ Object
Credits the specified amount to the card.
-
#delete! ⇒ Object
Deletes the credit card from the provider’s vault.
-
#expired? ⇒ true, false
Indicates if the card is expired.
-
#initialize(provider, id, vals, raw = nil) ⇒ CreditCard
constructor
Construct a new credit card using the ID from the provider and a hash containing the values of the card.
-
#number ⇒ Object
A getter which takes the card number stored and generates a nice masked version.
-
#purchase!(amount) ⇒ Object
Generates a payment of the specified amount.
-
#retain! ⇒ Object
Retains the credit card within the payment provider’s vault.
-
#valid? ⇒ true, false
Indicates if the card details are valid.
Methods included from ErroringReader
Constructor Details
#initialize(provider, id, vals, raw = nil) ⇒ CreditCard
Construct a new credit card using the ID from the provider and a hash containing the values of the card.
44 45 46 47 48 49 |
# File 'lib/spook_and_pay/credit_card.rb', line 44 def initialize(provider, id, vals, raw = nil) @provider = provider @id = id @raw = raw FIELDS.each {|f| instance_variable_set(:"@#{f}", vals[f]) if vals.has_key?(f)} end |
Instance Attribute Details
#id ⇒ Object (readonly)
The basic attributes of the credit card.
18 19 20 |
# File 'lib/spook_and_pay/credit_card.rb', line 18 def id @id end |
#provider ⇒ Object (readonly)
The basic attributes of the credit card.
18 19 20 |
# File 'lib/spook_and_pay/credit_card.rb', line 18 def provider @provider end |
#raw ⇒ Object (readonly)
The basic attributes of the credit card.
18 19 20 |
# File 'lib/spook_and_pay/credit_card.rb', line 18 def raw @raw end |
Instance Method Details
#authorize!(amount) ⇒ Object
Authorizes a payment of the specified amount. This generates a new transaction that must be later settled.
121 122 123 124 |
# File 'lib/spook_and_pay/credit_card.rb', line 121 def (amount) verify_action provider.(self, amount) end |
#can_authorize? ⇒ true, false
Checks to see if this card can be authorized against the specified gateway.
83 84 85 |
# File 'lib/spook_and_pay/credit_card.rb', line 83 def provider. and valid? and !expired? end |
#can_credit? ⇒ true, false
Checks to see if funds can be credited to a card. Depends on the gateway/provider supporting crediting and having a valid card.
75 76 77 |
# File 'lib/spook_and_pay/credit_card.rb', line 75 def can_credit? provider.supports_credit? and valid? and !expired? end |
#can_delete? ⇒ true, false
Checks to see if the provider/gateway supports the deletion of credit card details.
99 100 101 |
# File 'lib/spook_and_pay/credit_card.rb', line 99 def can_delete? provider.supports_delete? end |
#can_purchase? ⇒ true, false
Checks to see if this card can be used for a purchase against the underlying gateway.
91 92 93 |
# File 'lib/spook_and_pay/credit_card.rb', line 91 def can_purchase? provider.supports_purchase? and valid? and !expired? end |
#credit!(amount) ⇒ Object
Credits the specified amount to the card.
109 110 111 112 |
# File 'lib/spook_and_pay/credit_card.rb', line 109 def credit!(amount) verify_action provider.credit_via_credit_card(self, amount) end |
#delete! ⇒ Object
Deletes the credit card from the provider’s vault.
141 142 143 |
# File 'lib/spook_and_pay/credit_card.rb', line 141 def delete! provider.delete_credit_card(self) end |
#expired? ⇒ true, false
Indicates if the card is expired. This is not calculated, but instead determined by the provider.
156 157 158 |
# File 'lib/spook_and_pay/credit_card.rb', line 156 def expired? expired end |
#number ⇒ Object
A getter which takes the card number stored and generates a nice masked version. It also handles the case where the number isn’t available and just returns nil instead.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/spook_and_pay/credit_card.rb', line 56 def number if @number.nil? or @number.empty? nil else if @number.length < 12 case card_type when 'american_express' then "XXXX-XXXXXX-#{@number}" else "XXXX-XXXX-XXXX-#{@number}" end else @number end end end |
#purchase!(amount) ⇒ Object
Generates a payment of the specified amount.
132 133 134 135 |
# File 'lib/spook_and_pay/credit_card.rb', line 132 def purchase!(amount) verify_action provider.purchase_via_credit_card(self, amount) end |
#retain! ⇒ Object
Retains the credit card within the payment provider’s vault.
164 165 166 |
# File 'lib/spook_and_pay/credit_card.rb', line 164 def retain! provider.retain_credit_card(self) end |
#valid? ⇒ true, false
Indicates if the card details are valid.
148 149 150 |
# File 'lib/spook_and_pay/credit_card.rb', line 148 def valid? valid end |