Sha256: e03f52cd695fdec99adc732c70ae100f88f35d7f5e09501dbd7d9664a48a2a74

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module WirecardSepa
  module DirectDebit
    class Response
      attr_reader :xml, :request

      def self.for_request(request)
        new(request.body, request: request)
      end

      def initialize(xml, request: nil)
        @xml = xml
        @request = request
      end

      def params
        {
          success:               success?,
          transaction_id:        transaction_id,
          transaction_state:     transaction_state,
          status_code:           status_code,
          status_description:    status_description,
          due_date:              due_date,
          reference_id:          provider_transaction_reference_id,
          original_response_xml: xml,
          requested_amount:      requested_amount
        }
      end

      def success?
        transaction_state == 'success'
      end

      def transaction_id
        value_at 'transaction-id'
      end

      def transaction_state
        value_at 'transaction-state'
      end

      def status_code
        value_at 'status', attribute: :code
      end

      def status_description
        value_at 'status', attribute: :description
      end

      def due_date
        value_at 'due-date'
      end

      def requested_amount
        value_at 'requested-amount'
      end

      def provider_transaction_reference_id
        value_at 'provider-transaction-reference-id'
      end

      def to_s
        xml
      end

      private

      # Returns the text of a node with the given position. If
      # an additional attribute is given, this attribute is returned
      # instead.
      # This method provides mainly nil-safeness.
      def value_at(position, attribute: nil)
        node = xml_doc.at_css(position)
        node or return
        attribute ? node[attribute] : node.text
      end

      def xml_doc
        @xml_doc ||= Nokogiri::XML xml
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wirecard_sepa-0.1.7 lib/wirecard_sepa/direct_debit/response.rb