Sha256: 9b89334cef654c3ef416b1647f3ebfda36d5a5b57add79b470bcbbc69a857a63

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

module MpApi
  class PaymentError
    attr_reader :json_response
    def initialize(json_response:)
      @json_response = json_response
    end

    def error
      return nil unless json_response["error"] || ["rejected"].include?(json_response["status"])
      rejected_payment_error || failed_payment_error || "Pagamento recusado."
    end

    def internal_error
      return nil unless json_response["error"]
      json_response["cause"]&.first["description"]
    end

    private

    def rejected_payment_error
      message_by_status_detail(json_response["status_detail"])
    end

    def failed_payment_error
      message_by_error_message(json_response.dig("cause", 0, "description"))
    end

    def message_by_status_detail(status_detail)
      {
        "cc_rejected_bad_filled_other" => "Pagamento recusado.",
        "cc_rejected_bad_filled_date" => "Pagamento recusado. Confira a data de validade do cartão.",
        "cc_rejected_bad_filled_security_code" => "Pagamento recusado. Confira o código de segurança (CVV) do cartão.",
        "cc_rejected_insufficient_amount" => "Pagamento recusado. Limite insuficiente.",
        "cc_rejected_call_for_authorize" => "Pagamento recusado.",
        "cc_rejected_other_reason" => "Pagamento recusado.",
        "cc_rejected_high_risk" => "Pagamento recusado."
      }[status_detail]
    end

    def message_by_error_message(error_message)
      {
        "Invalid user identification number" => "CPF inválido.",
        "payer.email must be a valid email" => "E-mail inválido."
        # "Payer email forbidden" => "E-mail inválido.", Caso do erro no email test_user_2147198029@testuser.com (Nao sei se da pra causar com email valido)
      }[error_message]
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mp_api-1.2.2 lib/mp_api/payment_error.rb
mp_api-1.2.1 lib/mp_api/payment_error.rb
mp_api-1.2.0 lib/mp_api/payment_error.rb
mp_api-1.0.1 lib/mp_api/payment_error.rb
mp_api-0.3.12 lib/mp_api/payment_error.rb