Class: MyMoip::Instruction

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Validators
Defined in:
lib/mymoip/instruction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Instruction

Returns a new instance of Instruction.



18
19
20
21
22
23
24
# File 'lib/mymoip/instruction.rb', line 18

def initialize(attrs)
  attrs.each do |attr, value|
    public_send(:"#{attr}=", value)
  end

  self.commissions ||= []
end

Instance Attribute Details

#commissionsObject

Returns the value of attribute commissions.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def commissions
  @commissions
end

#fee_payer_loginObject

Returns the value of attribute fee_payer_login.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def 
  @fee_payer_login
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def id
  @id
end

#installmentsObject

Returns the value of attribute installments.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def installments
  @installments
end

#notification_urlObject

Returns the value of attribute notification_url.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def notification_url
  @notification_url
end

#payerObject

Returns the value of attribute payer.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def payer
  @payer
end

#payment_methodsObject

Returns the value of attribute payment_methods.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def payment_methods
  @payment_methods
end

#payment_reasonObject

Returns the value of attribute payment_reason.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def payment_reason
  @payment_reason
end

#payment_receiver_loginObject

Returns the value of attribute payment_receiver_login.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def 
  @payment_receiver_login
end

#payment_receiver_nameObject

Returns the value of attribute payment_receiver_name.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def payment_receiver_name
  @payment_receiver_name
end

#payment_slipObject

Returns the value of attribute payment_slip.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def payment_slip
  @payment_slip
end

#return_urlObject

Returns the value of attribute return_url.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def return_url
  @return_url
end

#valuesObject

Returns the value of attribute values.



7
8
9
# File 'lib/mymoip/instruction.rb', line 7

def values
  @values
end

Instance Method Details

#commissions_sumObject



83
84
85
86
87
# File 'lib/mymoip/instruction.rb', line 83

def commissions_sum
  commissions.inject(0) do |sum, commission|
    sum + commission.gross_amount(self)
  end
end

#gross_amountObject



89
90
91
# File 'lib/mymoip/instruction.rb', line 89

def gross_amount
  values ? values.reduce(0) { |sum, value| sum + value } : 0
end

#to_xml(root = nil) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mymoip/instruction.rb', line 26

def to_xml(root = nil)
  raise InvalidPayer       if payer.invalid?
  raise InvalidPaymentSlip if payment_slip and payment_slip.invalid?
  raise InvalidInstruction if self.invalid?
  if invalid_commission = commissions.detect { |c| c.invalid? }
    raise InvalidComission, invalid_commission
  end

  xml = ""
  root = Builder::XmlMarkup.new(target: xml)

  root.EnviarInstrucao do |n1|
    n1.InstrucaoUnica(TipoValidacao: "Transparente") do |n2|
      n2.Razao(@payment_reason)
      n2.Valores do |n3|
        @values.each { |v| n3.Valor("%.2f" % v, moeda: "BRL") }
      end
      n2.IdProprio(@id)

      if @installments
        n2.Parcelamentos do |n4|
          @installments.each do |installments|
            n4.Parcelamento do |n5|
              n5.MinimoParcelas(installments[:min])
              n5.MaximoParcelas(installments[:max])
              if installments[:forward_taxes]
                n5.Repassar(installments[:forward_taxes])
              end
              n5.Juros(installments[:fee])
            end
          end
        end
      end

      commissions_to_xml(n2)      if commissions.any?
      payment_receiver_to_xml(n2) if 

      n2.Pagador { |n3| @payer.to_xml(n3) }

      unless @payment_methods.blank? or @payment_methods.using_all?
        n2.FormasPagamento { |n3| @payment_methods.to_xml(n3) }
      end
      unless @payment_slip.blank?
        n2.Boleto { |n3| @payment_slip.to_xml(n3) }
      end
      unless @notification_url.blank?
        n2.URLNotificacao(@notification_url)
      end
      unless @return_url.blank?
        n2.URLRetorno(@return_url)
      end
    end
  end

  xml
end