Class: MyMoip::CreditCardPayment

Inherits:
Payment
  • Object
show all
Defined in:
lib/mymoip/payments/credit_card_payment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Payment

payment_method

Constructor Details

#initialize(credit_card, opts = {}) ⇒ CreditCardPayment



5
6
7
8
# File 'lib/mymoip/payments/credit_card_payment.rb', line 5

def initialize(credit_card, opts = {})
  self.credit_card  = credit_card
  self.installments = opts[:installments] || 1
end

Instance Attribute Details

#credit_cardObject

Returns the value of attribute credit_card.



3
4
5
# File 'lib/mymoip/payments/credit_card_payment.rb', line 3

def credit_card
  @credit_card
end

#installmentsObject

Returns the value of attribute installments.



3
4
5
# File 'lib/mymoip/payments/credit_card_payment.rb', line 3

def installments
  @installments
end

Instance Method Details

#cash?Boolean



49
50
51
# File 'lib/mymoip/payments/credit_card_payment.rb', line 49

def cash?
  @installments == 1
end

#to_json(formatter = MyMoip::Formatter) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mymoip/payments/credit_card_payment.rb', line 10

def to_json(formatter = MyMoip::Formatter)
  raise InvalidCreditCard, 'No credit card provided.' if credit_card.nil?
  raise InvalidCreditCard                             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.)

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

  json
end