./lib/ews/transaction/validator.rb in exact4r-0.6 vs ./lib/ews/transaction/validator.rb in exact4r-0.7
- old
+ new
@@ -45,14 +45,14 @@
append_error(:surcharge_amount, "invalid surcharge_amount supplied") unless valid_amount?(self.surcharge_amount)
append_error(:tax1_amount, "invalid tax1_amount supplied") unless valid_amount?(self.tax1_amount)
append_error(:tax2_amount, "invalid tax2_amount supplied") unless valid_amount?(self.tax2_amount)
# ensure our amounts are within range
- append_error(:amount, "amount must be between 0.00 and 99999.99") unless amount_too_big?(self.amount)
- append_error(:surcharge_amount, "amount must be between 0.00 and 99999.99") unless amount_too_big?(self.surcharge_amount)
- append_error(:tax1_amount, "amount must be between 0.00 and 99999.99") unless amount_too_big?(self.tax1_amount)
- append_error(:tax2_amount, "amount must be between 0.00 and 99999.99") unless amount_too_big?(self.tax2_amount)
+ append_error(:amount, "amount must be between 0.00 and 99999.99") unless amount_in_range?(self.amount)
+ append_error(:surcharge_amount, "amount must be between 0.00 and 99999.99") unless amount_in_range?(self.surcharge_amount)
+ append_error(:tax1_amount, "amount must be between 0.00 and 99999.99") unless amount_in_range?(self.tax1_amount)
+ append_error(:tax2_amount, "amount must be between 0.00 and 99999.99") unless amount_in_range?(self.tax2_amount)
# ensure our credit card information is valid
append_error(:cc_number, "invalid cc_number supplied") unless valid_card_number?
append_error(:cc_expiry, "invalid cc_expiry supplied") unless valid_expiry_date?
@@ -61,11 +61,11 @@
private
def validate_lengths
@@valid_lengths.each do |k,len|
value = self.send k
- append_error(k, "#{k.to_s} is too long. maximum allowed length is #{len} characters") unless value.nil? or (value.length <= len)
+ append_error(k, "#{k.to_s} is too long. Maximum allowed length is #{len} characters") unless value.nil? or (value.length <= len)
end
end
# which fields are mandatory and which optional depends on the transaction_type and
# also how the credit card information is supplied.
@@ -74,27 +74,22 @@
# a) via the cc_number field
# b) via a tagged transaction
# c) encoded in a track1 value, or
# d) encoded in a track2 value
def validate_mandatory_fields
- if !self.cc_number.blank?
- validate_for_card
- elsif !self.transaction_tag.blank?
- validate_for_transaction_tag
- elsif !self.track1.blank?
- validate_for_track1
- elsif !self.track2.blank?
- validate_for_track2
- end
+ validate_for_card unless self.cc_number.blank?
+ validate_for_transaction_tag unless self.transaction_tag.blank?
+ validate_for_track1 unless self.track1.blank?
+ validate_for_track2 unless self.track2.blank?
end
def valid_amount?(amount)
return true if amount.blank?
((amount.class == Float) or (amount.class == Fixnum) or !amount.match(/[^0-9.]/))
end
- def amount_too_big?(amount)
+ def amount_in_range?(amount)
return true if amount.blank?
return ((amount.to_f <= 99999.99) and (amount.to_f >= 0.0))
end
@@ -122,11 +117,12 @@
# check format
return false unless self.cc_expiry.match(/^\d{4}$/)
# check date is not in past
- year, month = 2000 + self.cc_expiry[2..3].to_i, self.cc_expiry[0..1].to_i
+ year, month = self.cc_expiry[2..3].to_i, self.cc_expiry[0..1].to_i
+ year += (year > 79) ? 1900 : 2000
# CC is still considered valid during the month of expiry,
# so just compare year and month, ignoring the rest.
now = DateTime.now
return ((1..12) === month) && DateTime.new(year, month) >= DateTime.new(now.year, now.month)
@@ -144,11 +140,11 @@
@errors[key] << message
end
end
end
- # validate presence of mandatort fields when cc_number present
+ # validate presence of mandatory fields when cc_number present
def validate_for_card
tt = self.transaction_type.to_i
# mandatory: transaction_type must != (30, 31, 32, 34, 35)
append_error(:cc_number, "cc_number must not be set for tagged transactions") if [30,31,32,34,35].include?(tt)
@@ -206,10 +202,10 @@
def validate_for_track2
tt = self.transaction_type.to_i
# mandatory: transaction_type must != (30, 31, 32, 34, 35, 50, 54)
- append_error(:track1, "track1 must not be set for tagged transactions") if [30,31,32,34,35,50,54].include?(tt)
+ append_error(:track2, "track2 must not be set for tagged transactions") if [30,31,32,34,35,50,54].include?(tt)
# track2, expiry_date, cardholder_name, amount mandatory
mandatory = [:track2, :cc_expiry, :cardholder_name, :amount]
# auth_number mandatory for (02, 03, 11, 12, 13)
\ No newline at end of file