module RubyPsigate class AccountManagerApi include RubyPsigate::Utils TEST_URL = 'https://dev.psigate.com:8645/Messenger/AMMessenger' LIVE_URL = nil # TODO attr_reader :action attr_accessor :test def initialize(options = {}) requires!(options, :cid, :login, :password) @options = options @live_url = @options[:live_url] end def test? @live_url.blank? end # Registers a new account # # Options (* asterisk denotes required) # => :name* # => :company # => :address1* # => :address2 # => :city* # => :province* # => :postal_code* # => :country* # => :phone* # => :fax # => :email* # => :comments # => :card_holder* # => :card_number* # => :card_exp_month* # => :card_exp_year* # def register(options = {}) requires!(options, :name, :address1, :city, :province, :postal_code, :country, :phone, :email, :card_holder, :card_number, :card_exp_month, :card_exp_year ) @register_options = options @action = "AMA01" end # Updates an already registered account def update(options = {}) @action = "AMA02" end # Retrieves a summary of a registered account def retrieve_summary(options = {}) requires!(options, :account_id) @options.update(options) @action = "AMA00" @params = { :Request => { :CID => @options[:cid], :UserID => @options[:login], :Password => @options[:password], :Action => @action, :Condition => { :AccountID => @options[:account_id] } } } @data = data_to_be_posted(@params) raw_response = post_to_server(@data) response = Response.new(successful?(raw_response), message_from(raw_response), raw_response, :test => test?) end def enable(options = {}) @action = "AMA08" end def disable(options = {}) @action = "AMA09" end def charge(options = {}) end private def data_to_be_posted(params) data = Serializer.new(params) data.to_xml end def post_to_server(data) endpoint = test? ? TEST_URL : @live_url psigate = ActiveMerchant::Connection.new(endpoint) # Some configurations for ActiveMerchant::Connection instance object psigate.verify_peer = true psigate.retry_safe = false psigate.open_timeout = 60 psigate.read_timeout = 60 # Since all of Psigate's requests are POST requests, we will use only the following psigate.request(:post, data) end def successful?(response) response[:approved] == "APPROVED" end def message_from(response) if response[:approved] == "APPROVED" return SUCCESS_MESSAGE else return FAILURE_MESSAGE if response[:errmsg].blank? return response[:errmsg].gsub(/[^\w]/, ' ').split.join(" ").capitalize end end end end