Irio / mymoip

MoIP transactions in a gem to call your own.
MIT License
31 stars 21 forks source link

Wrong commission percentage values #28

Closed zangrandi closed 11 years ago

zangrandi commented 11 years ago

In Moip documentation (https://labs.moip.com.br/parametro/comissoes/) the commissions with percentage values are not from 0 to 1, but from 0 to 100:

        <Comissionamento>
            <Comissionado>
                <LoginMoIP>recebedor_secundario</LoginMoIP>
            </Comissionado>
            <Razao>Motivo da comissão</Razao>
            <ValorFixo>10.00</ValorFixo>
            **<ValorPercentual>12.00</ValorPercentual>**
        </Comissionamento>
        <PagadorTaxa>
            <LoginMoIP>recebedor_secundario</LoginMoIP>
        </PagadorTaxa>
    </Comissoes>

This caused an error when I went to production. This code generated a commission value of R$ 1 in an item that costs R$ 100:

commissions << MyMoip::Commission.new(
    reason: "Venda de curso na escola #{current_school.name}",
    receiver_login: current_school.moip_login,
    percentage_value: 1.0
)

I got no time right now to contribute to the project, so I fixed it locally overring the commission "to_xml" method:

module MyMoip
    class Commission
        def to_xml(root = nil)
      raise InvalidComission if invalid?

      if root.nil?
        xml  = ""
        root ||= Builder::XmlMarkup.new(target: xml)
      end

      root.Comissionamento do |n1|
        n1.Razao(reason)
        n1.Comissionado {|n2| n2.LoginMoIP(receiver_login)}
        n1.ValorFixo(fixed_value) if fixed_value
        n1.ValorPercentual(percentage_value*100) if percentage_value
      end

      xml
        end
    end
end

It's working, including in production. The commissions are being generated ok now.

Irio commented 11 years ago

Thanks @zangrandi, it's a really important bug, apparently fixed in fix-commission-percentage-format branch.

It will be released in a new gem version as soon as possible.

Irio commented 11 years ago

You can now update to 0.6.1 with this patch included.