5
6
7
8
9
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/pagseguro/rake.rb', line 5
def run
require "digest/md5"
env = ENV.inject({}) do |buffer, (name, value)|
value = value.respond_to?(:force_encoding) ? value.dup.force_encoding("UTF-8") : value
buffer.merge(name => value)
end
unless PagSeguro.developer?
puts "=> [PagSeguro] Can only notify development URLs"
puts "=> [PagSeguro] Double check your config/pagseguro.yml file"
exit 1
end
unless File.exist?(PagSeguro::DeveloperController::PAGSEGURO_ORDERS_FILE)
puts "=> [PagSeguro] No orders added. Exiting now!"
exit 1
end
orders = YAML.load_file(PagSeguro::DeveloperController::PAGSEGURO_ORDERS_FILE)
unless orders
puts "=> [PagSeguro] No invoices created. Exiting now!"
exit 1
end
order = orders[env["ID"]]
unless order
puts "=> [PagSeguro] The order #{env['ID'].inspect} could not be found. Exiting now!"
exit 1
end
name = env.fetch("NAME", Faker.name)
email = env.fetch("EMAIL", Faker.email)
order["CliNome"] = name
order["CliEmail"] = email
order["CliEndereco"] = Faker.street_name
order["CliNumero"] = rand(1000)
order["CliComplemento"] = Faker.secondary_address
order["CliBairro"] = Faker.city
order["CliCidade"] = Faker.city
order["CliCEP"] = Faker.zipcode
order["CliTelefone"] = Faker.phone_number
order["DataTransacao"] = Time.now.strftime("%d/%m/%Y %H:%M:%S")
order["Referencia"] = order.delete("ref_transacao")
order["NumItens"] = order.inject(0) do |count, (key, value)|
count += 1 if key =~ /item_id_/
count
end
to_price = proc do |price|
if price.to_s =~ /^(.*?)(.{2})$/
"#{$1},#{$2}"
else
"0,00"
end
end
for index in (1..order["NumItens"])
order["ProdID_#{index}"] = order.delete("item_id_#{index}")
order["ProdDescricao_#{index}"] = order.delete("item_descr_#{index}")
order["ProdValor_#{index}"] = to_price.call(order.delete("item_valor_#{index}"))
order["ProdQuantidade_#{index}"] = order.delete("item_quant_#{index}")
order["ProdFrete_#{index}"] = to_price.call(order.delete("item_frete_#{index}"))
order["ProdExtras_#{index}"] = "0,00"
end
status = env.fetch("STATUS", :completed).to_sym
payment_method = env.fetch("PAYMENT_METHOD", :credit_card).to_sym
order["TransacaoID"] = Digest::MD5.hexdigest(Time.now.to_s)
order["Anotacao"] = env["NOTE"].to_s
index = proc do |hash, value|
if hash.respond_to?(:key)
hash.key(value)
else
hash.index(value)
end
end
order["TipoPagamento"] = index[PagSeguro::Notification::PAYMENT_METHOD, payment_method]
order["StatusTransacao"] = index[PagSeguro::Notification::STATUS, status]
uri = URI.parse File.join(PagSeguro.config["base"], PagSeguro.config["return_to"])
Net::HTTP.post_form uri, order
end
|