forked from guinascimento/moip-ruby
/
moip_spec.rb
289 lines (244 loc) · 10.8 KB
/
moip_spec.rb
1
2
3
4
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# encoding: utf-8
require "moip"
require "digest/sha1"
describe "Make payments with the MoIP API" do
before :all do
MoIP.setup do |config|
config.uri = 'https://desenvolvedor.moip.com.br/sandbox'
config.token = 'token'
config.key = 'key'
end
@pagador = { :nome => "Luiz Inácio Lula da Silva",
:login_moip => "lula",
:email => "presidente@planalto.gov.br",
:tel_cel => "(61)9999-9999",
:apelido => "Lula",
:identidade => "111.111.111-11",
:logradouro => "Praça dos Três Poderes",
:numero => "0",
:complemento => "Palácio do Planalto",
:bairro => "Zona Cívico-Administrativa",
:cidade => "Brasília",
:estado => "DF",
:pais => "BRA",
:cep => "70100-000",
:tel_fixo => "(61)3211-1221" }
@billet_without_razao = { :value => "8.90", :id_proprio => id,
:forma => "BoletoBancario", :pagador => @pagador}
@billet = { :value => "8.90", :id_proprio => id,
:forma => "BoletoBancario", :pagador => @pagador ,
:razao=> "Pagamento" }
@debit = { :value => "8.90", :id_proprio => id, :forma => "DebitoBancario",
:instituicao => "BancoDoBrasil", :pagador => @pagador,
:razao => "Pagamento"}
@credit = { :value => "8.90", :id_proprio => id, :forma => "CartaoCredito",
:instituicao => "AmericanExpress",:numero => "345678901234564",
:expiracao => "08/11", :codigo_seguranca => "1234",
:nome => "João Silva", :identidade => "134.277.017.00",
:telefone => "(21)9208-0547", :data_nascimento => "25/10/1980",
:parcelas => "2", :recebimento => "AVista",
:pagador => @pagador, :razao => "Pagamento"}
end
context "checkout" do
before(:each) do
MoIP::Client.stub!(:post).
and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
{ "ID"=>Time.now.strftime("%y%m%d%H%M%S"),
"Status"=>"Sucesso",
"Token" => "T2N0L0X8E0S71217U2H3W1T4F4S4G4K731D010V0S0V0S080M010E0Q082X2"
}
})
end
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.checkout(@billet) }
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.checkout has been deprecated/ }.should be_true
end
context "when it is a billet checkout" do
it "should raise an exception when razao parameter is not passed" do
error = "É necessário informar a razão do pagamento"
lambda { MoIP::Client.checkout(@billet_without_razao) }.should
raise_error(MoIP::MissingPaymentTypeError,error)
end
it "should have status 'Sucesso'" do
response = MoIP::Client.checkout(@billet)
response["Status"].should == "Sucesso"
end
end
context "when it is a debit checkout" do
it "should have status 'Sucesso' with valid arguments" do
response = MoIP::Client.checkout(@debit)
response["Status"].should == "Sucesso"
end
it "should have status 'Falha' when a instituition is not passed as argument" do
@incorrect_debit = { :value => "37.90", :id_proprio => id,
:forma => "DebitoBancario", :pagador => @pagador,
:razao => "Pagamento"}
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
MoIP::Client.stub!(:post)
.and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
{
"Status"=>"Falha",
"Erro"=>error
}
})
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
lambda { MoIP::Client.checkout(@incorrect_debit) }.should
raise_error(MoIP::WebServerResponseError, error)
end
it "should raise an exception if payer informations were not passed" do
@incorrect_debit = { :value => "37.90", :id_proprio => id,
:forma => "DebitoBancario",
:instituicao => "BancoDoBrasil",
:razao => "Pagamento"
}
lambda { MoIP::Client.checkout(@incorrect_debit) }.should
raise_error(MoIP::MissingPayerError, "É obrigatório passar as informações do pagador")
end
end
context "when it is a credit card checkout" do
it "should have status 'Sucesso' with valid arguments" do
response = MoIP::Client.checkout(@credit)
response["Status"].should == "Sucesso"
end
it "should have status 'Falha' when the card informations were not passed as argument" do
@incorrect_credit = { :value => "8.90", :id_proprio => id,
:forma => "CartaoCredito", :pagador => @pagador,
:razao => "Pagamento"
}
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
MoIP::Client.stub!(:post)
.and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{
"Resposta"=>
{
"Status"=>"Falha",
"Erro"=>error
}
})
error = "Pagamento direto não é possível com a instituição de pagamento enviada"
lambda { MoIP::Client.checkout(@incorrect_credit) }.should
raise_error(MoIP::WebServerResponseError, error)
end
end
context "in error scenario" do
it "should raise an exception if response is nil" do
MoIP::Client.stub!(:post).and_return(nil)
lambda { MoIP::Client.checkout(@billet) }.should
raise_error(StandardError,"Ocorreu um erro ao chamar o webservice")
end
it "should raise an exception if status is fail" do
MoIP::Client.stub!(:post)
.and_return("ns1:EnviarInstrucaoUnicaResponse"=>
{ "Resposta"=>
{"Status"=>"Falha",
"Erro"=>"O status da resposta é Falha"
}
})
lambda { MoIP::Client.checkout(@billet) }.should raise_error(StandardError, "O status da resposta é Falha")
end
end
end
context "query a transaction token" do
before(:each) do
MoIP::Client.stub!(:get)
.and_return("ns1:ConsultarTokenResponse"=>
{ "RespostaConsultar"=>
{"Status"=>"Sucesso",
"ID"=>"201010291031001210000000046760"
}
})
end
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.query(token) }
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.query has been deprecated/ }.should be_true
end
it "should retrieve the transaction" do
response = MoIP::Client.query(token)
response["Status"].should == "Sucesso"
end
context "in a error scenario" do
it "should retrieve status 'Falha'" do
MoIP::Client.stub!(:get)
.and_return("ns1:ConsultarTokenResponse"=>
{ "RespostaConsultar"=>
{"Status"=>"Falha",
"Erro"=>"Instrução não encontrada",
"ID"=>"201010291102522860000000046768"
}
})
query = "000000000000000000000000000000000000000000000000000000000000"
lambda { MoIP::Client.query(query) }.should raise_error(StandardError, "Instrução não encontrada")
end
end
end
context "build the MoIP URL" do
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.moip_page(token) }
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.moip_page has been deprecated/ }.should be_true
end
it "should build the correct URL" do
page = "https://desenvolvedor.moip.com.br/sandbox/Instrucao.do?token=#{token}"
MoIP::Client.moip_page(token).should == page
end
it "should raise an error if the token is not informed" do
error = "É necessário informar um token para retornar os dados da transação"
lambda { MoIP::Client.moip_page("").should
raise_error(ArgumentError, error) }
end
it "should raise an error if nil is passed as the token" do
error = "É necessário informar um token para retornar os dados da transação"
lambda { MoIP::Client.moip_page(nil).should
raise_error(ArgumentError, error) }
end
end
context "when receive notification" do
before(:each) do
@params = { "id_transacao" => "Pag62", "valor" => "8.90",
"status_pagamento" => "3", "cod_moip" => "001",
"forma_pagamento" => "73", "tipo_pagamento" => "BoletoBancario",
"email_consumidor" => "presidente@planalto.gov.br" }
end
it "with old api should be deprecated" do
deprecations = collect_deprecations{ MoIP.notification(@param) }
deprecations.should_not be_empty
deprecations.any? {|w| w =~ /MoIP.notification has been deprecated/ }.should be_true
end
it "should return a hash with the params extracted from NASP" do
response = { :transaction_id => "Pag62", :amount => "8.90",
:status => "printed", :code => "001",
:payment_type => "BoletoBancario",
:email => "presidente@planalto.gov.br" }
MoIP::Client.notification(@params).should == response
end
it "should return valid status based on status code" do
MoIP::STATUS[1].should == "authorized"
MoIP::STATUS[2].should == "started"
MoIP::STATUS[3].should == "printed"
MoIP::STATUS[4].should == "completed"
MoIP::STATUS[5].should == "canceled"
MoIP::STATUS[6].should == "analysing"
end
end
def id
"transaction_" + Digest::SHA1.hexdigest([Time.now, rand].join)
end
def token
"T2X0Q1N021E0B2S9U1P0V3Y0G1F570Y2P4M0P000M0Z0F0J0G0U4N6C7W5T9"
end
def collect_deprecations
old_behavior = ActiveSupport::Deprecation.behavior
deprecations = []
ActiveSupport::Deprecation.behavior = Proc.new do |message, callstack|
deprecations << message
end
result = yield
deprecations
ensure
ActiveSupport::Deprecation.behavior = old_behavior
end
end