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

- old
+ new

@@ -22,16 +22,17 @@ :Card_Number => :cc_number, :Transaction_Tag => :transaction_tag, :Track1 => :track1, :Track2 => :track2, :PAN => :pan, - :Authorization_Num => :auth_number, + :Authorization_Num => :authorization_num, :Expiry_Date => :cc_expiry, :CardHoldersName => :cardholder_name, :VerificationStr1 => :cc_verification_str1, :VerificationStr2 => :cc_verification_str2, :CVD_Presence_Ind => :cvd_presence_ind, + :ZipCode => :zip_code, :Tax1Amount => :tax1_amount, :Tax1Number => :tax1_number, :Tax2Amount => :tax2_amount, :Tax2Number => :tax2_number, :Secure_AuthRequired => :secure_auth_required, @@ -43,12 +44,11 @@ :Reference_No => :reference_no, :Customer_Ref => :customer_ref, :Reference_3 => :reference_3, :Language => :language, :Client_IP => :client_ip, - :Client_Email => :client_email, - :User_Name => :user_name + :Client_Email => :client_email } unless defined?(XML_REQUEST_TAGS_TO_ATTRS) XML_RESPONSE_TAGS_TO_ATTRS = { :LogonMessage => :logon_message, :Error_Number => :error_number, @@ -70,11 +70,11 @@ :MerchantCity => :merchant_city, :MerchantProvince => :merchant_province, :MerchantCountry => :merchant_country, :MerchantPostal => :merchant_postal, :MerchantURL => :merchant_url, - :CTR => :CTR + :CTR => :ctr }.merge(XML_REQUEST_TAGS_TO_ATTRS) unless defined?(XML_RESPONSE_TAGS_TO_ATTRS) def self.request_to_json(request) request.to_json end @@ -108,16 +108,42 @@ ActiveSupport::JSON.decode(content).each { |k,v| response.send "#{k}=", v if response.respond_to?(k) } response end def self.rest_to_response(content) response = EWS::Transaction::Response.new - response_xml_string_to_hash(response, content) + xml = REXML::Document.new(content) + root = REXML::XPath.first(xml, "//TransactionResult") + response_xml_string_to_hash(response, root) if root response end def self.soap_to_response(content) response = EWS::Transaction::Response.new - response_xml_string_to_hash(response, content, xpath = "//types:TransactionResult") + xml = REXML::Document.new(content) + root = REXML::XPath.first(xml, "//types:TransactionResult") + if root + # we have a normal response + response_xml_string_to_hash(response, root) + else + # check if we have an error response + faultErrorRoot = REXML::XPath.first(xml, "//soap:Fault") + unless faultErrorRoot.nil? + # if we do, then see if we have a details section + detailRoot = REXML::XPath.first(faultErrorRoot, "detail") + if detailRoot.nil? or !detailRoot.has_elements? + # no details section, so we have an XML parsing error and should raise an exception + faultString = REXML::XPath.first(faultErrorRoot, "faultstring") + raise faultString.text + else + errorElem = REXML::XPath.first(detailRoot, "error") + # do have details, so figure out the error_number and error_description + errorNumElem = errorElem.attribute("number") + response.error_number = errorNumElem.value.to_i unless errorNumElem.nil? + errorDescElem = errorElem.attribute("description") + response.error_description = errorDescElem.value unless errorDescElem.nil? + end + end + end response end private @@ -126,18 +152,15 @@ XML_REQUEST_TAGS_TO_ATTRS.each do |k, v| xml.tag! k.to_s, request.send(v.to_s) end end - # parses xml response string into the response attributes. The XPath identifies the root element - # of the payload. For REST (default) this is "//TransactionResult", for SOAP this is name space - # prefixed "//types:TransactionResult". - def self.response_xml_string_to_hash(response, xml_string, xpath = "//TransactionResult") - xml = REXML::Document.new(xml_string) - if root = REXML::XPath.first(xml, xpath) - root.elements.to_a.each do |node| - gwlib_prop_name = XML_RESPONSE_TAGS_TO_ATTRS[node.name.to_sym] - logger.warn("No mapping for the tag #{node.name}.") if gwlib_prop_name.nil? + # parses xml response elements into the response attributes + def self.response_xml_string_to_hash(response, root) + root.elements.to_a.each do |node| + gwlib_prop_name = XML_RESPONSE_TAGS_TO_ATTRS[node.name.to_sym] + unless gwlib_prop_name.nil? + # todo: should notiy this somehow?? value = (ATTR_FORMATS.include?(gwlib_prop_name)) ? node.text.send("to_#{ATTR_FORMATS[gwlib_prop_name]}") : node.text response.send "#{gwlib_prop_name}=", value end end end \ No newline at end of file