./lib/ews/transaction/request.rb in exact4r-0.5.2 vs ./lib/ews/transaction/request.rb in exact4r-0.6

- old
+ new

@@ -1,5 +1,6 @@ +require 'ews/transaction/validator' module EWS # :nodoc: module Transaction # :nodoc: # The Request class allows you to build transaction requests for # submission to the E-xact Web Service. @@ -16,11 +17,11 @@ # Different transaction types will have their own additional requirements when it comes to # mandatory and optional fields, and it is recommended that the E-xact Web Service Programming # Reference Guide, v8.5 be consulted. This document is contained in the Webservice Plugin ZIP file: # http://www.e-xact.com/wp-content/uploads/2007/06/E-xact_Payment_Webservice_Plug-In.zip # - # Please note that, if your chosen transaction requires it, credit card expiry dates *must* be entered in YYMM format. + # Please note that, if your chosen transaction requires it, credit card expiry dates *must* be entered in MMYY format. # # =Allowable transaction types # :purchase # :pre_auth # :pre_auth_completion @@ -42,15 +43,17 @@ # :idebit_refund # :secure_storage # :secure_storage_eft # :transaction_details class Request + include Validator + # yeah, it's ugly, but otherwise RDoc won't pick them up attr_accessor :errors - attr_accessor :gateway_id, :password, :transaction_type, :amount, :surcharge_amount, :cc_number, :transaction_tag, :track1, :track2, :pan, :auth_number, :cc_expiry, :cardholder_name + attr_accessor :gateway_id, :password, :transaction_type, :amount, :surcharge_amount, :cc_number, :transaction_tag, :track1, :track2, :pan, :authorization_num, :cc_expiry, :cardholder_name attr_accessor :cc_verification_str1, :cc_verification_str2, :cvd_presence_ind, :tax1_amount, :tax1_number, :tax2_amount, :tax2_number, :secure_auth_required, :secure_auth_result - attr_accessor :ecommerce_flag, :xid, :cavv, :cavv_algorithm, :reference_no, :customer_ref, :reference_3, :language, :client_ip, :client_email, :user_name + attr_accessor :ecommerce_flag, :xid, :cavv, :cavv_algorithm, :reference_no, :customer_ref, :reference_3, :language, :client_ip, :client_email, :zip_code # Initialize a Request with a hash of values def initialize(hash = {}) hash.each {|k,v| self.send "#{k.to_s}=", v} @errors = {} @@ -60,74 +63,23 @@ # you can use either <tt>:purchase</tt> or <tt>'00'</tt> def transaction_type=(type_sym) # assume we're given a symbol, so look up the code value = @@transaction_codes[type_sym] # if nothing found, then maybe we were given an actual code? - value = type_sym if value.nil? and @@transaction_codes.values.include?(type_sym) + if(value.nil?) + raise "invalid transaction_type supplied #{type_sym}" unless @@transaction_codes.values.include?(type_sym) + value = type_sym + end @transaction_type = value end # Indicates whether or not this transaction is a <tt>:transaction_details</tt> transaction def is_find_transaction? self.transaction_type == "CR" end - def valid? - errors[:transaction_type] = "transaction_type must be supplied" if self.transaction_type.blank? - errors[:transaction_type] = "invalid transaction_type supplied" unless @@transaction_codes.values.include? self.transaction_type - - # need to authenticate - errors[:gateway_id] = "gateway_id must be supplied" if self.gateway_id.blank? - errors[:password] = "password must be supplied" if self.password.blank? - - # ensure we've been given valid amounts - errors[:amount] = "invalid amount supplied" unless valid_amount?(self.amount) - errors[:surcharge_amount] = "invalid surcharge_amount supplied" unless valid_amount?(self.surcharge_amount) - errors[:tax1_amount] = "invalid tax1_amount supplied" unless valid_amount?(self.tax1_amount) - errors[:tax2_amount] = "invalid tax2_amount supplied" unless valid_amount?(self.tax2_amount) - - errors[:cc_number] = "invalid cc_number supplied" unless valid_card_number? - errors[:cc_expiry] = "invalid cc_expiry supplied" unless valid_expiry_date? - - errors.empty? - end - private - def valid_amount?(amount) - return true if amount.blank? - - (amount.class == Float) or (amount.class == Fixnum) or !amount.match(/[^0-9.]/) - end - - def valid_card_number? - return true if self.cc_number.blank? - - # do a mod10 check - weight = 1 - card_number = self.cc_number.scan(/./).map(&:to_i) - result = card_number.reverse[1..-1].inject(0) do |sum, num| - weight = 1 + weight%2 - digit = num * weight - sum += (digit / 10) + (digit % 10) - end - card_number.last == (10 - result % 10 ) % 10 - end - - def valid_expiry_date? - return true if self.cc_expiry.blank? - - # check format - return false unless self.cc_expiry.match(/^\d{4}$/) - - # check date is not in past - year, month = 2000 + self.cc_expiry[0..1].to_i, self.cc_expiry[2..3].to_i - - # 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) - end @@transaction_codes = { :purchase => '00', :pre_auth => '01', :pre_auth_completion => '02', \ No newline at end of file