class Endicia::Refund

Public Instance Methods

request_refund(tracking_numbers, options = {}) click to toggle source

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

# File lib/endicia_ruby/refund.rb, line 17
def request_refund(tracking_numbers, options = {})
  raise StandardError.new('Not supported.')

  # 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