module MpApi class PaymentMethod attr_reader :payment_method_id, :issuer_id, :installments, :errors def initialize(payment_method_id: , issuer_id: , installments:, errors:nil) @payment_method_id = payment_method_id @issuer_id = issuer_id @installments = installments @errors = errors end def self.find_by_first_six_digits(first_six_digits, credit: true) response = Client.new.search_payment_methods(build_query(first_six_digits)) new(**build_hash(response.json, credit)) end def self.build_query(first_six_digits) { marketplace: 'NONE', status: 'active', bins: first_six_digits } end def self.build_hash(response, credit) payment_type_id = credit ? 'credit_card' : 'debit_card' payment_method = response.dig('results')&.find {|pm| pm['payment_type_id'] == payment_type_id} { payment_method_id: payment_method&.dig('id'), issuer_id: payment_method&.dig('issuer', 'id'), installments: payment_method&.dig('payer_costs'), errors: response.dig('message') } end end end