Class: PaystackCard

Inherits:
Object
  • Object
show all
Defined in:
lib/paystack/objects/card.rb

Constant Summary collapse

MAX_DINERS_CARD_LENGTH =
14
MAX_AMERICAN_EXPRESS_CARD_LENGTH =
15
MAX_NORMAL_CARD_LENGTH =
16
PATTERN_VISA =
/^4[0-9]{6,}$/
PATTERN_MASTERCARD =
/^5[1-5][0-9]{5,}$/
PATTERN_AMERICAN_EXPRESS =
/^3[47][0-9]{5,}$/
PATTERN_DINERS_CLUB =
/^3(?:0[0-5]|[68][0-9])[0-9]{4,}$/
PATTERN_DISCOVER =
/^6(?:011|5[0-9]{2})[0-9]{3,}$/
PATTERN_JCB =
/^(?:2131|1800|35[0-9]{3})[0-9]{3,}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ PaystackCard

Returns a new instance of PaystackCard.



17
18
19
20
21
22
23
24
# File 'lib/paystack/objects/card.rb', line 17

def initialize(args = {})
	@name = Utils.nullifyString(args[:name])
	@number = Utils.nullifyString(args[:number])
	@cvc = Utils.nullifyString(args[:cvc])
	@expiryMonth =  Utils.nullifyString(args[:expiryMonth])
	@expiryYear = Utils.nullifyString(args[:expiryYear])
	@cardIssuer = PaystackCard.getCardType(@number)
end

Instance Attribute Details

#addressCountryObject (readonly)

Returns the value of attribute addressCountry.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressCountry
  @addressCountry
end

#addressLine1Object (readonly)

Returns the value of attribute addressLine1.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressLine1
  @addressLine1
end

#addressLine2Object (readonly)

Returns the value of attribute addressLine2.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressLine2
  @addressLine2
end

#addressLine3Object (readonly)

Returns the value of attribute addressLine3.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressLine3
  @addressLine3
end

#addressLine4Object (readonly)

Returns the value of attribute addressLine4.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressLine4
  @addressLine4
end

#addressPostalCodeObject (readonly)

Returns the value of attribute addressPostalCode.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def addressPostalCode
  @addressPostalCode
end

#cardCountryObject (readonly)

Returns the value of attribute cardCountry.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def cardCountry
  @cardCountry
end

#cardIssuerObject (readonly)

Returns the value of attribute cardIssuer.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def cardIssuer
  @cardIssuer
end

#cvcObject (readonly)

Returns the value of attribute cvc.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def cvc
  @cvc
end

#emailObject (readonly)

Returns the value of attribute email.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def email
  @email
end

#expiryMonthObject (readonly)

Returns the value of attribute expiryMonth.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def expiryMonth
  @expiryMonth
end

#expiryYearObject (readonly)

Returns the value of attribute expiryYear.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def expiryYear
  @expiryYear
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



4
5
6
# File 'lib/paystack/objects/card.rb', line 4

def number
  @number
end

Class Method Details

.getCardType(number) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/paystack/objects/card.rb', line 35

def PaystackCard.getCardType(number)
	if(number == nil)
		return 'invalid'
	end
	if(number =~ PATTERN_VISA) != nil
		return 'visa'
	end

	if(number =~ PATTERN_MASTERCARD) != nil
		return 'mastercard'
	end

	if(number =~ PATTERN_AMERICAN_EXPRESS) != nil
		return 'american_express'
	end

	if(number =~ PATTERN_DINERS_CLUB)
		return 'diners'
	end
	if(number =~ PATTERN_DISCOVER)
		return 'discover'
	end
	if(number =~ PATTERN_JCB)
		return 'jcb'
	end
	return 'unknown'		
end

Instance Method Details

#isValidCardObject



26
27
28
29
30
31
32
33
# File 'lib/paystack/objects/card.rb', line 26

def isValidCard()
   	if (@cvc != nil) 
     		return isValidNumber() && isValidExpiryDate() && isValidCVC()
   	else 
     		return isValidNumber() && isValidExpiryDate()
     	end
   
end

#isValidCVCObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/paystack/objects/card.rb', line 90

def isValidCVC
	if(@cvc.eql?(""))
		return false
	end
	cvc = @cvc.strip
	cvc_len = cvc.length

	validLength = ((cvc_len >= 3 && cvc_len <= 4) || (@cardIssuer.eql?('american_express') && cvc_len == 4) ||(!@cardIssuer.eql?('american_express') && cvc_len == 3))

end

#isValidExpiryDateObject



101
102
103
# File 'lib/paystack/objects/card.rb', line 101

def isValidExpiryDate()
	return !(@expiryMonth == nil || @expiryYear == nil) && Utils.hasCardExpired(@expiryYear, @expiryMonth);
end

#isValidNumberObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/paystack/objects/card.rb', line 68

def isValidNumber
	if(Utils.isEmpty(@number)) 
		return false
	end
	formatted_number = @number.gsub(/\s+|-/) {|s| '' }.strip
	
	if(Utils.isEmpty(formatted_number) || !Utils.isWholePositiveNumber(formatted_number) || !Utils.isLuhnValidNumber(formatted_number))

		return false
	end
	if PaystackCard.getCardType(formatted_number).eql?('diners') 
		return (formatted_number.length == MAX_DINERS_CARD_LENGTH)
	end

	if PaystackCard.getCardType(formatted_number).eql?('american_express') 
		return (formatted_number.length == MAX_AMERICAN_EXPRESS_CARD_LENGTH)
	end

	return (formatted_number.length == MAX_NORMAL_CARD_LENGTH)

end