Class: Endicia::Refund

Inherits:
Request show all
Defined in:
lib/endicia_ruby/refund.rb

Constant Summary

Constants inherited from Request

Endicia::Request::ENDICIA_API_HOSTS

Instance Method Summary collapse

Methods inherited from Request

#initialize

Constructor Details

This class inherits a constructor from Endicia::Request

Instance Method Details

#request_refund(tracking_numbers, options = {}) ⇒ Object

Request a refund for the given tracking number(s)

tracking_numbers can be an array of strings or a single string

Returns an Endicia::RefundResponse



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/endicia_ruby/refund.rb', line 15

def request_refund(tracking_numbers, options = {})
  # Build the options for this method with passed in values overriding defaults
  options.reverse_merge!(default_options)

  # If we didn't get an array of tracking numbers make it one for simplicity
  tracking_numbers = tracking_numbers.is_a?(Array) ? tracking_numbers : [tracking_numbers]

  # Build the XML document
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.RefundRequest do
      # Add the credentials
      recursive_build_xml_nodes!(xml, @options[:credentials])
      # Add the value for Test since this API is different and wants it as a node and not attribute, apparently
      xml.Test(api_test_mode_value)
      # Add all tracking numbers
      xml.RefundList do |xml|
        tracking_numbers.collect do |tracking_number|
          xml.PICNumber(tracking_number)
        end
      end
    end
  end
  xml_body = builder.to_xml

  # Log the XML of the request if desired
  log("ENDICIA REFUND REQUEST: #{format_xml_for_logging(xml_body)}") if options[:log_requests]

  params = { method: 'RefundRequest', XMLInput: URI.encode(xml_body) }
  raw_response = self.class.get(els_service_url(params))

  # Log the XML of the response if desired
  log("ENDICIA REFUND RESPONSE: #{format_xml_for_logging(raw_response.body)}") if options[:log_responses]

  Endicia::RefundResponse.new(raw_response)

end