locomotiva-braspag 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +10 -0
  6. data/README.md +59 -0
  7. data/RELEASES.md +20 -0
  8. data/Rakefile +6 -0
  9. data/config/braspag.yml +17 -0
  10. data/lib/generators/braspag/install/install_generator.rb +15 -0
  11. data/lib/generators/braspag/install/templates/config/braspag.yml +17 -0
  12. data/lib/locomotiva-braspag.rb +45 -0
  13. data/lib/locomotiva-braspag/bill.rb +164 -0
  14. data/lib/locomotiva-braspag/connection.rb +50 -0
  15. data/lib/locomotiva-braspag/credit_card.rb +210 -0
  16. data/lib/locomotiva-braspag/crypto/jar_webservice.rb +91 -0
  17. data/lib/locomotiva-braspag/crypto/webservice.rb +100 -0
  18. data/lib/locomotiva-braspag/eft.rb +90 -0
  19. data/lib/locomotiva-braspag/errors.rb +40 -0
  20. data/lib/locomotiva-braspag/order.rb +42 -0
  21. data/lib/locomotiva-braspag/payment_method.rb +73 -0
  22. data/lib/locomotiva-braspag/poster.rb +36 -0
  23. data/lib/locomotiva-braspag/protected_credit_card.rb +160 -0
  24. data/lib/locomotiva-braspag/utils.rb +32 -0
  25. data/lib/locomotiva-braspag/version.rb +3 -0
  26. data/locomotiva-braspag.gemspec +33 -0
  27. data/spec/bill_spec.rb +427 -0
  28. data/spec/connection_spec.rb +188 -0
  29. data/spec/credit_card_spec.rb +517 -0
  30. data/spec/crypto/jar_webservice_spec.rb +187 -0
  31. data/spec/crypto/webservice_spec.rb +143 -0
  32. data/spec/eft_spec.rb +209 -0
  33. data/spec/order_spec.rb +118 -0
  34. data/spec/poster_spec.rb +53 -0
  35. data/spec/protected_credit_card_spec.rb +296 -0
  36. data/spec/spec_helper.rb +21 -0
  37. data/spec/utils_spec.rb +47 -0
  38. metadata +244 -0
@@ -0,0 +1,188 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe Braspag::Connection do
5
+ let(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
6
+ let(:crypto_key) { "{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" }
7
+ let(:crypto_url) { "http://localhost:9292" }
8
+
9
+ let(:braspag_environment) { "homologation" }
10
+
11
+ let(:braspag_homologation_url) { "https://homologacao.pagador.com.br" }
12
+ let(:braspag_production_url) { "https://transaction.pagador.com.br" }
13
+
14
+ let(:braspag_config) do
15
+ config = {}
16
+ config[ENV["BRASPAG_ENV"]] = {
17
+ "environment" => braspag_environment,
18
+ "merchant_id" => merchant_id,
19
+ "crypto_key" => crypto_key,
20
+ "crypto_url" => crypto_url
21
+ }
22
+ config
23
+ end
24
+
25
+ before(:all) do
26
+ @connection = Braspag::Connection.clone
27
+ end
28
+
29
+ context "changing default config file path" do
30
+ before :each do
31
+ @original_path = Braspag.config_file_path
32
+ end
33
+
34
+ after :each do
35
+ Braspag.config_file_path = @original_path
36
+ end
37
+
38
+ it "should read config from a different path when specified" do
39
+ connection = Braspag::Connection.clone
40
+
41
+ Braspag.config_file_path = '/some/crazy/path'
42
+
43
+ YAML.should_receive(:load_file).
44
+ with("/some/crazy/path").
45
+ and_return(braspag_config)
46
+
47
+ connection.instance
48
+ end
49
+ end
50
+
51
+ it "should read config/braspag.yml when alloc first instance" do
52
+ YAML.should_receive(:load_file)
53
+ .with("config/braspag.yml")
54
+ .and_return(braspag_config)
55
+ @connection.instance
56
+ end
57
+
58
+ it "should not read config/braspag.yml when alloc a second instance" do
59
+ YAML.should_not_receive(:load_file)
60
+ @connection.instance
61
+ end
62
+
63
+ it "should generate an exception when BRASPAG_ENV is nil" do
64
+ ENV.should_receive(:[])
65
+ .with("BRASPAG_ENV")
66
+ .and_return(nil)
67
+
68
+ expect {
69
+ Braspag::Connection.clone.instance
70
+ }.to raise_error Braspag::Connection::InvalidEnv
71
+ end
72
+
73
+ it "should generate an exception when BRASPAG_ENV is empty" do
74
+ ENV.should_receive(:[])
75
+ .twice
76
+ .with("BRASPAG_ENV")
77
+ .and_return("")
78
+
79
+ expect {
80
+ Braspag::Connection.clone.instance
81
+ }.to raise_error Braspag::Connection::InvalidEnv
82
+ end
83
+
84
+ it "should generate an exception when merchant_id is not in a correct format" do
85
+ braspag_config[ENV["BRASPAG_ENV"]]["merchant_id"] = "A" * 38
86
+
87
+ YAML.should_receive(:load_file)
88
+ .with("config/braspag.yml")
89
+ .and_return(braspag_config)
90
+
91
+ expect {
92
+ Braspag::Connection.clone.instance
93
+ }.to raise_error Braspag::Connection::InvalidMerchantId
94
+ end
95
+
96
+ it { @connection.instance.crypto_url.should == crypto_url }
97
+ it { @connection.instance.crypto_key.should == crypto_key }
98
+ it { @connection.instance.merchant_id.should == merchant_id }
99
+
100
+ [:braspag_url, :merchant_id, :crypto_url, :crypto_key,
101
+ :options, :environment].each do |attribute|
102
+
103
+ it { @connection.instance.should respond_to(attribute) }
104
+
105
+ end
106
+
107
+ context "when there is pagador_url and protected_card_url on configuration" do
108
+ let(:pagador_url) { "http://foo.bar" }
109
+ let(:protected_card_url) { "http://bar.foo" }
110
+
111
+ before do
112
+ braspag_config[ENV["BRASPAG_ENV"]]["pagador_url"] = pagador_url
113
+ braspag_config[ENV["BRASPAG_ENV"]]["protected_card_url"] = protected_card_url
114
+ YAML.should_receive(:load_file).and_return(braspag_config)
115
+ end
116
+
117
+ subject { Braspag::Connection.clone.instance }
118
+
119
+ its(:braspag_url) { should == pagador_url }
120
+ its(:protected_card_url) { should == protected_card_url }
121
+ end
122
+
123
+ describe "#production?" do
124
+ it "should return true when environment is production" do
125
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "production"
126
+
127
+ YAML.should_receive(:load_file)
128
+ .and_return(braspag_config)
129
+
130
+ Braspag::Connection.clone.instance.production?.should be_true
131
+ end
132
+
133
+ it "should return false when environment is not production" do
134
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "homologation"
135
+
136
+ YAML.should_receive(:load_file)
137
+ .and_return(braspag_config)
138
+
139
+ Braspag::Connection.clone.instance.production?.should be_false
140
+ end
141
+ end
142
+
143
+ describe "#homologation?" do
144
+ it "should return true when environment is homologation" do
145
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "homologation"
146
+
147
+ YAML.should_receive(:load_file)
148
+ .and_return(braspag_config)
149
+
150
+ Braspag::Connection.clone.instance.homologation?.should be_true
151
+ end
152
+
153
+ it "should return false when environment is not homologation" do
154
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "production"
155
+
156
+ YAML.should_receive(:load_file)
157
+ .and_return(braspag_config)
158
+
159
+ Braspag::Connection.clone.instance.homologation?.should be_false
160
+ end
161
+ end
162
+
163
+ describe "#braspag_url" do
164
+ context "when environment is homologation" do
165
+ it "should return the Braspag homologation url" do
166
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "homologation"
167
+
168
+ YAML.should_receive(:load_file)
169
+ .and_return(braspag_config)
170
+
171
+ connection = Braspag::Connection.clone.instance
172
+ connection.braspag_url.should == braspag_homologation_url
173
+ end
174
+ end
175
+
176
+ context "when environment is production" do
177
+ it "should return the Braspag production url" do
178
+ braspag_config[ENV["BRASPAG_ENV"]]["environment"] = "production"
179
+
180
+ YAML.should_receive(:load_file)
181
+ .and_return(braspag_config)
182
+
183
+ connection = Braspag::Connection.clone.instance
184
+ connection.braspag_url.should == braspag_production_url
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,517 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ostruct'
3
+
4
+ describe Braspag::CreditCard do
5
+ let(:braspag_homologation_url) { "https://homologacao.pagador.com.br" }
6
+ let(:braspag_production_url) { "https://transaction.pagador.com.br" }
7
+ let(:merchant_id) { "um id qualquer" }
8
+ let(:logger) { mock(:info => nil) }
9
+
10
+ before do
11
+ @connection = mock(:merchant_id => merchant_id, :protected_card_url => 'https://www.cartaoprotegido.com.br/Services/TestEnvironment', :homologation? => false)
12
+ Braspag::Connection.stub(:instance => @connection)
13
+ end
14
+
15
+ describe ".authorize" do
16
+ let(:params) do
17
+ {
18
+ :order_id => "um order id",
19
+ :customer_name => "W" * 21,
20
+ :amount => "100.00",
21
+ :payment_method => :redecard,
22
+ :holder => "Joao Maria Souza",
23
+ :card_number => "9" * 10,
24
+ :expiration => "10/12",
25
+ :security_code => "123",
26
+ :number_payments => 1,
27
+ :type => 0
28
+ }
29
+ end
30
+
31
+ let(:params_with_merchant_id) do
32
+ params.merge!(:merchant_id => merchant_id)
33
+ end
34
+
35
+ let(:authorize_url) { "http://braspag/bla" }
36
+
37
+ before do
38
+ @connection.should_receive(:merchant_id)
39
+
40
+ Braspag::CreditCard.should_receive(:authorize_url)
41
+ .and_return(authorize_url)
42
+
43
+ Braspag::CreditCard.should_receive(:check_params)
44
+ .and_return(true)
45
+ end
46
+
47
+ context "with invalid params"
48
+
49
+ context "with valid params" do
50
+ let(:valid_xml) do
51
+ <<-EOXML
52
+ <?xml version="1.0" encoding="utf-8"?>
53
+ <PagadorReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
55
+ xmlns="https://www.pagador.com.br/webservice/pagador">
56
+ <amount>5</amount>
57
+ <message>Transaction Successful</message>
58
+ <authorisationNumber>733610</authorisationNumber>
59
+ <returnCode>7</returnCode>
60
+ <status>2</status>
61
+ <transactionId>0</transactionId>
62
+ </PagadorReturn>
63
+ EOXML
64
+ end
65
+
66
+ let(:request) { OpenStruct.new :url => authorize_url }
67
+
68
+ before do
69
+ Braspag::Connection.instance.should_receive(:homologation?)
70
+ ::HTTPI::Request.should_receive(:new).with(authorize_url).and_return(request)
71
+ ::HTTPI.should_receive(:post).with(request).and_return(mock(:body => valid_xml))
72
+ end
73
+
74
+ it "should return a Hash" do
75
+ response = Braspag::CreditCard.authorize(params)
76
+ response.should be_kind_of Hash
77
+ response.should == {
78
+ :amount => "5",
79
+ :message => "Transaction Successful",
80
+ :number => "733610",
81
+ :return_code => "7",
82
+ :status => "2",
83
+ :transaction_id => "0"
84
+ }
85
+ end
86
+
87
+ it "should post transation info" do
88
+ Braspag::CreditCard.authorize(params)
89
+ request.body.should == {"merchantId"=>"um id qualquer", "order"=>"", "orderId"=>"um order id", "customerName"=>"WWWWWWWWWWWWWWWWWWWWW", "amount"=>"100,00", "paymentMethod"=>20, "holder"=>"Joao Maria Souza", "cardNumber"=>"9999999999", "expiration"=>"10/12", "securityCode"=>"123", "numberPayments"=>1, "typePayment"=>0}
90
+ end
91
+ end
92
+ end
93
+
94
+ describe ".capture" do
95
+ let(:capture_url) { "http://foo.bar/bar/baz" }
96
+ let(:order_id) { "um id qualquer" }
97
+
98
+ before do
99
+ @connection.should_receive(:merchant_id)
100
+ end
101
+
102
+ context "invalid order id" do
103
+ it "should raise an error" do
104
+ Braspag::CreditCard.should_receive(:valid_order_id?)
105
+ .with(order_id)
106
+ .and_return(false)
107
+
108
+ expect {
109
+ Braspag::CreditCard.capture(order_id)
110
+ }.to raise_error(Braspag::InvalidOrderId)
111
+ end
112
+ end
113
+
114
+ context "valid order id" do
115
+ let(:valid_xml) do
116
+ <<-EOXML
117
+ <?xml version="1.0" encoding="utf-8"?>
118
+ <PagadorReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
119
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
120
+ xmlns="https://www.pagador.com.br/webservice/pagador">
121
+ <amount>2</amount>
122
+ <message>Approved</message>
123
+ <returnCode>0</returnCode>
124
+ <status>0</status>
125
+ </PagadorReturn>
126
+ EOXML
127
+ end
128
+
129
+ let(:request) { OpenStruct.new :url => capture_url }
130
+
131
+ before do
132
+ Braspag::CreditCard.should_receive(:capture_url)
133
+ .and_return(capture_url)
134
+
135
+ ::HTTPI::Request.should_receive(:new)
136
+ .with(capture_url)
137
+ .and_return(request)
138
+
139
+ ::HTTPI.should_receive(:post)
140
+ .with(request)
141
+ .and_return(mock(:body => valid_xml))
142
+ end
143
+
144
+ it "should return a Hash" do
145
+ response = Braspag::CreditCard.capture("order id qualquer")
146
+ response.should be_kind_of Hash
147
+ response.should == {
148
+ :amount => "2",
149
+ :number => nil,
150
+ :message => "Approved",
151
+ :return_code => "0",
152
+ :status => "0",
153
+ :transaction_id => nil
154
+ }
155
+ end
156
+
157
+ it "should post capture info" do
158
+ Braspag::CreditCard.capture("order id qualquer")
159
+ request.body.should == {"orderId"=>"order id qualquer", "merchantId"=>"um id qualquer"}
160
+ end
161
+ end
162
+ end
163
+
164
+ describe ".partial_capture" do
165
+ let(:partial_capture_url) { "http://foo.bar/bar/partial" }
166
+ let(:order_id) { "um id qualquer" }
167
+
168
+ before do
169
+ @connection.should_receive(:merchant_id)
170
+ end
171
+
172
+ context "invalid order id" do
173
+ it "should raise an error" do
174
+ Braspag::CreditCard.should_receive(:valid_order_id?)
175
+ .with(order_id)
176
+ .and_return(false)
177
+
178
+ expect {
179
+ Braspag::CreditCard.partial_capture(order_id, 10.0)
180
+ }.to raise_error(Braspag::InvalidOrderId)
181
+ end
182
+ end
183
+
184
+ context "valid order id" do
185
+ let(:valid_xml) do
186
+ <<-EOXML
187
+ <?xml version="1.0" encoding="utf-8"?>
188
+ <PagadorReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
189
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
190
+ xmlns="https://www.pagador.com.br/webservice/pagador">
191
+ <amount>2</amount>
192
+ <message>Approved</message>
193
+ <returnCode>0</returnCode>
194
+ <status>0</status>
195
+ </PagadorReturn>
196
+ EOXML
197
+ end
198
+
199
+ let(:request) { OpenStruct.new :url => partial_capture_url }
200
+
201
+ before do
202
+ Braspag::CreditCard.should_receive(:partial_capture_url)
203
+ .and_return(partial_capture_url)
204
+
205
+ ::HTTPI::Request.should_receive(:new)
206
+ .with(partial_capture_url)
207
+ .and_return(request)
208
+
209
+ ::HTTPI.should_receive(:post)
210
+ .with(request)
211
+ .and_return(mock(:body => valid_xml))
212
+ end
213
+
214
+ it "should return a Hash" do
215
+ response = Braspag::CreditCard.partial_capture("order id qualquer", 10.0)
216
+ response.should be_kind_of Hash
217
+ response.should == {
218
+ :amount => "2",
219
+ :number => nil,
220
+ :message => "Approved",
221
+ :return_code => "0",
222
+ :status => "0",
223
+ :transaction_id => nil
224
+ }
225
+ end
226
+
227
+ it "should post capture info" do
228
+ Braspag::CreditCard.partial_capture("order id qualquer", 10.0)
229
+ request.body.should == {"orderId"=>"order id qualquer", "captureAmount"=>"10,00", "merchantId"=>"um id qualquer"}
230
+ end
231
+ end
232
+ end
233
+
234
+ describe ".void" do
235
+ let(:cancellation_url) { "http://foo.bar/bar/baz" }
236
+ let(:order_id) { "um id qualquer" }
237
+
238
+ before do
239
+ @connection.should_receive(:merchant_id)
240
+ end
241
+
242
+ context "invalid order id" do
243
+ it "should raise an error" do
244
+ Braspag::CreditCard.should_receive(:valid_order_id?)
245
+ .with(order_id)
246
+ .and_return(false)
247
+
248
+ expect {
249
+ Braspag::CreditCard.void(order_id)
250
+ }.to raise_error(Braspag::InvalidOrderId)
251
+ end
252
+ end
253
+
254
+ context "valid order id" do
255
+ let(:valid_xml) do
256
+ <<-EOXML
257
+ <?xml version="1.0" encoding="utf-8"?>
258
+ <PagadorReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
259
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
260
+ xmlns="https://www.pagador.com.br/webservice/pagador">
261
+ <amount>2</amount>
262
+ <message>Approved</message>
263
+ <returnCode>0</returnCode>
264
+ <status>0</status>
265
+ </PagadorReturn>
266
+ EOXML
267
+ end
268
+
269
+ let(:request) { OpenStruct.new :url => cancellation_url }
270
+
271
+ before do
272
+ Braspag::CreditCard.should_receive(:cancellation_url)
273
+ .and_return(cancellation_url)
274
+
275
+ ::HTTPI::Request.should_receive(:new).with(cancellation_url).and_return(request)
276
+ ::HTTPI.should_receive(:post).with(request).and_return(mock(:body => valid_xml))
277
+ end
278
+
279
+ it "should return a Hash" do
280
+ response = Braspag::CreditCard.void("order id qualquer")
281
+ response.should be_kind_of Hash
282
+ response.should == {
283
+ :amount => "2",
284
+ :number => nil,
285
+ :message => "Approved",
286
+ :return_code => "0",
287
+ :status => "0",
288
+ :transaction_id => nil
289
+ }
290
+ end
291
+
292
+ it "should post void info" do
293
+ Braspag::CreditCard.void("order id qualquer")
294
+ request.body.should == {"order"=>"order id qualquer", "merchantId"=>"um id qualquer"}
295
+ end
296
+ end
297
+ end
298
+
299
+ describe ".info" do
300
+ let(:info_url) { "http://braspag/bla" }
301
+
302
+ let(:invalid_xml) do
303
+ <<-EOXML
304
+ <DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
305
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
306
+ xmlns="http://www.pagador.com.br/">
307
+ <NumeroComprovante></NumeroComprovante>
308
+ <Autenticada>false</Autenticada>
309
+ <NumeroAutorizacao>557593</NumeroAutorizacao>
310
+ <NumeroCartao>345678*****0007</NumeroCartao>
311
+ <NumeroTransacao>101001225645</NumeroTransacao>
312
+ </DadosCartao>
313
+ EOXML
314
+ end
315
+
316
+ let(:valid_xml) do
317
+ <<-EOXML
318
+ <DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
319
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
320
+ xmlns="http://www.pagador.com.br/">
321
+ <NumeroComprovante>11111</NumeroComprovante>
322
+ <Autenticada>false</Autenticada>
323
+ <NumeroAutorizacao>557593</NumeroAutorizacao>
324
+ <NumeroCartao>345678*****0007</NumeroCartao>
325
+ <NumeroTransacao>101001225645</NumeroTransacao>
326
+ </DadosCartao>
327
+ EOXML
328
+ end
329
+
330
+ it "should raise an error when order id is not valid" do
331
+ Braspag::CreditCard.should_receive(:valid_order_id?)
332
+ .with("bla")
333
+ .and_return(false)
334
+
335
+ expect {
336
+ Braspag::CreditCard.info "bla"
337
+ }.to raise_error(Braspag::InvalidOrderId)
338
+ end
339
+
340
+ it "should raise an error when Braspag returned an invalid xml as response" do
341
+ FakeWeb.register_uri(:post, info_url, :body => invalid_xml)
342
+
343
+ Braspag::CreditCard.should_receive(:info_url)
344
+ .and_return(info_url)
345
+
346
+ expect {
347
+ Braspag::CreditCard.info("orderid")
348
+ }.to raise_error(Braspag::UnknownError)
349
+ end
350
+
351
+ it "should return a Hash when Braspag returned a valid xml as response" do
352
+ FakeWeb.register_uri(:post, info_url, :body => valid_xml)
353
+
354
+ Braspag::CreditCard.should_receive(:info_url)
355
+ .and_return(info_url)
356
+
357
+ response = Braspag::CreditCard.info("orderid")
358
+ response.should be_kind_of Hash
359
+
360
+ response.should == {
361
+ :checking_number => "11111",
362
+ :certified => "false",
363
+ :autorization_number => "557593",
364
+ :card_number => "345678*****0007",
365
+ :transaction_number => "101001225645"
366
+ }
367
+ end
368
+ end
369
+
370
+ describe ".check_params" do
371
+ let(:params) do
372
+ {
373
+ :order_id => 12345,
374
+ :customer_name => "AAAAAAAA",
375
+ :payment_method => :amex_2p,
376
+ :amount => "100.00",
377
+ :holder => "Joao Maria Souza",
378
+ :expiration => "10/12",
379
+ :card_number => "9" * 10,
380
+ :security_code => "123",
381
+ :number_payments => 1,
382
+ :type => 0
383
+ }
384
+ end
385
+
386
+ [:order_id, :amount, :payment_method, :customer_name, :holder, :card_number, :expiration,
387
+ :security_code, :number_payments, :type].each do |param|
388
+ it "should raise an error when #{param} is not present" do
389
+ expect {
390
+ params[param] = nil
391
+ Braspag::CreditCard.check_params(params)
392
+ }.to raise_error Braspag::IncompleteParams
393
+ end
394
+ end
395
+
396
+ it "should raise an error when order_id is not valid" do
397
+ Braspag::CreditCard.should_receive(:valid_order_id?)
398
+ .with(params[:order_id])
399
+ .and_return(false)
400
+
401
+ expect {
402
+ Braspag::CreditCard.check_params(params)
403
+ }.to raise_error Braspag::InvalidOrderId
404
+ end
405
+
406
+ it "should raise an error when payment_method is not invalid" do
407
+ expect {
408
+ params[:payment_method] = "non ecziste"
409
+ Braspag::CreditCard.check_params(params)
410
+ }.to raise_error Braspag::InvalidPaymentMethod
411
+ end
412
+
413
+ it "should raise an error when customer_name is greater than 255 chars" do
414
+ expect {
415
+ params[:customer_name] = "b" * 256
416
+ Braspag::CreditCard.check_params(params)
417
+ }.to raise_error Braspag::InvalidCustomerName
418
+ end
419
+
420
+ it "should raise an error when holder is greater than 100 chars" do
421
+ expect {
422
+ params[:holder] = "r" * 101
423
+ Braspag::CreditCard.check_params(params)
424
+ }.to raise_error Braspag::InvalidHolder
425
+ end
426
+
427
+ it "should raise an error when expiration is not in a valid format" do
428
+ expect {
429
+ params[:expiration] = "2011/19/19"
430
+ Braspag::CreditCard.check_params(params)
431
+ }.to raise_error Braspag::InvalidExpirationDate
432
+
433
+ expect {
434
+ params[:expiration] = "12/2012"
435
+ Braspag::CreditCard.check_params(params)
436
+ }.to_not raise_error Braspag::InvalidExpirationDate
437
+
438
+ expect {
439
+ params[:expiration] = "12/12"
440
+ Braspag::CreditCard.check_params(params)
441
+ }.to_not raise_error Braspag::InvalidExpirationDate
442
+ end
443
+
444
+ it "should raise an error when security code is greater than 4 chars" do
445
+ expect {
446
+ params[:security_code] = "12345"
447
+ Braspag::CreditCard.check_params(params)
448
+ }.to raise_error Braspag::InvalidSecurityCode
449
+
450
+ expect {
451
+ params[:security_code] = ""
452
+ Braspag::CreditCard.check_params(params)
453
+ }.to raise_error Braspag::InvalidSecurityCode
454
+ end
455
+
456
+ it "should raise an error when number_payments is greater than 99" do
457
+ expect {
458
+ params[:number_payments] = 100
459
+ Braspag::CreditCard.check_params(params)
460
+ }.to raise_error Braspag::InvalidNumberPayments
461
+
462
+ expect {
463
+ params[:number_payments] = 0
464
+ Braspag::CreditCard.check_params(params)
465
+ }.to raise_error Braspag::InvalidNumberPayments
466
+ end
467
+ end
468
+
469
+ describe ".info_url" do
470
+ it "should return the correct info url when connection environment is homologation" do
471
+ @connection.stub(:braspag_url => braspag_homologation_url)
472
+ @connection.should_receive(:production?)
473
+ .and_return(false)
474
+
475
+ Braspag::CreditCard.info_url.should == "#{braspag_homologation_url}/pagador/webservice/pedido.asmx/GetDadosCartao"
476
+ end
477
+
478
+ it "should return the correct info url when connection environment is production" do
479
+ @connection.stub(:braspag_url => braspag_production_url)
480
+ @connection.should_receive(:production?)
481
+ .and_return(true)
482
+
483
+ Braspag::CreditCard.info_url.should == "#{braspag_production_url}/webservices/pagador/pedido.asmx/GetDadosCartao"
484
+ end
485
+ end
486
+
487
+ describe ".authorize_url .capture_url .cancellation_url" do
488
+ it "should return the correct credit card creation url when connection environment is homologation" do
489
+ @connection.stub(:braspag_url => braspag_homologation_url)
490
+
491
+ Braspag::CreditCard.authorize_url.should == "#{braspag_homologation_url}/webservices/pagador/Pagador.asmx/Authorize"
492
+ Braspag::CreditCard.capture_url.should == "#{braspag_homologation_url}/webservices/pagador/Pagador.asmx/Capture"
493
+ Braspag::CreditCard.cancellation_url.should == "#{braspag_homologation_url}/webservices/pagador/Pagador.asmx/VoidTransaction"
494
+ end
495
+
496
+ it "should return the correct credit card creation url when connection environment is production" do
497
+ @connection.stub(:braspag_url => braspag_production_url)
498
+
499
+ Braspag::CreditCard.authorize_url.should == "#{braspag_production_url}/webservices/pagador/Pagador.asmx/Authorize"
500
+ Braspag::CreditCard.capture_url.should == "#{braspag_production_url}/webservices/pagador/Pagador.asmx/Capture"
501
+ Braspag::CreditCard.cancellation_url.should == "#{braspag_production_url}/webservices/pagador/Pagador.asmx/VoidTransaction"
502
+ end
503
+ end
504
+
505
+ describe ".save .get .just_click_shop" do
506
+ it "should delegate to ProtectedCreditCard class" do
507
+ Braspag::ProtectedCreditCard.should_receive(:save).with({})
508
+ Braspag::CreditCard.save({})
509
+
510
+ Braspag::ProtectedCreditCard.should_receive(:get).with('just_click_key')
511
+ Braspag::CreditCard.get('just_click_key')
512
+
513
+ Braspag::ProtectedCreditCard.should_receive(:just_click_shop).with({})
514
+ Braspag::CreditCard.just_click_shop({})
515
+ end
516
+ end
517
+ end