Sha256: 9be13151cee9c75e02bd6a5f4a61d5501a278f8de10ddbc8adf915d01ec84551

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Bs2Api
  module Entities
    class Payment
      attr_accessor :payment_id, :end_to_end_id, :receiver, :payer

      def initialize(args = {})
        @payment_id    = args.fetch(:payment_id, nil)
        @end_to_end_id = args.fetch(:end_to_end_id, nil)
        @receiver      = args.fetch(:receiver, nil)
        @payer         = args.fetch(:payer, nil)
      end

      def to_hash
        hash_data = {
          "pagamentoId": @payment_id,
          "endToEndId":  @end_to_end_id
        }

        hash_data.merge!({ "recebedor": @receiver.to_hash } ) if @receiver.present?
        hash_data.merge!({ "pagador": @payer.to_hash } ) if @payer.present?
        ActiveSupport::HashWithIndifferentAccess.new(hash_data)
      end

      def self.from_response(hash_payload)
        hash = ActiveSupport::HashWithIndifferentAccess.new(hash_payload)

        Bs2Api::Entities::Payment.new(
          payment_id:    hash["pagamentoId"] || hash["cobranca"]["id"],
          end_to_end_id: hash["endToEndId"],
          receiver:      Bs2Api::Entities::Bank.from_response(hash["recebedor"]),
          payer:         Bs2Api::Entities::Bank.from_response(hash["pagador"])
        )
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bs2_api-1.1.7 lib/bs2_api/entities/payment.rb
bs2_api-1.1.6 lib/bs2_api/entities/payment.rb
bs2_api-1.1.5 lib/bs2_api/entities/payment.rb
bs2_api-1.1.4 lib/bs2_api/entities/payment.rb
bs2_api-1.1.3 lib/bs2_api/entities/payment.rb
bs2_api-1.1.2 lib/bs2_api/entities/payment.rb
bs2_api-1.1.1 lib/bs2_api/entities/payment.rb
bs2_api-1.1.0 lib/bs2_api/entities/payment.rb