module MpApi class Card attr_reader :token, :customer_id, :external_id, :cardholder_name, :expiration_month, :expiration_year, :first_six_digits, :last_four_digits, :issuer_id, :mp_payment_method_id, :error, :error_detail def initialize(token: nil, customer_id: nil, external_id: nil, cardholder_name: nil, expiration_month: nil, expiration_year: nil, first_six_digits: nil, last_four_digits: nil, issuer_id: nil, mp_payment_method_id: nil, error: nil, error_detail: nil) @token = token @customer_id = customer_id @external_id = external_id @cardholder_name = cardholder_name @expiration_month = expiration_month @expiration_year = expiration_year @first_six_digits = first_six_digits @last_four_digits = last_four_digits @issuer_id = issuer_id @mp_payment_method_id = mp_payment_method_id @error = error @error_detail = error_detail end def build_json { token: token } end def create response = Client.new.create_card(customer_id, JSON.dump(build_json)) self.class.new(**self.class.build_hash(response.json)) end def self.build_hash(response) { external_id: response.dig("id"), cardholder_name: response.dig("cardholder", "name"), expiration_month: response.dig("expiration_month"), expiration_year: response.dig("expiration_year"), first_six_digits: response.dig("first_six_digits"), last_four_digits: response.dig("last_four_digits"), issuer_id: response.dig("issuer", "id"), mp_payment_method_id: response.dig("payment_method", "id"), error: response.dig("message"), error_detail: response.dig("cause") } end end end