module RubyPsigate class InvalidRecurringCharge < RubyPsigateError; end class Account include AccountMethods include CreditCardMethods # List of actions you can perform on an account and their associated constant codes ACTIONS = { :account => { :summary => "AMA00", :details => "AMA05", :register => "AMA01", :update => "AMA02", :enable => "AMA08", :disable => "AMA09" }, :credit_card => { :add => "AMA11", :delete => "AMA14", :enable => "AMA18", :disable => "AMA19" }, :charges => { :summary => "RBC00", :add => "RBC01", :update => "RBC02", :delete => "RBC04", :details => "RBC05", :enable => "RBC08", :disable => "RBC09", :immediate => "RBC99" } } def self.action_reverse_lookup(code) result = nil result_first = nil result_second = nil ACTIONS.each_pair do |section, actions| break if result_second result_second = actions.select { |k,v| v == code } if result_second result_first = section result_second = result_second.keys[0] end end result = "#{result_first}_#{result_second}".downcase.to_sym end attr_writer :action attr_reader :address, :rbcharge attr_accessor :account_id, :comments, :email, :serial_no, :store_id # For hasahable alias_method :accountid, :account_id def to_hash(type=nil) type = self.class.action_reverse_lookup(type) result = {} result = process_hash(result, type) result end def action ACTIONS[@action.first[0]][@action.first[1]] end def process_hash(result, type) result.merge!({:Action => action}) if type == :account_register result[:Account] = Hash.new result[:Account].merge!(:Email => email) unless email.nil? result[:Account].merge!({:AccountID => nil}) result[:Account].merge!(address.to_hash(:account)) unless address.nil? result[:Account].merge!({ :CardInfo => cc.to_hash(:card_info) }) end if type == :account_update result.merge!({:Condition => { :AccountID => account_id }}) result[:Update] = {} result[:Update].merge!(address.to_hash(:billing)) unless address.nil? result[:Update].merge!(:Email => email) unless email.nil? result[:Update].merge!(:Comments => comments) unless comments.nil? end if type == :account_details || type == :account_enable || type == :account_disable result.merge!({:Condition => { :AccountID => account_id }}) end if type == :credit_card_add result[:Account] = Hash.new result[:Account].merge!({ :AccountID => account_id }) result[:Account].merge!({:CardInfo => cc.to_hash(:card_info)}) end if type == :credit_card_delete || type == :credit_card_enable || type == :credit_card_disable result.merge!({:Condition => { :AccountID => account_id, :SerialNo => serial_no }}) end if type == :charges_summary result[:Condition] = Hash.new result.merge!({:Condition => { :AccountID => account_id, :StoreID => store_id }}) result[:Condition].merge!(rbcharge.to_hash) end if type == :charges_add || type == :charges_immediate result[:Charge] = Hash.new result[:Charge].merge!({:AccountID => account_id, :SerialNo => serial_no}) result[:Charge].merge!(rbcharge.to_hash) end if type == :charges_update result.merge!({:Condition => { :RBCID => rbcharge.rbcid }}) result.merge!({:Update => { :RBTrigger => rbcharge.rbtrigger }}) end if type == :charges_delete || type == :charges_details || type == :charges_enable || type == :charges_disable result.merge!({:Condition => { :RBCID => rbcharge.rbcid }}) end result end # Method used to describe the query to cross reference the account on local and Psigate's servers def condition # AccountID, RBCID, RBTrigger, Type, InvoiceNo, Status, SerialNo { :AccountID => "123" } end def address=(input_address) raise InvalidAddress unless input_address.is_a?(Address) @address = input_address end def rbcharge=(charge) raise InvalidRecurringCharge unless charge.is_a?(RecurringCharge) @rbcharge = charge end end end