Skip to content

Commit

Permalink
Changing the checkout form generator to HAML Engine.
Browse files Browse the repository at this point in the history
Changing the Integration class, extracting the to_weight and to_money methods and implementing them in other class: Util, in Pagseguro Module. This approuch looks better than join all in Integration class.
  • Loading branch information
Bruno Azisaka Maciel committed Nov 9, 2008
1 parent 7d4f675 commit 6c0329f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 134 deletions.
4 changes: 2 additions & 2 deletions pagseguro/lib/pagseguro.rb
@@ -1,10 +1,10 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

%w(integration).each {|req| require File.dirname(__FILE__) + "/pagseguro/#{req}"}

%w(integration util).each {|req| require File.dirname(__FILE__) + "/pagseguro/#{req}"}

module Pagseguro
extend Util
end


Expand Down
107 changes: 6 additions & 101 deletions pagseguro/lib/pagseguro/integration.rb
Expand Up @@ -3,7 +3,7 @@
require 'net/http'
require 'net/https'
require 'hpricot'
require "erb"
require "haml"

class Integration

Expand Down Expand Up @@ -38,109 +38,14 @@ def checkout sale, *frete
raise "Invalid buyer! (not a Buyer)" unless sale.buyer.class.eql? Buyer
raise "Invalid Itens! (qt = 0)" if sale.itens.size == 0


data = "<form action=\"#{@config['webpagto']}\" method=\"post\" name=\"payment_form\"> \n"
data += " <input type=\"hidden\" name=\"ref_transacao\" value=\"#{sale.code}\" /> \n"
data += " <input type=\"hidden\" name=\"email_cobranca\" value=\"#{@config['email_cobranca']}\" /> \n"
data += " <input type=\"hidden\" name=\"tipo\" value=\"#{@config['tipo_carrinho']}\" /> \n"
data += " <input type=\"hidden\" name=\"moeda\" value=\"#{@config['moeda']}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_pais\" value=\"#{@config['pais']}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_nome\" value=\"#{sale.buyer.name}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_cep\" value=\"#{sale.buyer.zip}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_end\" value=\"#{sale.buyer.address}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_num\" value=\"#{sale.buyer.number}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_compl\" value=\"#{sale.buyer.complement}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_bairro\" value=\"#{sale.buyer.district}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_cidade\" value=\"#{sale.buyer.city}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_uf\" value=\"#{sale.buyer.state}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_ddd\" value=\"#{sale.buyer.ext}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_tel\" value=\"#{sale.buyer.phone}\" /> \n"
data += " <input type=\"hidden\" name=\"cliente_email\" value=\"#{sale.buyer.email}\" /> \n"

# shipment type...
# if no shipment_type is not specified and also shipment price,
# the buyer will be asked, to select one
#
# https://pagseguro.uol.com.br/Security/WebPagamentos/ConfigWebPagto.aspx
#
if (frete.empty?)
#... calculated price, based on the weight and shipment type, calculated by PagSeguro
# configure on the PagSeguro: https://pagseguro.uol.com.br/Security/WebPagamentos/ConfigWebPagto.aspx (Frete por peso)
data += " <input type=\"hidden\" name=\"tipo_frete\" value=\"#{sale.shipment_type}\" /> \n" unless sale.shipment_type.nil?
else
#... fixed price
# configure on the PagSeguro: https://pagseguro.uol.com.br/Security/WebPagamentos/ConfigWebPagto.aspx (Frete fixo com desconto)
data += " <input type=\"hidden\" name=\"item_frete_1\" value=\"#{frete.first}\" /> \n"
end

sale.itens.values.each_with_index do |item, i|
data += " <input type=\"hidden\" name=\"item_id_#{i+1}\" value=\"#{item.code}\" /> \n"
data += " <input type=\"hidden\" name=\"item_descr_#{i+1}\" value=\"#{item.description}\" /> \n"
data += " <input type=\"hidden\" name=\"item_quant_#{i+1}\" value=\"#{item.quantity}\" /> \n"
data += " <input type=\"hidden\" name=\"item_valor_#{i+1}\" value=\"#{self.to_money(item.price.to_s)}\" /> \n"
data += " <input type=\"hidden\" name=\"item_peso_#{i+1}\" value=\"#{self.to_weight(item.weight.to_s)}\" /> \n"

#TODO colocar o valor do frete.....
end

data += "<input type=\"submit\" /> \n"
# data += "<script language=\"JavaScript\"> document.payment_form.submit(); </script> \n"

data += "</form>"
Haml::Engine.new(IO.readlines(File.dirname(__FILE__)+"/views/form.html.haml").join).render(Object.new,
:config=>@config,
:sale=>sale,
:frete=>frete
)

end


#TODO Traduzir para ingles............
# Formata o campo de decimal para String no padrao solicitado do PagSeguro
# Ex.:
# integracao.to_peso "0.5" ==> 500 #500 gramas
# integracao.to_peso "0.50 ==> 500 #500 gramas
# integracao.to_peso "0.05" ==> 050 #50 gramas
# integracao.to_peso "0.005" ==> 005 #5 gramas
# integracao.to_peso "1" ==> 1000 #1 kilo
# integracao.to_peso "1.0" ==> 1000 #1 kilo
#
def to_weight valor
return nil if valor.nil?
return nil if valor.class != String
return "000" if valor.to_f == 0

# obtem a parte fracionaria e transforma em string.
frac = valor.to_f - valor.to_i
frac = frac.to_s + "00"
frac = frac[2..4]
inteiro = ""
inteiro = valor.to_i.to_s if (valor.to_f.truncate > 0)
novo_valor = inteiro + frac.to_s
end


# TODO traduzir para ingles................
# Formata o campo de decimal para String no padrao solicitado do PagSeguro
# Ex.:
# integracao.to_dinheiro "0.5" ==> 50 #50 centavos
# integracao.to_dinheiro "0.50 ==> 50 #50 centavos
# integracao.to_dinheiro "0.05" ==> 05 #5 centavos
# integracao.to_dinheiro "1" ==> 100 #1 real
# integracao.to_dinheiro "1.0" ==> 100 #1 real
#
def to_money valor
return nil if valor.nil?
return nil if valor.class != String
return "00" if valor.to_f == 0

# obtem a parte fracionaria e transforma em string.
frac = valor.to_f - valor.to_i
frac = frac.to_s + "0"
frac = frac[2..3]
# Se tiver parte inteira, concatena com a parte fracionaria
inteiro = ""
inteiro = valor.to_i.to_s if valor.to_f.truncate > 0
inteiro + frac
end


# # Envia um post para o PagSeguro solicitando a autenticidade do post enviado pelo PagSeguro
# # Segundo a documentacao do PagSeguro, após o pagamento da compra, o sistema envia uma notificacao
# # via post para o site do lojista.
Expand Down
52 changes: 52 additions & 0 deletions pagseguro/lib/pagseguro/util.rb
@@ -0,0 +1,52 @@
module Util
Object.class_eval do
#TODO Traduzir para ingles............
# Formata o campo de decimal para String no padrao solicitado do PagSeguro
# Ex.:
# integracao.to_peso "0.5" ==> 500 #500 gramas
# integracao.to_peso "0.50 ==> 500 #500 gramas
# integracao.to_peso "0.05" ==> 050 #50 gramas
# integracao.to_peso "0.005" ==> 005 #5 gramas
# integracao.to_peso "1" ==> 1000 #1 kilo
# integracao.to_peso "1.0" ==> 1000 #1 kilo
#
def to_ps_weight
return nil if self.nil?
return nil if self.class != String
return "000" if self.to_f == 0

# obtem a parte fracionaria e transforma em string.
frac = self.to_f - self.to_i
frac = frac.to_s + "00"
frac = frac[2..4]
inteiro = ""
inteiro = self.to_i.to_s if (self.to_f.truncate > 0)
novo_valor = inteiro + frac.to_s
end


# TODO traduzir para ingles................
# Formata o campo de decimal para String no padrao solicitado do PagSeguro
# Ex.:
# integracao.to_dinheiro "0.5" ==> 50 #50 centavos
# integracao.to_dinheiro "0.50 ==> 50 #50 centavos
# integracao.to_dinheiro "0.05" ==> 05 #5 centavos
# integracao.to_dinheiro "1" ==> 100 #1 real
# integracao.to_dinheiro "1.0" ==> 100 #1 real
#
def to_ps_money
return nil if self.nil?
return nil if self.class != String
return "00" if self.to_f == 0

# obtem a parte fracionaria e transforma em string.
frac = self.to_f - self.to_i
frac = frac.to_s + "0"
frac = frac[2..3]
# Se tiver parte inteira, concatena com a parte fracionaria
inteiro = ""
inteiro = self.to_i.to_s if self.to_f.truncate > 0
inteiro + frac
end
end
end
32 changes: 32 additions & 0 deletions pagseguro/lib/pagseguro/views/form.html.haml
@@ -0,0 +1,32 @@
%form{ :action=>config['webpagto'], :method=>"post", :name=>"payment_form" }
%input{ :type=>"hidden", :name=>"ref_transacao", :value=>sale.code}
%input{ :type=>"hidden", :name=>"email_cobranca", :value=>config['email_cobranca'] }
%input{ :type=>"hidden", :name=>"tipo", :value=>config['tipo_carrinho'] }
%input{ :type=>"hidden", :name=>"moeda", :value=>config['moeda'] }
%input{ :type=>"hidden", :name=>"cliente_pais", :value=>config['pais'] }
%input{ :type=>"hidden", :name=>"cliente_nome", :value=>sale.buyer.name }
%input{ :type=>"hidden", :name=>"cliente_cep", :value=>sale.buyer.zip }
%input{ :type=>"hidden", :name=>"cliente_end", :value=>sale.buyer.address }
%input{ :type=>"hidden", :name=>"cliente_num", :value=>sale.buyer.number }
%input{ :type=>"hidden", :name=>"cliente_compl", :value=>sale.buyer.complement }
%input{ :type=>"hidden", :name=>"cliente_bairro", :value=>sale.buyer.district }
%input{ :type=>"hidden", :name=>"cliente_cidade", :value=>sale.buyer.city }
%input{ :type=>"hidden", :name=>"cliente_uf", :value=>sale.buyer.state }
%input{ :type=>"hidden", :name=>"cliente_ddd", :value=>sale.buyer.ext }
%input{ :type=>"hidden", :name=>"cliente_tel", :value=>sale.buyer.phone }
%input{ :type=>"hidden", :name=>"cliente_email", :value=>sale.buyer.email }

- if frete.empty?
- unless sale.shipment_type.nil?
%input{ :type=>"hidden", :name=>"tipo_frete", :value=>sale.shipment_type}
- else
%input{ :type=>"hidden", :name=>"item_frete_1", :value=>frete.first }

- sale.itens.values.each_with_index do |item, i|
%input{ :type=>"hidden", :name=>"item_id_#{i+1}", :value=>item.code }
%input{ :type=>"hidden", :name=>"item_descr_#{i+1}", :value=>item.description }
%input{ :type=>"hidden", :name=>"item_quant_#{i+1}", :value=>item.quantity }
%input{ :type=>"hidden", :name=>"item_valor_#{i+1}", :value=>item.price.to_ps_money }
%input{ :type=>"hidden", :name=>"item_peso_#{i+1}", :value=>item.weight.to_ps_weight }

%input{ :type=>"submit" }
58 changes: 27 additions & 31 deletions pagseguro/test/pagseguro_test.rb
Expand Up @@ -25,101 +25,97 @@ def setup


def test_to_weight
pagseguro = Integration.new

#1.1 - nil
price = pagseguro.to_weight nil
price = nil.to_ps_weight
assert_nil price, "1.1 - nil, se o parametro for nil, o retorno tbem devera ser nil"

#1.2 - price nao conversivel a decimal
price = pagseguro.to_weight "abc"
price = "abc".to_ps_weight
assert_equal "000", price, "1.2 - o price deve ser conversivel a decimal, caso contrario retornara o weight zerado no formado PS (000)"

#1.3 - price deve ser uma String
price = pagseguro.to_weight 14
price = 14.to_ps_weight
assert_nil nil, "1.3 - price deve ser uma string"

#2 - entradas validas
price = pagseguro.to_weight "0.5"
price = "0.5".to_ps_weight
assert_equal "500", price, "2.1 - 0.5 => 500"

price = pagseguro.to_weight "0.50"
price = "0.50".to_ps_weight
assert_equal "500", price, "2.2 - 0.50 => 500"

price = pagseguro.to_weight "0.500"
price = "0.500".to_ps_weight
assert_equal "500", price, "2.3 - 0.500 => 500"

price = pagseguro.to_weight "0.05"
price = "0.05".to_ps_weight
assert_equal "050", price, "2.4 - 0.05 => 050"

price = pagseguro.to_weight "0.005"
price = "0.005".to_ps_weight
assert_equal "005", price, "2.5 - 0.05 => 005"

price = pagseguro.to_weight "1"
price = "1".to_ps_weight
assert_equal "1000", price, "2.6 - 1 => 1000"

price = pagseguro.to_weight "1.0"
price = "1.0".to_ps_weight
assert_equal "1000", price, "2.7 - 1.0 => 1000"

price = pagseguro.to_weight "1.5"
price = "1.5".to_ps_weight
assert_equal "1500", price, "2.8 - 1.5 => 1500"

price = pagseguro.to_weight "1.500"
price = "1.500".to_ps_weight
assert_equal "1500", price, "2.9 - 1.500 => 1500"

price = pagseguro.to_weight "1.501"
price = "1.501".to_ps_weight
assert_equal "1501", price, "2.10 - 1.501 => 1501"

price = pagseguro.to_weight "1.510"
price = "1.510".to_ps_weight
assert_equal "1510", price, "2.11 - 1.510 => 1510"

price = pagseguro.to_weight "0.1"
price = "0.1".to_ps_weight
assert_equal "100", price, "2.12 - 0.1 => 100"


end

def test_to_money
pagseguro = Integration.new

#1.1 - nil
price = pagseguro.to_money nil
price = nil.to_ps_money
assert_nil price, "1.1 - nil, se o parametro for nil, o retorno tbem devera ser nil"

#1.2 - price nao conversivel a decimal
price = pagseguro.to_money "abc"
price = "abc".to_ps_money
assert_equal "00", price, "1.2 - o price deve ser conversivel a decimal, caso contrario retornara o weight zerado no formado PS (000)"

#1.3 - price deve ser uma String
price = pagseguro.to_money 14
price = 14.to_ps_money
assert_nil nil, "1.3 - price deve ser uma string"

#2 - entradas validas
price = pagseguro.to_money "0.5"
price = "0.5".to_ps_money
assert_equal "50", price, "2.1 - 0.5 => 50 centavos"

price = pagseguro.to_money "0.50"
price = "0.50".to_ps_money
assert_equal "50", price, "2.2 - 0.50 => 50 centavos"

price = pagseguro.to_money "0.05"
price = "0.05".to_ps_money
assert_equal "05", price, "2.3 - 0.05 => 05 centavos"

price = pagseguro.to_money "1"
price = "1".to_ps_money
assert_equal "100", price, "2.4 - 1 => 1 real (100)"

price = pagseguro.to_money "1.0"
price = "1.0".to_ps_money
assert_equal "100", price, "2.5 - 1.0 => 1 real (100)"

price = pagseguro.to_money "1.5"
price = "1.5".to_ps_money
assert_equal "150", price, "2.6 - 1.5 => 1,5 real"

price = pagseguro.to_money "1.55"
price = "1.55".to_ps_money
assert_equal "155", price, "2.7 - 1.55 => 1,55 real"

price = pagseguro.to_money "1.05"
price = "1.05".to_ps_money
assert_equal "105", price, "2.8 - 1.05 => 105 real (100)"

price = pagseguro.to_money "0.05"
price = "0.05".to_ps_money
assert_equal "05", price, "2.9 - 0.05 => 05 real"

end
Expand Down

0 comments on commit 6c0329f

Please sign in to comment.