lib/mp_api/payment.rb in mp_api-0.3.5 vs lib/mp_api/payment.rb in mp_api-0.3.6

- old
+ new

@@ -1,10 +1,9 @@ module MpApi class Payment - - attr_reader :status_detail, :three_ds_info_creq, :three_ds_info_external_resource_url, :three_d_secure_mode, :id, :description, :date_approved, :date_created, :money_release_date, :payer_email, :payer_identification_type, :payer_identification_number, :payment_method, :payment_type, :qr_code, :qr_code_base_64, :transaction_id, :ticket_url, :status, :amount, :token, :issuer_id, :installments, :statement_descriptor, :items, :errors, :capture, :date_of_expiration - def initialize(status_detail:nil, three_ds_info_creq:nil, three_ds_info_external_resource_url:nil, three_d_secure_mode:nil, id:nil, description:nil, date_approved:nil, date_created:nil, money_release_date:nil, payer_email:, payer_identification_type:, payer_identification_number:, payment_method:, payment_type:nil, qr_code:nil, qr_code_base_64:nil, transaction_id:nil, ticket_url:nil, status:nil, amount:, token:nil, issuer_id:nil, installments:nil, items:nil, statement_descriptor:nil, errors:nil, capture:nil, date_of_expiration:nil) + attr_reader :status_detail, :three_ds_info_creq, :three_ds_info_external_resource_url, :three_d_secure_mode, :id, :description, :date_approved, :date_created, :money_release_date, :payer_email, :payer_identification_type, :payer_identification_number, :payment_method, :payment_type, :qr_code, :qr_code_base_64, :transaction_id, :ticket_url, :status, :amount, :token, :issuer_id, :installments, :statement_descriptor, :items, :error, :internal_error, :capture, :date_of_expiration + def initialize(payer_email:, payer_identification_type:, payer_identification_number:, payment_method:, amount:, status_detail: nil, three_ds_info_creq: nil, three_ds_info_external_resource_url: nil, three_d_secure_mode: nil, id: nil, description: nil, date_approved: nil, date_created: nil, money_release_date: nil, payment_type: nil, qr_code: nil, qr_code_base_64: nil, transaction_id: nil, ticket_url: nil, status: nil, token: nil, issuer_id: nil, installments: nil, items: nil, statement_descriptor: nil, error: nil, internal_error: nil, capture: nil, date_of_expiration: nil) @id = id @description = description @date_approved = date_approved @date_created = date_created @money_release_date = money_release_date @@ -26,23 +25,24 @@ @three_ds_info_external_resource_url = three_ds_info_external_resource_url @three_ds_info_creq = three_ds_info_creq @status_detail = status_detail @statement_descriptor = statement_descriptor @items = items - @errors = errors + @error = error + @internal_error = internal_error @capture = capture @date_of_expiration = date_of_expiration end def build_json - send("build_json_#{payment_method == 'pix' ? 'pix' : 'credit_card'}").except(:description) + send("build_json_#{(payment_method == "pix") ? "pix" : "credit_card"}").except(:description) end def build_update_json { # capture: capture, - status: status, + status: status # date_of_expiration: date_of_expiration, # transaction_amount: amount }.compact end @@ -70,53 +70,55 @@ build_json_pix.merge({ capture: capture, token: token, issuer_id: issuer_id, installments: installments, - three_d_secure_mode: three_d_secure_mode ? 'optional' : 'not_supported' + three_d_secure_mode: three_d_secure_mode ? "optional" : "not_supported" }.compact) end - + def create response = Client.new.create_payment(JSON.dump(build_json)) self.class.new(**self.class.build_hash(response.json)) end - + def update attributes attributes.each do |key, value| instance_variable_set("@#{key}", value) end Client.new.update_payment(id, JSON.dump(build_update_json)) - self.class.find_by_id(self.id) + self.class.find_by_id(id) end def self.find_by_id(payment_id) response = Client.new.get_payment(payment_id) new(**build_hash(response.json)) end def self.build_hash json_response + payment_error = PaymentError.new(json_response: json_response) { - id: json_response.dig('id'), - description: json_response.dig('description'), - date_approved: json_response.dig('date_approved'), - date_created: json_response.dig('date_created'), - money_release_date: json_response.dig('money_release_date'), - payer_email: json_response.dig('payer', 'email'), - payer_identification_type: json_response.dig('payer', 'identification', 'type'), - payer_identification_number: json_response.dig('payer', 'identification', 'number'), - payment_method: json_response.dig('payment_method_id'), - payment_type: json_response.dig('payment_type_id'), - qr_code: json_response.dig('point_of_interaction', 'transaction_data', 'qr_code'), - qr_code_base_64: json_response.dig('point_of_interaction', 'transaction_data', 'qr_code_base64'), - three_ds_info_external_resource_url: json_response.dig('three_ds_info', 'external_resource_url'), - three_ds_info_creq: json_response.dig('three_ds_info', 'creq'), - transaction_id: json_response.dig('point_of_interaction', 'transaction_data', 'transaction_id'), - ticket_url: json_response.dig('point_of_interaction', 'transaction_data', 'ticket_url'), - status: json_response.dig('status'), - status_detail: json_response.dig('status_detail'), - amount: json_response.dig('transaction_amount'), - errors: PaymentError.new(json_response: json_response).errors + id: json_response.dig("id"), + description: json_response.dig("description"), + date_approved: json_response.dig("date_approved"), + date_created: json_response.dig("date_created"), + money_release_date: json_response.dig("money_release_date"), + payer_email: json_response.dig("payer", "email"), + payer_identification_type: json_response.dig("payer", "identification", "type"), + payer_identification_number: json_response.dig("payer", "identification", "number"), + payment_method: json_response.dig("payment_method_id"), + payment_type: json_response.dig("payment_type_id"), + qr_code: json_response.dig("point_of_interaction", "transaction_data", "qr_code"), + qr_code_base_64: json_response.dig("point_of_interaction", "transaction_data", "qr_code_base64"), + three_ds_info_external_resource_url: json_response.dig("three_ds_info", "external_resource_url"), + three_ds_info_creq: json_response.dig("three_ds_info", "creq"), + transaction_id: json_response.dig("point_of_interaction", "transaction_data", "transaction_id"), + ticket_url: json_response.dig("point_of_interaction", "transaction_data", "ticket_url"), + status: json_response.dig("status"), + status_detail: json_response.dig("status_detail"), + amount: json_response.dig("transaction_amount"), + error: payment_error.error, + internal_error: payment_error.internal_error } end end end