Class: EWS::Transaction::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/ews/transaction/mapping.rb

Overview

This class handles encoding of transaction requests to the various transport formats, and the decoding of responses to transaction response objects.

The supported formats are:

* REST/JSON
* REST/XML
* SOAP/XML

Constant Summary collapse

XML_REQUEST_TAGS_TO_ATTRS =

:nodoc:

{
  :Authorization_Num => :authorization_num,
  :CardHoldersName => :cardholder_name,
  :Card_Number => :cc_number,
  :CAVV => :cavv,
  :CAVV_Algorithm => :cavv_algorithm,
  :Client_Email => :client_email,
  :Client_IP => :client_ip,
  :Customer_Ref => :customer_ref,
  :CVD_Presence_Ind => :cvd_presence_ind,
  :DollarAmount => :amount,
  :Ecommerce_Flag => :ecommerce_flag,
  :ExactID => :gateway_id,
  :Expiry_Date => :cc_expiry,
  :Language => :language,
  :PAN => :pan,
  :Password => :password,
  :Reference_3 => :reference_3,
  :Reference_No => :reference_no,
  :Secure_AuthRequired => :secure_auth_required,
  :Secure_AuthResult => :secure_auth_result,
  :SurchargeAmount => :surcharge_amount,
  :Tax1Amount => :tax1_amount,
  :Tax1Number => :tax1_number,
  :Tax2Amount => :tax2_amount,
  :Tax2Number => :tax2_number,
  :Track1 => :track1,
  :Track2 => :track2,
  :Transaction_Tag => :transaction_tag,
  :Transaction_Type => :transaction_type,
  :VerificationStr1 => :cc_verification_str1,
  :VerificationStr2 => :cc_verification_str2,
  :User_Name => :user_name,
  :XID => :xid,
  :ZipCode => :zip_code
}
XML_RESPONSE_TAGS_TO_ATTRS =
{
  :AVS => :avs,
  :Bank_Message => :bank_message,
  :Bank_Resp_Code => :bank_resp_code,
  :Bank_Resp_Code_2 => :bank_resp_code_2,
  :CAVV_Response => :cavv_response,
  :CTR => :ctr,
  :CVV2 => :cvv2,
  :Error_Description => :error_description,
  :Error_Number =>  :error_number,
  :EXact_Message => :exact_message,
  :EXact_Resp_Code => :exact_resp_code,
  :LogonMessage => :logon_message,
  :MerchantAddress => :merchant_address,
  :MerchantCity => :merchant_city,
  :MerchantCountry => :merchant_country,
  :MerchantName => :merchant_name,
  :MerchantPostal => :merchant_postal,
  :MerchantProvince => :merchant_province,
  :MerchantURL => :merchant_url,
  :Retrieval_Ref_No => :retrieval_ref_no,
  :SequenceNo => :sequence_no,
  :Transaction_Approved => :transaction_approved,
  :Transaction_Error => :transaction_error
}.merge(XML_REQUEST_TAGS_TO_ATTRS)

Class Method Summary collapse

Class Method Details

.json_to_response(content) ⇒ Object



107
108
109
110
111
# File 'lib/ews/transaction/mapping.rb', line 107

def self.json_to_response(content)
  response = EWS::Transaction::Response.new
  ActiveSupport::JSON.decode(content).each { |k,v| response.send "#{k}=", v if response.respond_to?("#{k}=") }
  response
end

.request_to_json(request) ⇒ Object



79
80
81
# File 'lib/ews/transaction/mapping.rb', line 79

def self.request_to_json(request)
  request.to_json
end

.request_to_rest(request) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/ews/transaction/mapping.rb', line 82

def self.request_to_rest(request)
  xml = Builder::XmlMarkup.new

  xml.instruct!
  xml.tag! 'Transaction' do
    add_request_hash(xml, request)
  end
  xml.target!
end

.request_to_soap(request) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ews/transaction/mapping.rb', line 91

def self.request_to_soap(request)
  xml = Builder::XmlMarkup.new(:indent => 2)

  xml.instruct!
  xml.tag! 'soap:Envelope', REQUEST_ENVELOPE_NAMESPACES do
    xml.tag! 'soap:Body' do
      xml.tag! 'n1:SendAndCommit', REQUEST_SAC_ATTRIBUTES do
        xml.tag! 'SendAndCommitSource', REQUEST_SAC_SOURCE_ATTRIBUTES do
          add_request_hash(xml, request)
        end
      end
    end
  end
  xml.target!
end

.rest_to_response(content) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/ews/transaction/mapping.rb', line 112

def self.rest_to_response(content)
  response = EWS::Transaction::Response.new
  xml = REXML::Document.new(content)
  root = REXML::XPath.first(xml, "//TransactionResult")
  response_xml_string_to_hash(response, root) if root
  response
end

.soap_to_response(content) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ews/transaction/mapping.rb', line 119

def self.soap_to_response(content)
  response = EWS::Transaction::Response.new
  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