Class: SpookAndPay::CreditCard

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ErroringReader

attr_erroring_reader

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.

Parameters:

  • SpookAndPay::Providers::Base

    provider

  • id (Numeric, String)
  • Hash

    vals

  • Class

    raw

  • vals (Hash)

    a customizable set of options

Options Hash (vals):

  • String (Object)

    :number

  • :expiration_month (String, Numeric)
  • :expiration_year (String, Numeric)
  • :cvv (String, Numeric)
  • String (Object)

    :card_type

  • String (Object)

    :name

  • :expired (true, false)
  • :valid (true, false)


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

#idObject (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

#providerObject (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

#rawObject (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.

Parameters:

  • amount (String, Numeric)

Returns:

  • SpookAndPay::Result



121
122
123
124
# 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?true, false

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

Returns:

  • (true, false)


83
84
85
# File 'lib/spook_and_pay/credit_card.rb', line 83

def can_authorize?
  provider.supports_authorize? 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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Returns:

  • (true, false)


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.

Parameters:

  • amount (String, Numeric)

Returns:

  • SpookAndPay::Result



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.

Returns:

  • SpookAndPay::Result



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.

Returns:

  • (true, false)


156
157
158
# File 'lib/spook_and_pay/credit_card.rb', line 156

def expired?
  expired
end

#numberObject

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.

Returns:

  • String



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.

Parameters:

  • amount (String, Numeric)

Returns:

  • SpookAndPay::Result



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.

Returns:

  • SpookAndPay::Result



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.

Returns:

  • (true, false)


148
149
150
# File 'lib/spook_and_pay/credit_card.rb', line 148

def valid?
  valid
end