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

id[R]

The basic attributes of the credit card.

provider[R]

The basic attributes of the credit card.

raw[R]

The basic attributes of the credit card.

Public Class Methods

new(provider, id, vals, raw = nil) click to toggle source

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

authorize!(amount) click to toggle source

Authorizes a payment of the specified amount. This generates a new transaction that must be later settled.

@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 121
def authorize!(amount)
  verify_action
  provider.authorize_via_credit_card(self, amount)
end
can_authorize?() click to toggle source

Checks to see if this card can be authorized against the specified gateway.

@return [true, false]

# File lib/spook_and_pay/credit_card.rb, line 83
def can_authorize?
  provider.supports_authorize? and valid? and !expired?
end
can_credit?() click to toggle source

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
can_delete?() click to toggle source

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
can_purchase?() click to toggle source

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
credit!(amount) click to toggle source

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
delete!() click to toggle source

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
expired?() click to toggle source

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
number() click to toggle source

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
purchase!(amount) click to toggle source

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
retain!() click to toggle source

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
valid?() click to toggle source

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

verify_action() click to toggle source

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