module ZuoraAPI class Basic < Login attr_accessor :username, :password def initialize(username: nil, password: nil, **keyword_args) @username = username @password = password super end def new_session( auth_type: :basic, debug: false) raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Basic Login, does not support Authentication of Type: #{auth_type}") if auth_type != :basic raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Basic Login requires username and password to be set.") if auth_type == :basic && (self.password.blank? && self.username.blank?) tries ||= 2 request = Nokogiri::XML::Builder.new do |xml| xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' =>"http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:api' => "http://api.zuora.com/" ) do xml['SOAP-ENV'].Header xml['SOAP-ENV'].Body do xml['api'].login do xml['api'].username self.username xml['api'].password self.password xml['api'].entityId self.entity_id if !self.entity_id.blank? end end end end input_xml = Nokogiri::XML(request.to_xml(:save_with => XML_SAVE_OPTIONS).strip) input_xml.xpath('//ns1:session', 'ns1' =>'http://api.zuora.com/').children.remove Rails.logger.debug('Connect') {"Request Code: #{@response_query.code} SOAP XML: #{input_xml.to_xml(:save_with => XML_SAVE_OPTIONS).strip}"} if debug @response_query = HTTParty.post(self.url,:body => request.to_xml(:save_with => XML_SAVE_OPTIONS).strip, :headers => {'Content-Type' => "text/xml; charset=utf-8"}, :timeout => 10) @output_xml = Nokogiri::XML(@response_query.body) Rails.logger.debug('Connect') {"Response Code: #{@response_query.code} SOAP XML: #{@output_xml.to_xml(:save_with => XML_SAVE_OPTIONS).strip}"} if debug if !@response_query.success? self.current_session = nil if @output_xml.namespaces.size > 0 && @output_xml.xpath('//soapenv:Fault').size > 0 self.current_error = @output_xml.xpath('//fns:FaultMessage', 'fns' =>'http://fault.api.zuora.com/').text if self.current_error.include?('deactivated') self.status = 'Deactivated' self.current_error = 'Deactivated user login, please check with Zuora tenant administrator' self.errors[:username] = self.current_error elsif self.current_error.include?('inactive') self.status = 'Inactive' self.current_error = 'Inactive user login, please check with Zuora tenant administrator' self.errors[:username] = self.current_error elsif self.current_error.include?("invalid username or password") || self.current_error.include?("Invalid login. User name and password do not match.") self.status = 'Invalid Login' self.current_error = 'Invalid login, please check username and password or URL endpoint' self.errors[:username] = self.current_error self.errors[:password] = self.current_error elsif self.current_error.include?('unsupported version') self.status = 'Unsupported API Version' self.current_error = 'Unsupported API version, please verify URL endpoint' self.errors[:url] = self.current_error elsif self.current_error.include?('invalid api version') self.status = 'Invalid API Version' self.current_error = 'Invalid API version, please verify URL endpoint' self.errors[:url] = self.current_error elsif self.current_error.include?('invalid session') self.status = 'Invalid Session' self.current_error = 'Session invalid, please update session and verify URL endpoint' self.errors[:session] = self.current_error elsif self.current_error.include?('Your IP address') self.status = 'Restricted IP' self.current_error = 'IP restricted, contact Zuora tenant administrator and remove IP restriction' self.errors[:base] = self.current_error elsif self.current_error.include?('This account has been locked') self.status = 'Locked' self.current_error = 'Locked user login, please wait or navigate to Zuora to unlock user' self.errors[:username] = self.current_error elsif self.current_error.include?('Entity not exist:') self.status = 'Entity Missing' self.errors[:base] = self.current_error else self.status = 'Unknown' self.current_error = @output_xml.xpath('//faultstring').text if self.current_error.blank? self.errors[:base] = self.current_error end else self.status = 'Unknown' self.current_error = @output_xml.xpath('//faultstring').text if self.current_error.blank? self.errors[:base] = self.current_error end else #Username & password combo retrieved_session = @output_xml.xpath('//ns1:Session', 'ns1' =>'http://api.zuora.com/').text raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("No session found for api call.") if retrieved_session.blank? self.status = 'Active' self.current_session = retrieved_session end return self.status rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => ex if !(tries -= 1).zero? Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"} sleep(self.timeout_sleep) retry else self.current_error = "Request timed out. Try again" self.status = 'Timeout' return self.status end rescue EOFError if self.url.match?(/.*services\d{1,}.zuora.com*/) self.current_error = "Services tenant '#{self.url.scan(/.*\/\/(services\d{1,}).zuora.com*/).last.first}' is no longer available." self.status = 'Not Available' return self.status end end end end