Class: GatewayCard

Inherits:
Object
  • Object
show all
Includes:
GatewayErrors
Defined in:
lib/gateway_card.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card_number, cvv, expiration_month, expiration_year) ⇒ GatewayCard

Returns a new instance of GatewayCard.



6
7
8
9
10
11
12
13
# File 'lib/gateway_card.rb', line 6

def initialize(card_number, cvv, expiration_month, expiration_year)
  self.card_number      = card_number.to_s.gsub(/\s/, "")
  self.cvv              = cvv.to_s
  self.expiration_month = expiration_month.to_s
  self.expiration_year  = format('%04d', expiration_year)
  
  self.validate
end

Instance Attribute Details

#card_numberObject

Returns the value of attribute card_number.



4
5
6
# File 'lib/gateway_card.rb', line 4

def card_number
  @card_number
end

#cvvObject

Returns the value of attribute cvv.



4
5
6
# File 'lib/gateway_card.rb', line 4

def cvv
  @cvv
end

#expiration_monthObject

Returns the value of attribute expiration_month.



4
5
6
# File 'lib/gateway_card.rb', line 4

def expiration_month
  @expiration_month
end

#expiration_yearObject

Returns the value of attribute expiration_year.



4
5
6
# File 'lib/gateway_card.rb', line 4

def expiration_year
  @expiration_year
end

Instance Method Details

#authorize_net_credit_cardObject



47
48
49
# File 'lib/gateway_card.rb', line 47

def authorize_net_credit_card
  return AuthorizeNet::CreditCard.new(self.card_number, self.expiration_month.to_s + self.expiration_year.to_s, {:card_code => self.cvv})
end

#validateObject

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gateway_card.rb', line 15

def validate
  ######## Card Number Validation ##########
  ######## start ##########
  raise CardNumberInvalid unless self.card_number  =~ /^\d+$/ # Ensure card contains only digits
  
  ######## LUHN ALGORITHM TO CHECK CARD NUMBER #######
  ######## start ########
  arr = card.split('').reverse.each_with_index.map {|d, index| (index.odd? ? 2*(d.to_i) : d.to_i)}
  raise CardNumberInvalid unless (arr.join.split('').inject(0) {|tot, d| tot + d.to_i}) % 10 == 0
  ######## end ##########
  ######## end ##########

  ######## CVV Validation #########
  raise  CardCVVInvalid if (self.cvv != "") and (!(self.cvv  =~ /^\d+$/ ) or self.cvv.length > 4 or self.cvv.to_i == 0)

  ######## Expiration Month Validation #########
  raise CardExpirationMonthInvalid unless [1..12].index(self.expiration_month.to_i)

  ######## Expiration Year Validation #########
  ######## start #########
  raise CardExpirationYearInvalid if (!(self.expiration_year  =~ /^\d+$/ ) or self.expiration_year.length > 4 or self.expiration_year.to_i == 0)
  
  begin
    self.expiration_year = Date.parse(self.expiration_year.to_s + "0101").strftime("%y")
  rescue
    raise CardExpirationYearInvalid
  end
  ######## end ##########
  
  raise CardExpireError if (self.expiration_month + self.expiration_year).to_i < Time.now.strftime("%m%y")
end