module RubyPsigate class InvalidOrder < RubyPsigateError; end class InvalidAccount < RubyPsigateError; end class InvalidGatewayMode < RubyPsigateError; end class InvalidCredentials < RubyPsigateError; end class InvalidCreditCard < RubyPsigateError; end class IncompleteParameters < RubyPsigateError; end class Gateway # Class defaults to @env = :test def self.env @env end def self.live! @env = :live end def self.test! @env = :test end include ::RubyPsigate::TransactionMethods # include RubyPsigate::AccountManagerMethods attr_reader :mode, :order, :options, :account def initialize(options={}) @options = options set_mode(@options) end def mode=(type) valid_modes = %w( transaction account_manager ) raise InvalidGatewayMode unless valid_modes.include?(type.to_s) @mode = type.to_sym end def order=(order_object) raise InvalidOrder unless order_object.is_a?(RubyPsigate::Order) @order = order_object end def account=(account_object) raise InvalidAccount unless account_object.is_a?(RubyPsigate::Account) @account = account_object end TRANSACTION_LOOKUP = { :sale => "0", :preauth => "1", :postauth => "2", :credit => "3", :force_postauth => "4", :void => "9" } def commit! # 1) Check the mode # 2) Ensure credentials are set # 3) Check which action user is wanting to take # 4) Ensure adequate minimum parameters for the given action # 5) Parameterize and ensure it is properly formatted # 6) Send! begin raise InvalidGatewayMode unless mode_set? raise InvalidCredentials unless sufficient_login_credentials? if mode == :transaction raise InvalidOrder unless order.valid? @params = {} @params[:Order] = {} @params[:Order] = { :StoreID => options[:store_id], :Passphrase => options[:passphrase] } @params[:Order].merge!(order.to_hash(order.action)) @endpoint = "https://dev.psigate.com:7989/Messenger/XMLMessenger" end if mode == :account_manager # raise InvalidAccount unless account.valid? # Parameterize based on account manager @params = {} @params[:Request] = {} @params[:Request] = { :CID => options[:cid], :UserID => options[:user_id], :Password => options[:password] } @params[:Request].merge!(account.to_hash(account.action)) @endpoint = "https://dev.psigate.com:8645/Messenger/AMMessenger" end # Serializes the parameters to xml @params = Serializer.new(@params, :header => true).to_xml @connection = Connection.new(@endpoint) @response = @connection.post(@params) @response = Response.new(@response) rescue ConnectionError => e @response = false end @response end private def set_mode(options) self.mode = :account_manager if options[:cid] && options[:user_id] && options[:password] self.mode = :transaction if options[:store_id] && options[:passphrase] end def mode_set? (mode == :account_manager || mode == :transaction) ? true : false end def sufficient_login_credentials? result = false if mode == :account_manager result = (options[:cid] && options[:user_id] && options[:password] ? true : false) elsif mode == :transaction result = (options[:store_id] && options[:passphrase] ? true : false) end result end end end