Sha256: d9fe05caca849474559f0342a97ecda4bc8a21324b8fdf9d401a73ae7e2abfb2

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 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["cause"]&.first["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."
      }[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

1 entries across 1 versions & 1 rubygems

Version Path
mp_api-0.3.10 lib/mp_api/payment_error.rb