Sha256: 2f3d5404c43fda7cdb3f60bccee82f356ab09bb097ffb745ba0062212bd62875

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module MyMoip
  class CreditCardPayment
    attr_accessor :credit_card, :installments

    def initialize(credit_card, opts = {})
      self.credit_card = credit_card
      # Backward compatibility. See 0.2.3 CHANGELOG
      self.installments = if opts.kind_of?(Integer)
                            opts
                          else
                            opts[:installments] || 1
                          end
    end

    def to_json(formatter = MyMoip::Formatter)
      raise "No CreditCard provided" if credit_card.nil?
      raise ArgumentError, 'Invalid credit card' if credit_card.invalid?

      json = {
        Forma:        "CartaoCredito",
        Parcelas:     @installments,
        CartaoCredito: {
          Numero:           credit_card.card_number,
          Expiracao:        credit_card.expiration_date,
          CodigoSeguranca:  credit_card.security_code
        }
      }

      json[:CartaoCredito][:Portador] = {
        Nome: credit_card.owner_name,
        DataNascimento: (credit_card.owner_birthday and
                         formatter.date(credit_card.owner_birthday)),
        Telefone: (credit_card.owner_phone and
                   formatter.phone(credit_card.owner_phone)),
        Identidade: (credit_card.owner_cpf and
                     formatter.cpf(credit_card.owner_cpf))
      }

      json[:Instituicao] = {
        american_express: "AmericanExpress",
        diners:           "Diners",
        hipercard:        "Hipercard",
        mastercard:       "Mastercard",
        visa:             "Visa"
      }.fetch(credit_card.logo)

      if cash?
        json[:Recebimento] = "AVista"
      end

      json
    end

    def cash?
      @installments == 1
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mymoip-0.4.1 lib/mymoip/credit_card_payment.rb
mymoip-0.4.0 lib/mymoip/credit_card_payment.rb
mymoip-0.3.1 lib/mymoip/credit_card_payment.rb