class SpookAndPay::CreditCard
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.
Constants
- FIELDS
The fields required for a credit card
Attributes
The basic attributes of the credit card.
The basic attributes of the credit card.
The basic attributes of the credit card.
Public Class Methods
Construct a new credit card using the ID from the provider and a hash containing the values of the card.
@param SpookAndPay::Providers::Base
provider @param [Numeric, String] id @param Hash vals @option vals String :number @option vals [String, Numeric] :expiration_month @option vals [String, Numeric] :expiration_year @option vals [String, Numeric] :cvv @option vals String :card_type @option vals String :name @option vals [true, false] :expired @option vals [true, false] :valid @param Class raw
# 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
Public Instance Methods
Checks to see if funds can be credited to a card. Depends on the gateway/provider supporting crediting and having a valid card.
@return [true, false]
# File lib/spook_and_pay/credit_card.rb, line 75 def can_credit? provider.supports_credit? and valid? and !expired? end
Checks to see if the provider/gateway supports the deletion of credit card details.
@return [true, false]
# File lib/spook_and_pay/credit_card.rb, line 99 def can_delete? provider.supports_delete? end
Checks to see if this card can be used for a purchase against the underlying gateway.
@return [true, false]
# File lib/spook_and_pay/credit_card.rb, line 91 def can_purchase? provider.supports_purchase? and valid? and !expired? end
Credits the specified amount to the card.
@param [String, Numeric] amount @return SpookAndPay::Result
@raises SpookAndPay::Providers::Base::NotSupportedError
@raises SpookAndPay::CreditCard::InvalidCardError
# File lib/spook_and_pay/credit_card.rb, line 109 def credit!(amount) verify_action provider.credit_via_credit_card(self, amount) end
Deletes the credit card from the provider's vault.
@return SpookAndPay::Result
@raises SpookAndPay::Providers::Base::NotSupportedError
# File lib/spook_and_pay/credit_card.rb, line 141 def delete! provider.delete_credit_card(self) end
Indicates if the card is expired. This is not calculated, but instead determined by the provider.
@return [true, false]
# File lib/spook_and_pay/credit_card.rb, line 156 def expired? expired end
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.
@return String
# 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
Generates a payment of the specified amount.
@param [String, Numeric] amount @return SpookAndPay::Result
@raises SpookAndPay::Providers::Base::NotSupportedError
@raises SpookAndPay::CreditCard::InvalidCardError
# File lib/spook_and_pay/credit_card.rb, line 132 def purchase!(amount) verify_action provider.purchase_via_credit_card(self, amount) end
Retains the credit card within the payment provider's vault.
@return SpookAndPay::Result
@raises SpookAndPay::Providers::Base::NotSupportedError
# File lib/spook_and_pay/credit_card.rb, line 164 def retain! provider.retain_credit_card(self) end
Indicates if the card details are valid.
@return [true, false]
# File lib/spook_and_pay/credit_card.rb, line 148 def valid? valid end
Private Instance Methods
A helper method which validates any actions — authorize, credit etc. — and where the card is expired or has invalid details, raises an error.
@return nil @raises SpookAndPay::CreditCard::InvalidCardError
# File lib/spook_and_pay/credit_card.rb, line 176 def verify_action raise InvalidCardError if expired? or !valid? end