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,187 @@
1
+ #encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Crypto::JarWebservice do
5
+ let!(:crypt) {Braspag::Crypto::JarWebservice}
6
+ let! (:key) {"5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7"}
7
+
8
+ after (:each) do
9
+ FakeWeb.clean_registry
10
+ end
11
+
12
+ context "when encrypt data" do
13
+
14
+ it "should return a string" do
15
+ FakeWeb.register_uri(
16
+ :post,
17
+ "http://localhost:9292/v1/encrypt.json",
18
+ :body => <<-EOJSON
19
+ {"encrypt":"5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7"}
20
+ EOJSON
21
+ )
22
+
23
+ crypt.encrypt(:nome => "Chapulin", :sobrenome => "Colorado").should == key
24
+ end
25
+
26
+ it "should raise a error with invalid params" do
27
+ expect {
28
+ crypt.encrypt(9999)
29
+ }.to raise_error(Braspag::IncompleteParams)
30
+ end
31
+
32
+ it "should raise an error with invalid params after process" do
33
+ FakeWeb.register_uri(
34
+ :post,
35
+ "http://localhost:9292/v1/encrypt.json",
36
+ :body => <<-EOJSON
37
+ {
38
+ "msg" : "INVALID FORMAT"
39
+ }
40
+ EOJSON
41
+ )
42
+
43
+ expect {
44
+ crypt.encrypt(:venda => "value")
45
+ }.to raise_error(Braspag::IncompleteParams)
46
+ end
47
+
48
+ it "should raise an error with invalid params after process" do
49
+ FakeWeb.register_uri(
50
+ :post,
51
+ "http://localhost:9292/v1/encrypt.json",
52
+ :body => <<-EOJSON
53
+ INVALIDO
54
+ EOJSON
55
+ )
56
+
57
+ expect {
58
+ crypt.encrypt(:venda => "value")
59
+ }.to raise_error(Braspag::UnknownError)
60
+ end
61
+
62
+ it "should raise an error with invalid params after process" do
63
+ FakeWeb.register_uri(
64
+ :post,
65
+ "http://localhost:9292/v1/encrypt.json",
66
+ :body => <<-EOJSON
67
+ {
68
+ "msg" : "INVALID FIELDS"
69
+ }
70
+ EOJSON
71
+ )
72
+
73
+ expect {
74
+ crypt.encrypt(:venda => nil)
75
+ }.to raise_error(Braspag::IncompleteParams)
76
+ end
77
+
78
+ it "should raise an error with invalid crypt key" do
79
+ FakeWeb.register_uri(
80
+ :post,
81
+ "http://localhost:9292/v1/encrypt.json",
82
+ :body => <<-EOJSON
83
+ {
84
+ "msg" : "INVALID KEY"
85
+ }
86
+ EOJSON
87
+ )
88
+
89
+ expect {
90
+ crypt.encrypt(:venda => "value")
91
+ }.to raise_error(Braspag::InvalidCryptKey)
92
+ end
93
+
94
+ end
95
+
96
+ context "when decrypt data" do
97
+
98
+ it "should return a hash" do
99
+ FakeWeb.register_uri(
100
+ :post,
101
+ "http://localhost:9292/v1/decrypt.json",
102
+ :body => <<-EOJSON
103
+ {"fields":{"nome":"Chapulin","sobrenome":"Colorado"}}
104
+ EOJSON
105
+ )
106
+
107
+ crypt.decrypt(key, [:nome, :sobrenome])[:nome].should eql("Chapulin")
108
+ end
109
+
110
+ it "should raise a error with invalid encrypted key" do
111
+ FakeWeb.register_uri(
112
+ :post,
113
+ "http://localhost:9292/v1/decrypt.json",
114
+ :body => <<-EOJSON
115
+ {
116
+ "msg" : "INVALID ENCRYPTED STRING"
117
+ }
118
+ EOJSON
119
+ )
120
+
121
+ expect {
122
+ crypt.decrypt("1", [:nome, :sobrenome])
123
+ }.to raise_error(Braspag::InvalidEncryptedKey)
124
+ end
125
+
126
+ it "should raise a error with invalid encrypted key" do
127
+ expect {
128
+ crypt.decrypt(1, [:nome, :sobrenome])
129
+ }.to raise_error(Braspag::InvalidEncryptedKey)
130
+ end
131
+
132
+
133
+ it "should raise a error with invalid fields" do
134
+ expect {
135
+ crypt.decrypt(key, 9999)
136
+ }.to raise_error(Braspag::IncompleteParams)
137
+ end
138
+
139
+ it "should raise a error with invalid fields" do
140
+ FakeWeb.register_uri(
141
+ :post,
142
+ "http://localhost:9292/v1/decrypt.json",
143
+ :body => <<-EOJSON
144
+ {
145
+ "msg" : "INVALID FIELDS"
146
+ }
147
+ EOJSON
148
+ )
149
+
150
+ expect {
151
+ crypt.decrypt(key, [:nome, :sobrenome])
152
+ }.to raise_error(Braspag::IncompleteParams)
153
+ end
154
+
155
+ it "should raise an error with invalid params after process" do
156
+ FakeWeb.register_uri(
157
+ :post,
158
+ "http://localhost:9292/v1/decrypt.json",
159
+ :body => <<-EOJSON
160
+ INVALIDO
161
+ EOJSON
162
+ )
163
+
164
+
165
+ expect {
166
+ crypt.decrypt(key, [:nome, :sobrenome])
167
+ }.to raise_error(Braspag::UnknownError)
168
+ end
169
+
170
+ it "should raise an error with invalid crypt key" do
171
+ FakeWeb.register_uri(
172
+ :post,
173
+ "http://localhost:9292/v1/decrypt.json",
174
+ :body => <<-EOJSON
175
+ {
176
+ "msg" : "INVALID KEY"
177
+ }
178
+ EOJSON
179
+ )
180
+
181
+ expect {
182
+ crypt.decrypt(key, [:nome, :sobrenome])
183
+ }.to raise_error(Braspag::InvalidCryptKey)
184
+ end
185
+
186
+ end
187
+ end
@@ -0,0 +1,143 @@
1
+ #encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Crypto::Webservice do
5
+
6
+ context "encrypt" do
7
+ let!(:key) {"XXXXX"}
8
+
9
+ context "consistencies" do
10
+ it "should return error with invalid data" do
11
+ expect {
12
+ Braspag::Crypto::Webservice.encrypt("INVALID DATA")
13
+ }.to raise_error(Braspag::IncompleteParams)
14
+ end
15
+
16
+ it "should return error with invalid data after process" do
17
+ body_invalid = <<-EOXML
18
+ SERVER was unable to process
19
+ EOXML
20
+ FakeWeb.register_uri(:post,
21
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
22
+ :body => body_invalid )
23
+ expect {
24
+ Braspag::Crypto::Webservice.encrypt(:key => "INVALID DATA")
25
+ }.to raise_error(Braspag::UnknownError)
26
+ FakeWeb.clean_registry
27
+ end
28
+
29
+ it "should return error with invalid merchant_id" do
30
+ body_invalid = <<-EOXML
31
+ <?xml version="1.0" encoding="utf-8"?>
32
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
33
+ <soap:Body><EncryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
34
+ <EncryptRequestResult>Erro BP 011</EncryptRequestResult></EncryptRequestResponse>
35
+ </soap:Body></soap:Envelope>
36
+ EOXML
37
+ FakeWeb.register_uri(:post,
38
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
39
+ :body => body_invalid )
40
+ expect {
41
+ Braspag::Crypto::Webservice.encrypt(:key => "value")
42
+ }.to raise_error(Braspag::InvalidMerchantId)
43
+ FakeWeb.clean_registry
44
+ end
45
+
46
+ it "should return error with invalid ip" do
47
+ body_invalid = <<-EOXML
48
+ <?xml version="1.0" encoding="utf-8"?>
49
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
50
+ <soap:Body><EncryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
51
+ <EncryptRequestResult>Erro BP 067</EncryptRequestResult></EncryptRequestResponse>
52
+ </soap:Body></soap:Envelope>
53
+ EOXML
54
+ FakeWeb.register_uri(:post,
55
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
56
+ :body => body_invalid )
57
+ expect {
58
+ Braspag::Crypto::Webservice.encrypt(:key => "value")
59
+ }.to raise_error(Braspag::InvalidIP)
60
+ FakeWeb.clean_registry
61
+ end
62
+ end
63
+
64
+ it "should return a string" do
65
+ FakeWeb.register_uri(:post,
66
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
67
+ :body => <<-EOXML
68
+ <?xml version='1.0' encoding='utf-8'?>
69
+ <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
70
+ <soap:Body>
71
+ <EncryptRequestResponse xmlns='https://www.pagador.com.br/webservice/BraspagGeneralService'>
72
+ <EncryptRequestResult>#{key}</EncryptRequestResult>
73
+ </EncryptRequestResponse>
74
+ </soap:Body></soap:Envelope>
75
+ EOXML
76
+ )
77
+ Braspag::Crypto::Webservice.encrypt(:key => "value").should == key
78
+ FakeWeb.clean_registry
79
+ end
80
+ end
81
+
82
+ context "when decrypt data" do
83
+
84
+ context "consistencies" do
85
+ it "should return error with invalid data" do
86
+ expect {
87
+ Braspag::Crypto::Webservice.decrypt(1213123)
88
+ }.to raise_error(Braspag::IncompleteParams)
89
+ end
90
+
91
+ it "should return error with invalid data" do
92
+ body_invalid = <<-EOXML
93
+ SERVER was unable to process
94
+ EOXML
95
+ FakeWeb.register_uri(:post,
96
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
97
+ :body => body_invalid )
98
+ expect {
99
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")
100
+ }.to raise_error(Braspag::UnknownError)
101
+ FakeWeb.clean_registry
102
+ end
103
+
104
+ it "should return error with invalid ip" do
105
+ body_invalid = <<-EOXML
106
+ <?xml version="1.0" encoding="utf-8"?>
107
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
108
+ <soap:Body><DecryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
109
+ <DecryptRequestResult><string>Erro BP 068</string></DecryptRequestResult>
110
+ </DecryptRequestResponse></soap:Body></soap:Envelope>
111
+ EOXML
112
+ FakeWeb.register_uri(:post,
113
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
114
+ :body => body_invalid )
115
+ expect {
116
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")
117
+ }.to raise_error(Braspag::InvalidIP)
118
+ FakeWeb.clean_registry
119
+ end
120
+ end
121
+
122
+ it "should return a string" do
123
+ FakeWeb.register_uri(:post,
124
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
125
+ :body => <<-EOXML
126
+ <?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
127
+ <soap:Body><DecryptRequestResponse xmlns='https://www.pagador.com.br/webservice/BraspagGeneralService'>
128
+ <DecryptRequestResult>
129
+ <string>CODPAGAMENTO=18</string>
130
+ <string>VENDAID=teste123</string>
131
+ <string>VALOR=100</string>
132
+ <string>PARCELAS=1</string>
133
+ <string>NOME=comprador</string>
134
+ </DecryptRequestResult></DecryptRequestResponse>
135
+ </soap:Body></soap:Envelope>
136
+ EOXML
137
+ )
138
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")[:parcelas].should eql("1")
139
+ FakeWeb.clean_registry
140
+ end
141
+
142
+ end
143
+ end
data/spec/eft_spec.rb ADDED
@@ -0,0 +1,209 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Braspag::Eft do
4
+ let(:braspag_homologation_url) { "https://homologacao.pagador.com.br" }
5
+ let(:braspag_production_url) { "https://transaction.pagador.com.br" }
6
+ let(:merchant_id) { "um id qualquer" }
7
+
8
+ before do
9
+ @connection = mock(:merchant_id => merchant_id)
10
+ Braspag::Connection.stub(:instance => @connection)
11
+ end
12
+
13
+ describe ".generate" do
14
+ let(:params) do
15
+ {
16
+ :order_id => 11,
17
+ :amount => 3,
18
+ :payment_method => :bradesco,
19
+ :installments => 1,
20
+ :has_interest => 0
21
+ }
22
+ end
23
+
24
+ let(:params_with_merchant_id) do
25
+ params.merge!(:merchant_id => merchant_id)
26
+ end
27
+
28
+ let(:action_url) { "https://bla.com/foo/bar/baz" }
29
+
30
+ before do
31
+ @connection.should_receive(:merchant_id)
32
+
33
+ Braspag::Eft.should_receive(:action_url)
34
+ .and_return(action_url)
35
+
36
+ Braspag::Eft.should_receive(:normalize_params)
37
+ .with(params_with_merchant_id)
38
+ .and_return(params_with_merchant_id)
39
+
40
+ Braspag::Eft.should_receive(:check_params)
41
+ .and_return(true)
42
+ end
43
+
44
+ it "should return a html form" do
45
+ html = "<form id=\"form_tef_11\" name=\"form_tef_11\" action=\"#{action_url}\" method=\"post\">" +
46
+ "<input type=\"text\" name=\"Id_Loja\" value=\"#{merchant_id}\" />" +
47
+ '<input type="text" name="VENDAID" value="11" />' +
48
+ '<input type="text" name="nome" value="" />' +
49
+ '<input type="text" name="CPF" value="" />' +
50
+ '<input type="text" name="VALOR" value="3,00" />' +
51
+ '<input type="text" name="CODPAGAMENTO" value="11" />' +
52
+ '<input type="text" name="PARCELAS" value="1" />' +
53
+ '<input type="text" name="TIPOPARCELADO" value="0" />' +
54
+ '</form>' +
55
+ '<script type="text/javascript" charset="utf-8">document.forms["form_tef_11"].submit();</script>'
56
+
57
+ Braspag::Eft.generate(params).should == html
58
+ end
59
+
60
+ it "should return a html form when crypto strategy is given" do
61
+ crypto = mock
62
+ crypto.should_receive(:encrypt)
63
+ .with(a_kind_of(Hash))
64
+ .and_return("vei na boa")
65
+
66
+ html = "<form id=\"form_tef_11\" name=\"form_tef_11\" action=\"#{action_url}\" method=\"post\">" +
67
+ "<input type=\"text\" name=\"Id_Loja\" value=\"#{merchant_id}\" />" +
68
+ '<input type="text" name="crypt" value="vei na boa" />' +
69
+ '</form>' +
70
+ '<script type="text/javascript" charset="utf-8">document.forms["form_tef_11"].submit();</script>'
71
+
72
+ Braspag::Eft.generate(params, crypto).should == html
73
+ end
74
+ end
75
+
76
+ describe ".normalize_params" do
77
+ it "should convert amount to BigDecimal" do
78
+ result = Braspag::Eft.normalize_params({ :amount => "100.2" })
79
+ result.should be_kind_of Hash
80
+
81
+ result[:amount].should be_kind_of BigDecimal
82
+ end
83
+
84
+ it "should transform installments to Integer" do
85
+ result = Braspag::Eft.normalize_params({ :installments => "12" })
86
+ result.should be_kind_of Hash
87
+
88
+ result[:installments].should be_kind_of Integer
89
+ result[:installments].should == 12
90
+ end
91
+
92
+ it "should assign 1 to installments if installments is not present" do
93
+ result = Braspag::Eft.normalize_params({})
94
+ result.should be_kind_of Hash
95
+
96
+ result[:installments].should == 1
97
+ end
98
+
99
+ it "should transform has_interest into String" do
100
+ result = Braspag::Eft.normalize_params({ :has_interest => true })
101
+ result.should be_kind_of Hash
102
+
103
+ result[:has_interest].should == "1"
104
+
105
+ result = Braspag::Eft.normalize_params({ :has_interest => false })
106
+ result.should be_kind_of Hash
107
+
108
+ result[:has_interest].should == "0"
109
+ end
110
+ end
111
+
112
+ describe ".check_params" do
113
+ let(:params) do
114
+ {
115
+ :order_id => "111",
116
+ :amount => 100.0,
117
+ :payment_method => :bradesco
118
+ }
119
+ end
120
+
121
+ [:order_id, :amount, :payment_method].each do |param|
122
+ it "should raise an error when #{param} is not present" do
123
+ expect {
124
+ params[param] = nil
125
+ Braspag::Eft.check_params(params)
126
+ }.to raise_error Braspag::IncompleteParams
127
+ end
128
+ end
129
+
130
+ it "should raise an error when order_id is not valid" do
131
+ Braspag::Eft.should_receive(:valid_order_id?)
132
+ .with(params[:order_id])
133
+ .and_return(false)
134
+
135
+ expect {
136
+ Braspag::Eft.check_params(params)
137
+ }.to raise_error Braspag::InvalidOrderId
138
+ end
139
+
140
+ it "should raise an error when customer_name is present and is greater than 255 chars" do
141
+ expect {
142
+ params[:customer_name] = "b" * 256
143
+ Braspag::Eft.check_params(params)
144
+ }.to raise_error Braspag::InvalidCustomerName
145
+ end
146
+
147
+ it "should raise an error when customer_id is present and is greater than 18 chars" do
148
+ expect {
149
+ params[:customer_id] = "1" * 19
150
+ Braspag::Eft.check_params(params)
151
+ }.to raise_error Braspag::InvalidCustomerId
152
+ end
153
+
154
+ it "should raise an error when customer_id is present and is less than 11 chars" do
155
+ expect {
156
+ params[:customer_id] = "1" * 10
157
+ Braspag::Eft.check_params(params)
158
+ }.to raise_error Braspag::InvalidCustomerId
159
+ end
160
+
161
+ it "should raise an error when installments is present and is greater than 99" do
162
+ expect {
163
+ params[:installments] = 100
164
+ Braspag::Eft.check_params(params)
165
+ }.to raise_error Braspag::InvalidInstallments
166
+ end
167
+
168
+ it "should raise an error when installments is present and is less than 99" do
169
+ expect {
170
+ params[:installments] = 0
171
+ Braspag::Eft.check_params(params)
172
+ }.to raise_error Braspag::InvalidInstallments
173
+
174
+ expect {
175
+ params[:installments] = 1
176
+ Braspag::Eft.check_params(params)
177
+ }.to_not raise_error Braspag::InvalidInstallments
178
+ end
179
+
180
+ it "should raise an error when has_interest is present and is invalid" do
181
+ expect {
182
+ params[:has_interest] = "string"
183
+ Braspag::Eft.check_params(params)
184
+ }.to raise_error Braspag::InvalidHasInterest
185
+
186
+ expect {
187
+ params[:has_interest] = "1"
188
+ Braspag::Eft.check_params(params)
189
+ }.to_not raise_error Braspag::InvalidHasInterest
190
+
191
+ expect {
192
+ params[:has_interest] = "0"
193
+ Braspag::Eft.check_params(params)
194
+ }.to_not raise_error Braspag::InvalidHasInterest
195
+ end
196
+ end
197
+
198
+ describe ".action_url" do
199
+ it "should return the correct eft creation url when connection environment is homologation" do
200
+ @connection.stub(:braspag_url => braspag_homologation_url)
201
+ Braspag::Eft.action_url.should == "#{braspag_homologation_url}/pagador/passthru.asp"
202
+ end
203
+
204
+ it "should return the correct eft creation url when connection environment is production" do
205
+ @connection.stub(:braspag_url => braspag_production_url)
206
+ Braspag::Eft.action_url.should == "#{braspag_production_url}/pagador/passthru.asp"
207
+ end
208
+ end
209
+ end