./lib/ews/transaction/validator.rb in exact4r-0.9.1 vs ./lib/ews/transaction/validator.rb in exact4r-0.9.2

- old
+ new

@@ -28,20 +28,34 @@ }.freeze unless defined?(@@valid_lengths) def valid? @errors = {} - validate_lengths - - validate_mandatory_fields - append_error(:transaction_type, "transaction_type must be supplied") if self.transaction_type.blank? # need to authenticate append_error(:gateway_id, "gateway_id must be supplied") if self.gateway_id.blank? append_error(:password, "password must be supplied") if self.password.blank? + validate_lengths + + if self.transaction_type == "CR" + append_error(:transaction_tag, "transaction_tag must be supplied") if self.transaction_tag.to_i < 1 + elsif %w(50 54).include?(self.transaction_type) + validate_for_pan + elsif !self.transaction_tag.blank? + validate_for_transaction_tag + elsif !self.cc_number.blank? + validate_for_cc_number + elsif !self.track1.blank? + validate_for_track1 + elsif !self.track2.blank? + validate_for_track2 + else + append_error(:base, "One of the following must be supplied: cc_number, track1, track2 or transaction_tag.") + end + # ensure we've been given valid amounts append_error(:amount, "invalid amount supplied") unless valid_amount?(self.amount) 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) @@ -65,25 +79,10 @@ 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) end end - # which fields are mandatory and which optional depends on the transaction_type and - # also how the credit card information is supplied. - # - # it can be supplied either - # 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 - 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 @@ -141,23 +140,19 @@ end end end # validate presence of mandatory fields when cc_number present - def validate_for_card + def validate_for_cc_number 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) + append_error(:cc_number, "cc_number must not be set for debit transactions") if [50,54].include?(tt) # amount, cardholder_name always mandaory - mandatory = [:amount, :cardholder_name] - - # card_number & expiry_date mandatory for all except 50, 54 - # pan mandatory for only 50, 54 - mandatory << ([50,54].include?(tt) ? :pan : [:cc_number, :cc_expiry]) - mandatory.flatten! + mandatory = [:amount, :cardholder_name, :cc_number, :cc_expiry] # reference_no mandatory for 60 mandatory << :reference_no if tt == 60 # auth_number mandatory for (02, 03, 11, 12, 13) @@ -174,25 +169,27 @@ # transaction_tag, auth_num & amount mandatory mandatory = [:transaction_tag] mandatory << [:authorization_num, :amount] unless tt == 'CR' + # ensure no cc details sent + append_error(:cc_number, "do not set cc_number for tagged transactions") unless self.cc_number.blank? + append_error(:cc_expiry, "do not set cc_expiry for tagged transactions") unless self.cc_expiry.blank? + append_error(:cardholder_name, "do not set cardholder_name for tagged transactions") unless self.cardholder_name.blank? + check_mandatory(mandatory.flatten) end def validate_for_track1 tt = self.transaction_type.to_i # mandatory: transaction_type must != (30, 31, 32, 34, 35) append_error(:track1, "track1 must not be set for tagged transactions") if [30,31,32,34,35].include?(tt) + append_error(:track1, "track1 must not be set for debit transactions") if [50,54].include?(tt) # amount mandatory for all - mandatory = [:amount] - - # track1 mandatory, except 50,54 - # pan mandatory 50,54 only - mandatory << ([50,54].include?(tt) ? :pan : :track1) + mandatory = [:track1, :amount] # reference_no mandatory for 60 mandatory << :reference_no if tt == 60 # auth_number mandatory for (02, 03, 11, 12, 13) mandatory << :authorization_num if [02, 03, 11, 12, 13].include?(tt) @@ -202,23 +199,36 @@ def validate_for_track2 tt = self.transaction_type.to_i # mandatory: transaction_type must != (30, 31, 32, 34, 35, 50, 54) - append_error(:track2, "track2 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].include?(tt) + append_error(:track2, "track2 must not be set for debit transactions") if [50,54].include?(tt) # track2, expiry_date, cardholder_name, amount mandatory - mandatory = [:track2, :cc_expiry, :cardholder_name, :amount] + mandatory = [:track2, :cardholder_name, :amount] # auth_number mandatory for (02, 03, 11, 12, 13) mandatory << :authorization_num if [02, 03, 11, 12, 13].include?(tt) check_mandatory(mandatory) end + def validate_for_pan + tt = self.transaction_type.to_i + + # mandatory: transaction_type must == (50, 54) + append_error(:pan, "pan must not be set for non-debit transactions") unless [50,54].include?(tt) + + # track2, expiry_date, cardholder_name, amount mandatory + mandatory = [:pan, :cardholder_name, :amount] + + check_mandatory(mandatory) + end + def check_mandatory(mandatory) mandatory.each do |key| - append_error(key, "#{key} is required") if self.send(key).blank? + append_error(key, "#{key} must be supplied") if self.send(key).blank? end end end end end \ No newline at end of file