rbraspag 0.0.19 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  module Braspag
2
2
  class Eft < PaymentMethod
3
+
3
4
  PAYMENT_METHODS = {
4
5
  :bradesco => 11,
5
6
  :itau => 12,
@@ -20,92 +21,70 @@ module Braspag
20
21
  :has_interest => "TIPOPARCELADO"
21
22
  }
22
23
 
24
+ ACTION_URI = "/pagador/passthru.asp"
25
+
23
26
  def self.generate(params, crypto_strategy = nil)
24
27
  connection = Braspag::Connection.instance
25
-
26
28
  params[:merchant_id] = connection.merchant_id
27
29
 
28
- if params[:amount] && !params[:amount].is_a?(BigDecimal)
29
- params[:amount] = BigDecimal.new(params[:amount].to_s)
30
- end
31
-
32
- raise IncompleteParams if params[:order_id].nil? || params[:amount].nil? || params[:payment_method].nil?
33
-
34
- raise InvalidOrderId unless params[:order_id].is_a?(String) || params[:order_id].is_a?(Fixnum)
35
- raise InvalidOrderId unless (1..50).include?(params[:order_id].to_s.size)
30
+ params = self.normalize_params(params)
31
+ self.check_params(params)
36
32
 
37
- params[:installments] = "1" if params[:installments].nil?
38
- params[:installments] = "1" if params[:installments].nil?
39
-
40
- unless params[:customer_name].nil?
41
- raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
42
- end
33
+ data = {}
43
34
 
44
- unless params[:customer_id].nil?
45
- raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
46
- end
47
-
48
- unless params[:installments].nil?
49
- raise InvalidInstallments unless (1..2).include?(params[:installments].to_s.size)
50
- begin
51
- params[:installments] = Integer(params[:installments]) unless params[:installments].is_a?(Integer)
52
- rescue Exception => e
53
- raise InvalidInstallments
35
+ MAPPING.each do |k, v|
36
+ case k
37
+ when :payment_method
38
+ data[v] = PAYMENT_METHODS[params[:payment_method]]
39
+ when :amount
40
+ data[v] = Utils.convert_decimal_to_string(params[:amount])
41
+ else
42
+ data[v] = params[k] || ""
54
43
  end
55
44
  end
56
45
 
57
- unless params[:has_interest].nil?
58
- raise InvalidHasInterest unless (params[:has_interest].is_a?(TrueClass) || params[:has_interest].is_a?(FalseClass))
59
- end
60
-
61
- if params[:has_interest]
62
- params[:has_interest] = "1"
63
- else
64
- params[:has_interest] = "0"
65
- end
66
-
67
- data = create_data(params)
68
-
69
- html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
46
+ html = "<form id=\"form_tef_#{params[:order_id]}\" name=\"form_tef_#{params[:order_id]}\" action=\"#{self.action_url}\" method=\"post\">"
70
47
 
71
48
  if crypto_strategy.nil?
72
49
  data.each do |key, value|
73
- html.concat " <input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
50
+ html << "<input type=\"text\" name=\"#{key}\" value=\"#{value}\" />"
74
51
  end
75
52
  else
76
53
  data.delete("Id_Loja")
77
- html.concat " <input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />\n"
78
- html.concat " <input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />\n"
54
+ html << "<input type=\"text\" name=\"Id_Loja\" value=\"#{params[:merchant_id]}\" />"
55
+ html << "<input type=\"text\" name=\"crypt\" value=\"#{crypto_strategy.encrypt(data)}\" />"
79
56
  end
80
57
 
81
- html.concat <<-EOHTML
82
- </form>
83
- <script type="text/javascript" charset="utf-8">
84
- document.forms["form_tef_#{params[:order_id]}"].submit();
85
- </script>
86
- EOHTML
58
+ html << "</form><script type=\"text/javascript\" charset=\"utf-8\">document.forms[\"form_tef_#{params[:order_id]}\"].submit();</script>"
87
59
 
88
60
  html
89
61
  end
90
62
 
91
- protected
92
- def self.create_data(params)
93
- MAPPING.inject({}) do |memo, k|
94
- if k[0] == :payment_method
95
- memo[k[1]] = PAYMENT_METHODS[params[:payment_method]]
96
- elsif k[0] == :amount
97
- memo[k[1]] = Utils.convert_decimal_to_string(params[:amount])
98
- else
99
- memo[k[1]] = params[k[0]] || "";
100
- end
63
+ def self.normalize_params(params)
64
+ params = super
65
+
66
+ params[:installments] = params[:installments].to_i unless params[:installments].nil?
67
+ params[:installments] ||= 1
68
+
69
+ params[:has_interest] = params[:has_interest] == true ? "1" : "0"
101
70
 
102
- memo
71
+ params
72
+ end
73
+
74
+ def self.check_params(params)
75
+ super
76
+
77
+ if params[:installments]
78
+ raise InvalidInstallments if params[:installments].to_i < 1 || params[:installments].to_i > 99
79
+ end
80
+
81
+ if params[:has_interest]
82
+ raise InvalidHasInterest if params[:has_interest] != "1" && params[:has_interest] != "0"
103
83
  end
104
84
  end
105
85
 
106
- def self.uri
107
- connection = Braspag::Connection.instance
108
- "#{connection.braspag_url}/pagador/passthru.asp"
86
+ def self.action_url
87
+ Braspag::Connection.instance.braspag_url + ACTION_URI
109
88
  end
110
89
  end
111
90
  end
@@ -24,4 +24,14 @@ module Braspag
24
24
  class InvalidType < Exception ; end
25
25
  class InvalidNumberPayments < Exception ; end
26
26
  class UnknownError < Exception ; end
27
+
28
+ class Connection
29
+ class InvalidMerchantId < Exception ; end
30
+ class InvalidEnv < Exception ; end
31
+ class InvalidBraspagUrl < Exception ; end
32
+ end
33
+
34
+ class Order
35
+ class InvalidData < Exception; end
36
+ end
27
37
  end
@@ -1,15 +1,17 @@
1
1
  module Braspag
2
2
  class Order
3
- class InvalidData < Exception; end
3
+ PRODUCTION_INFO_URI = "/webservices/pagador/pedido.asmx/GetDadosPedido"
4
+ HOMOLOGATION_INFO_URI = "/pagador/webservice/pedido.asmx/GetDadosPedido"
4
5
 
5
6
  def self.status(order_id)
6
7
  connection = Braspag::Connection.instance
7
8
 
8
- raise InvalidOrderId unless order_id.is_a?(String) || order_id.is_a?(Fixnum)
9
- raise InvalidOrderId unless (1..50).include?(order_id.to_s.size)
9
+ raise InvalidOrderId unless Braspag::PaymentMethod.valid_order_id?(order_id)
10
10
 
11
- request = ::HTTPI::Request.new("#{connection.braspag_query_url}/GetDadosPedido")
12
- request.body = {:loja => connection.merchant_id, :numeroPedido => order_id.to_s}
11
+ request = ::HTTPI::Request.new(self.status_url)
12
+ request.body = {
13
+ :loja => connection.merchant_id, :numeroPedido => order_id.to_s
14
+ }
13
15
 
14
16
  response = ::HTTPI.post(request)
15
17
 
@@ -31,7 +33,11 @@ module Braspag
31
33
 
32
34
  raise InvalidData if response[:authorization].nil?
33
35
  response
36
+ end
34
37
 
38
+ def self.status_url
39
+ connection = Braspag::Connection.instance
40
+ connection.braspag_url + (connection.production? ? PRODUCTION_INFO_URI : HOMOLOGATION_INFO_URI)
35
41
  end
36
42
  end
37
43
  end
@@ -3,5 +3,37 @@ module Braspag
3
3
  def self.payment_method_from_id(code)
4
4
  self::PAYMENT_METHODS.invert.values_at(code).first
5
5
  end
6
+
7
+ def self.normalize_params(params)
8
+ if params[:amount] && !params[:amount].is_a?(BigDecimal)
9
+ params[:amount] = BigDecimal.new(params[:amount].to_s)
10
+ end
11
+
12
+ params
13
+ end
14
+
15
+ def self.check_params(params)
16
+ [:order_id, :amount, :payment_method].each do |param|
17
+ raise IncompleteParams if params[param].nil?
18
+ end
19
+
20
+ raise InvalidOrderId unless self.valid_order_id?(params[:order_id])
21
+
22
+ if params[:customer_name]
23
+ raise InvalidCustomerName unless (1..255).include?(params[:customer_name].to_s.size)
24
+ end
25
+
26
+ if params[:customer_id]
27
+ raise InvalidCustomerId unless (11..18).include?(params[:customer_id].to_s.size)
28
+ end
29
+
30
+ unless params[:payment_method].is_a?(Symbol) && self::PAYMENT_METHODS[params[:payment_method]]
31
+ raise InvalidPaymentMethod
32
+ end
33
+ end
34
+
35
+ def self.valid_order_id?(order_id)
36
+ (order_id.is_a?(String) || order_id.is_a?(Fixnum)) && (1..50).include?(order_id.to_s.size)
37
+ end
6
38
  end
7
39
  end
@@ -1,32 +1,29 @@
1
1
  module Braspag
2
2
  class Utils
3
3
  def self.convert_decimal_to_string(value)
4
- cents = "0#{((value - value.to_i) * 100).to_i}".slice(-2,2)
5
- integer = (value - (value - value.to_i)).to_i
6
- "#{integer},#{cents}"
4
+ ("%.2f" % value.to_f).gsub('.', ',')
7
5
  end
8
6
 
9
7
  def self.convert_to_map(document, map = {})
10
8
  document = Nokogiri::XML(document)
11
9
 
12
- map.each do |keyForMap , keyValue|
13
- if keyValue.is_a?(String) || keyValue.nil?
14
- keyValue = keyForMap if keyValue.nil?
10
+ map.each do |key, value|
11
+ if value.is_a?(String) || value.nil?
12
+ value = key if value.nil?
15
13
 
16
- value = document.search(keyValue).first
17
- if !value.nil?
18
- value = value.content.to_s
19
- map[keyForMap] = value unless value == ""
20
- map[keyForMap] = nil if value == ""
14
+ new_value = document.search(value).first
15
+
16
+ if new_value.nil?
17
+ map[key] = nil
21
18
  else
22
- map[keyForMap] = nil
19
+ new_value = new_value.content.to_s
20
+ map[key] = new_value unless new_value == ""
21
+ map[key] = nil if new_value == ""
23
22
  end
24
23
 
25
- elsif keyValue.is_a?(Proc)
26
- map[keyForMap] = keyValue.call(document)
24
+ elsif value.is_a?(Proc)
25
+ map[key] = value.call(document)
27
26
  end
28
-
29
- map[keyForMap]
30
27
  end
31
28
 
32
29
  map
@@ -1,3 +1,3 @@
1
1
  module Braspag
2
- VERSION = "0.0.19"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency 'cs-httpi', '>= 0.9.5.2'
22
22
  s.add_dependency 'json', '>= 1.6.1'
23
- s.add_dependency 'nokogiri', '>= 1.5.0'
23
+ s.add_dependency 'nokogiri', '>= 1.4.7'
24
24
 
25
25
  s.add_development_dependency "rake"
26
26
  s.add_development_dependency "rspec"
@@ -1,272 +1,47 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
 
4
4
  describe Braspag::Bill do
5
- let!(:braspag_url) { "https://homologacao.pagador.com.br" }
6
- let!(:braspag_query_url) { "https://homologacao.pagador.com.br/pagador/webservice/pedido.asmx" }
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" }
7
8
 
8
- describe ".generate" do
9
- context "consitencies" do
10
- before do
11
- xml = <<-EOXML
12
- <?xml version="1.0" encoding="utf-8"?>
13
- <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://www.pagador.com.br/webservice/pagador">
14
- <amount>1234.56</amount>
15
- <boletoNumber>70345</boletoNumber>
16
- <expirationDate>2011-08-13T00:00:00-03:00</expirationDate>
17
- <url>https://homologacao.pagador.com.br/pagador/reenvia.asp?Id_Transacao=0224306d-d483-4026-9022-a855a645e7ec</url>
18
- <returnCode>0</returnCode>
19
- <status>0</status>
20
- </PagadorBoletoReturn>
21
- EOXML
22
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
23
- end
24
-
25
- after do
26
- FakeWeb.clean_registry
27
- end
28
-
29
- it "should raise an error when :order_id is not present" do
30
- expect {
31
- Braspag::Bill.generate( {
32
- :amount => "100.00",
33
- :payment_method => :bradesco
34
- })
35
- }.to raise_error(Braspag::IncompleteParams)
36
- end
37
-
38
- it "should raise an error when :amount is not present" do
39
- expect {
40
- Braspag::Bill.generate( {
41
- :order_id => "12",
42
- :payment_method => :bradesco
43
- })
44
- }.to raise_error(Braspag::IncompleteParams)
45
- end
46
-
47
- it "should raise an error when :payment_method is not present" do
48
- expect {
49
- Braspag::Bill.generate( {
50
- :order_id => "13",
51
- :amount => "120.00"
52
- })
53
- }.to raise_error(Braspag::IncompleteParams)
54
- end
55
-
56
- it "should raise an error when invalid string :payment_method" do
57
- expect {
58
- Braspag::Bill.generate( {
59
- :order_id => "13",
60
- :amount => "120.00",
61
- :payment_method => "10"
62
- })
63
- }.to raise_error(Braspag::InvalidPaymentMethod)
64
- end
65
-
66
- it "should raise an error when invalid symbol :payment_method" do
67
- expect {
68
- Braspag::Bill.generate( {
69
- :order_id => "13",
70
- :amount => "120.00",
71
- :payment_method => :invalid
72
- })
73
- }.to raise_error(Braspag::InvalidPaymentMethod)
74
- end
75
-
76
- it "should raise an error when :order_id is less than 1 character" do
77
- expect {
78
- Braspag::Bill.generate( {
79
- :order_id => "",
80
- :amount => "123.00",
81
- :payment_method => :bradesco
82
- })
83
- }.to raise_error(Braspag::InvalidOrderId)
84
- end
85
-
86
- it "should raise an error when :order_id is more than 50 characters" do
87
- expect {
88
- Braspag::Bill.generate( {
89
- :order_id => "1" * 51,
90
- :amount => "12.00",
91
- :payment_method => :bradesco
92
- })
93
- }.to raise_error(Braspag::InvalidOrderId)
94
- end
95
-
96
- it "should raise an error when :customer_name is less than 1 character" do
97
- expect {
98
- Braspag::Bill.generate( {
99
- :order_id => "102",
100
- :amount => "42.00",
101
- :payment_method => :bradesco,
102
- :customer_name => ""
103
- })
104
- }.to raise_error(Braspag::InvalidCustomerName)
105
- end
106
-
107
- it "should raise an error when :customer_name is more than 255 characters" do
108
- expect {
109
- Braspag::Bill.generate( {
110
- :order_id => "112",
111
- :amount => "121.00",
112
- :payment_method => :bradesco,
113
- :customer_name => "A" * 256
114
- })
115
- }.to raise_error(Braspag::InvalidCustomerName)
116
- end
117
-
118
- it "should raise an error when :customer_id is less than 11 characters" do
119
- expect {
120
- Braspag::Bill.generate( {
121
- :order_id => "23",
122
- :amount => "251.00",
123
- :payment_method => :bradesco,
124
- :customer_id => "2" * 10
125
- })
126
- }.to raise_error(Braspag::InvalidCustomerId)
127
- end
128
-
129
- it "should raise an error when :customer_id is more than 18 characters" do
130
- expect {
131
- Braspag::Bill.generate( {
132
- :order_id => "90",
133
- :amount => "90.00",
134
- :payment_method => :bradesco,
135
- :customer_id => "3" * 19
136
- })
137
- }.to raise_error(Braspag::InvalidCustomerId)
138
- end
139
-
140
- it "should raise an error when :number is less than 1 character" do
141
- expect {
142
- Braspag::Bill.generate( {
143
- :order_id => "900",
144
- :amount => "92.00",
145
- :payment_method => :bradesco,
146
- :number => ""
147
- })
148
- }.to raise_error(Braspag::InvalidNumber)
149
- end
150
-
151
- it "should raise an error when :number is more than 255 characters" do
152
- expect {
153
- Braspag::Bill.generate( {
154
- :order_id => "91",
155
- :amount => "80.00",
156
- :payment_method => :bradesco,
157
- :number => "5" * 256
158
- })
159
- }.to raise_error(Braspag::InvalidNumber)
160
- end
161
-
162
- it "should raise an error when :instructions is less than 1 character" do
163
- expect {
164
- Braspag::Bill.generate( {
165
- :order_id => "76",
166
- :amount => "50.00",
167
- :payment_method => :bradesco,
168
- :instructions => ""
169
- })
170
- }.to raise_error(Braspag::InvalidInstructions)
171
- end
172
-
173
- it "should raise an error when :instructions is more than 512 characters" do
174
- expect {
175
- Braspag::Bill.generate( {
176
- :order_id => "65",
177
- :amount => "210.00",
178
- :payment_method => :bradesco,
179
- :instructions => "O" * 513
180
- })
181
- }.to raise_error(Braspag::InvalidInstructions)
182
- end
183
-
184
- it "should raise an error when :expiration_date is more or less than 8 characters" do
185
- expect {
186
- Braspag::Bill.generate( {
187
- :order_id => "34",
188
- :amount => "245.00",
189
- :payment_method => :bradesco,
190
- :expiration_date => "1" * 7
191
- })
192
- }.to raise_error(Braspag::InvalidExpirationDate)
193
-
194
- expect {
195
- Braspag::Bill.generate( {
196
- :order_id => "67",
197
- :amount => "321.00",
198
- :payment_method => :bradesco,
199
- :expiration_date => "2" * 9
200
- })
201
- }.to raise_error(Braspag::InvalidExpirationDate)
202
- end
9
+ before do
10
+ @connection = mock(:merchant_id => merchant_id)
11
+ Braspag::Connection.stub(:instance => @connection)
12
+ end
203
13
 
204
- it "should accept :payment_method as Symbol Object and convert to relative String value" do
205
- Braspag::Bill.generate( {
206
- :order_id => "67",
207
- :amount => "321.00",
208
- :payment_method => :itau,
209
- :expiration_date => Date.today + 2
210
- }).should be_instance_of(Hash)
211
- end
14
+ describe ".generate" do
15
+ let(:params) do
16
+ {
17
+ :order_id => 11,
18
+ :amount => 3,
19
+ :payment_method => :hsbc
20
+ }
21
+ end
212
22
 
213
- it "should accept :expiration_date with Date Object" do
214
- Braspag::Bill.generate( {
215
- :order_id => "67",
216
- :amount => "321.00",
217
- :payment_method => :bradesco,
218
- :expiration_date => Date.today + 2
219
- }).should be_instance_of(Hash)
220
- end
23
+ let(:params_with_merchant_id) do
24
+ params.merge!(:merchant_id => merchant_id)
25
+ end
221
26
 
222
- it "should accept :expiration_date with valid string date" do
223
- Braspag::Bill.generate( {
224
- :order_id => "67",
225
- :amount => "321.00",
226
- :payment_method => :bradesco,
227
- :expiration_date => (Date.today + 2).strftime("%d/%m/%y")
228
- }).should be_instance_of(Hash)
229
- end
27
+ let(:creation_url) { "https://bla.com/foo/bar/baz" }
230
28
 
231
- it "should accept string as :amount" do
232
- Braspag::Bill.generate( {
233
- :order_id => "67",
234
- :amount => "54321.45",
235
- :payment_method => :bradesco,
236
- :expiration_date => (Date.today + 2).strftime("%d/%m/%y")
237
- }).should be_instance_of(Hash)
238
- end
29
+ before do
30
+ @connection.should_receive(:merchant_id)
239
31
 
240
- it "should accept integer as :amount" do
241
- Braspag::Bill.generate( {
242
- :order_id => "67",
243
- :amount => 123,
244
- :payment_method => :bradesco,
245
- :expiration_date => (Date.today + 2).strftime("%d/%m/%y")
246
- }).should be_instance_of(Hash)
247
- end
32
+ Braspag::Bill.should_receive(:creation_url)
33
+ .and_return(creation_url)
248
34
 
249
- it "should accept BigDecimal as :amount" do
250
- Braspag::Bill.generate( {
251
- :order_id => "67",
252
- :amount => BigDecimal.new("123.45"),
253
- :payment_method => :bradesco,
254
- :expiration_date => (Date.today + 2).strftime("%d/%m/%y")
255
- }).should be_instance_of(Hash)
256
- end
35
+ Braspag::Bill.should_receive(:normalize_params)
36
+ .with(params_with_merchant_id)
37
+ .and_return(params_with_merchant_id)
257
38
 
258
- it "should accept integer as :amount" do
259
- Braspag::Bill.generate( {
260
- :order_id => "67",
261
- :amount => 12345,
262
- :payment_method => :bradesco,
263
- :expiration_date => (Date.today + 2).strftime("%d/%m/%y")
264
- }).should be_instance_of(Hash)
265
- end
39
+ Braspag::Bill.should_receive(:check_params)
40
+ .and_return(true)
266
41
  end
267
42
 
268
- context "with incorrect data" do
269
- it "should raise an error for invalid :merchantId" do
43
+ context "with invalid params" do
44
+ it "should raise an error when Braspag returns 'Invalid merchantId' as response" do
270
45
  xml = <<-EOXML
271
46
  <?xml version="1.0" encoding="utf-8"?>
272
47
  <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -280,20 +55,14 @@ describe Braspag::Bill do
280
55
  </PagadorBoletoReturn>
281
56
  EOXML
282
57
 
283
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
58
+ FakeWeb.register_uri(:post, creation_url, :body => xml)
284
59
 
285
60
  expect {
286
- Braspag::Bill.generate( {
287
- :order_id => 1,
288
- :amount => 3,
289
- :payment_method => :hsbc
290
- })
61
+ Braspag::Bill.generate(params)
291
62
  }.to raise_error(Braspag::InvalidMerchantId)
292
-
293
- FakeWeb.clean_registry
294
63
  end
295
64
 
296
- it "should raise an error for invalid :number" do
65
+ it "should raise an error when Braspag returns 'Input string was not in a correct format' as response" do
297
66
  xml = <<-EOXML
298
67
  <?xml version="1.0" encoding="utf-8"?>
299
68
  <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -307,21 +76,14 @@ describe Braspag::Bill do
307
76
  </PagadorBoletoReturn>
308
77
  EOXML
309
78
 
310
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
79
+ FakeWeb.register_uri(:post, creation_url, :body => xml)
311
80
 
312
81
  expect {
313
- Braspag::Bill.generate( {
314
- :number => "A" * 50,
315
- :order_id => "x",
316
- :amount => 3,
317
- :payment_method => :hsbc
318
- })
82
+ Braspag::Bill.generate(params)
319
83
  }.to raise_error(Braspag::InvalidStringFormat)
320
-
321
- FakeWeb.clean_registry
322
84
  end
323
85
 
324
- it "should raise an error for invalid :payment_method" do
86
+ it "should raise an error when Braspag returns 'Invalid payment method' as response" do
325
87
  xml = <<-EOXML
326
88
  <?xml version="1.0" encoding="utf-8"?>
327
89
  <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -335,20 +97,14 @@ describe Braspag::Bill do
335
97
  </PagadorBoletoReturn>
336
98
  EOXML
337
99
 
338
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
100
+ FakeWeb.register_uri(:post, creation_url, :body => xml)
339
101
 
340
102
  expect {
341
- bill = Braspag::Bill.generate( {
342
- :order_id => 1,
343
- :amount => "0000",
344
- :payment_method => :hsbc
345
- })
103
+ Braspag::Bill.generate(params)
346
104
  }.to raise_error(Braspag::InvalidPaymentMethod)
347
-
348
- FakeWeb.clean_registry
349
105
  end
350
106
 
351
- it "should raise an error for invalid :amount" do
107
+ it "should raise an error when Braspag returns 'Invalid purchase amount' as response" do
352
108
  xml = <<-EOXML
353
109
  <?xml version="1.0" encoding="utf-8"?>
354
110
  <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -362,20 +118,14 @@ describe Braspag::Bill do
362
118
  </PagadorBoletoReturn>
363
119
  EOXML
364
120
 
365
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
121
+ FakeWeb.register_uri(:post, creation_url, :body => xml)
366
122
 
367
123
  expect {
368
- bill = Braspag::Bill.generate( {
369
- :order_id => 1,
370
- :amount => -33,
371
- :payment_method => :hsbc
372
- })
124
+ Braspag::Bill.generate(params)
373
125
  }.to raise_error(Braspag::InvalidAmount)
374
-
375
- FakeWeb.clean_registry
376
126
  end
377
127
 
378
- it "should raise an error for unknown problems" do
128
+ it "should raise an error when Braspag returns any other error as response" do
379
129
  xml = <<-EOXML
380
130
  <?xml version="1.0" encoding="utf-8"?>
381
131
  <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -389,164 +139,244 @@ describe Braspag::Bill do
389
139
  </PagadorBoletoReturn>
390
140
  EOXML
391
141
 
392
- FakeWeb.register_uri(:post, "#{braspag_url}/webservices/pagador/Boleto.asmx/CreateBoleto", :body => xml)
142
+ FakeWeb.register_uri(:post, creation_url, :body => xml)
393
143
 
394
144
  expect {
395
- bill = Braspag::Bill.generate( {
396
- :order_id => 1,
397
- :amount => 3,
398
- :payment_method => :real
399
- })
145
+ Braspag::Bill.generate(params)
400
146
  }.to raise_error(Braspag::UnknownError)
401
-
402
- FakeWeb.clean_registry
403
147
  end
404
148
  end
405
149
 
150
+ context "with valid params" do
151
+ let(:valid_xml) do
152
+ <<-EOXML
153
+ <?xml version="1.0" encoding="utf-8"?>
154
+ <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
155
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
156
+ xmlns="https://www.pagador.com.br/webservice/pagador">
157
+ <amount>3.00</amount>
158
+ <boletoNumber>123123</boletoNumber>
159
+ <expirationDate>2012-01-08T00:00:00</expirationDate>
160
+ <url>https://homologacao.pagador.com.br/pagador/reenvia.asp?Id_Transacao=722934be-6756-477a-87ab-42115ee1424d</url>
161
+ <returnCode>0</returnCode>
162
+ <status>0</status>
163
+ </PagadorBoletoReturn>
164
+ EOXML
165
+ end
406
166
 
407
- context "with all data" do
408
- before(:all) do
409
- @tomorrow = (Time.now + 3600 * 24 * 2)
410
-
411
- data = {
412
- :order_id => Time.now.to_i.to_s,
167
+ let(:params) do
168
+ {
169
+ :order_id => 2901,
413
170
  :amount => 3,
414
171
  :payment_method => :real,
415
172
  :number => "123123",
416
- :expiration_date => @tomorrow.strftime("%d/%m/%y")
173
+ :expiration_date => Date.today.strftime("%d/%m/%y")
417
174
  }
418
-
419
- @response = Braspag::Bill.generate( data)
420
175
  end
421
176
 
422
- it "should return the bill number" do
423
- @response[:number].should == "123123"
177
+ before do
178
+ FakeWeb.register_uri(:post, creation_url, :body => valid_xml)
179
+ @response = Braspag::Bill.generate(params)
424
180
  end
425
181
 
426
- it "should return the expiration date" do
427
- @response[:expiration_date].should be_kind_of Date
428
- @response[:expiration_date].strftime("%d/%m/%Y").should == @tomorrow.strftime("%d/%m/%Y")
182
+ it "should return a Hash" do
183
+ @response.should be_kind_of Hash
184
+ @response.should == {
185
+ :url => "https://homologacao.pagador.com.br/pagador/reenvia.asp?Id_Transacao=722934be-6756-477a-87ab-42115ee1424d",
186
+ :amount => BigDecimal.new("3.00"),
187
+ :number => "123123",
188
+ :expiration_date => Date.new(2012, 1, 8),
189
+ :return_code => "0",
190
+ :status => "0",
191
+ :message => nil
192
+ }
429
193
  end
430
194
  end
195
+ end
431
196
 
432
- context "with minimum correct data" do
433
- before(:all) do
434
- data = {
435
- :order_id => Time.now.to_i.to_s,
436
- :amount => 1234.56,
437
- :payment_method => :real
438
- }
439
- @response = Braspag::Bill.generate( data)
440
- end
197
+ describe ".normalize_params" do
198
+ it "should format the expiration_date param" do
199
+ params = { :expiration_date => Date.new(2011, 12, 10) }
441
200
 
442
- it "should return a public url" do
443
- regexp = %r"https://homologacao\.pagador\.com\.br/pagador/reenvia\.asp\?Id_Transacao=[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12}"
444
- @response[:url].should match(regexp)
445
- end
201
+ result = Braspag::Bill.normalize_params(params)
202
+ result.should be_kind_of Hash
446
203
 
447
- it "should return 0 (waiting payment) as the status" do
448
- @response[:status].should == "0"
449
- end
204
+ result[:expiration_date].should =~ /10\/12\/11/
205
+ end
450
206
 
451
- it "should return 0 (success) as the return_code" do
452
- @response[:return_code].should == "0"
453
- end
207
+ it "should convert amount to BigDecimal" do
208
+ params = { :amount => "100.2" }
454
209
 
455
- it "should return 3 as the amount" do
456
- @response[:amount].should == BigDecimal.new("1234.56")
457
- end
210
+ result = Braspag::Bill.normalize_params(params)
211
+ result.should be_kind_of Hash
458
212
 
459
- it "should return the bill number" do
460
- @response[:number].should_not be_empty
461
- end
213
+ result[:amount].should be_kind_of BigDecimal
214
+ end
215
+ end
462
216
 
463
- it "should return the expiration date" do
464
- ten_days_after_today = (Time.now + 3600 * 24 * 10)
217
+ describe ".check_params" do
218
+ let(:params) do
219
+ {
220
+ :order_id => "111",
221
+ :amount => 100.0,
222
+ :payment_method => :bradesco
223
+ }
224
+ end
465
225
 
466
- @response[:expiration_date].should be_kind_of Date
467
- @response[:expiration_date].strftime("%d/%m/%Y").should == ten_days_after_today.strftime("%d/%m/%Y")
226
+ [:order_id, :amount, :payment_method].each do |param|
227
+ it "should raise an error when #{param} is not present" do
228
+ expect {
229
+ params[param] = nil
230
+ Braspag::Bill.check_params(params)
231
+ }.to raise_error Braspag::IncompleteParams
468
232
  end
469
233
  end
470
- end
471
234
 
472
- describe "#status" do
473
- it "should raise an error when no Bill_id is given" do
235
+ it "should raise an error when order_id is not valid" do
236
+ Braspag::Bill.should_receive(:valid_order_id?)
237
+ .with(params[:order_id])
238
+ .and_return(false)
239
+
474
240
  expect {
475
- Braspag::Bill.info(nil)
476
- }.to raise_error(Braspag::InvalidOrderId)
241
+ Braspag::Bill.check_params(params)
242
+ }.to raise_error Braspag::InvalidOrderId
477
243
  end
478
244
 
479
- it "should raise an error when Bill_id is empty" do
245
+ it "should raise an error when customer_name is present and is greater than 255 chars" do
480
246
  expect {
481
- Braspag::Bill.info("")
482
- }.to raise_error(Braspag::InvalidOrderId)
247
+ params[:customer_name] = "b" * 256
248
+ Braspag::Bill.check_params(params)
249
+ }.to raise_error Braspag::InvalidCustomerName
483
250
  end
484
251
 
485
- it "should raise an error when Bill_id is more than 50 characters" do
252
+ it "should raise an error when customer_id is present and is greater than 18 chars" do
486
253
  expect {
487
- Braspag::Bill.info("1" * 51)
488
- }.to raise_error(Braspag::InvalidOrderId)
254
+ params[:customer_id] = "1" * 19
255
+ Braspag::Bill.check_params(params)
256
+ }.to raise_error Braspag::InvalidCustomerId
489
257
  end
490
258
 
491
- it "should raise an error for incorrect data" do
492
- xml = <<-EOXML
493
- <?xml version="1.0" encoding="utf-8"?>
494
- <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
495
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
496
- xsi:nil="true"
497
- xmlns="http://www.pagador.com.br/" />
498
- EOXML
259
+ it "should raise an error when customer_id is present and is less than 11 chars" do
260
+ expect {
261
+ params[:customer_id] = "1" * 10
262
+ Braspag::Bill.check_params(params)
263
+ }.to raise_error Braspag::InvalidCustomerId
264
+ end
499
265
 
500
- FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosBoleto",
501
- :body => xml)
266
+ it "should raise an error when number is present and is greater than 255 chars" do
267
+ expect {
268
+ params[:number] = "1" * 256
269
+ Braspag::Bill.check_params(params)
270
+ }.to raise_error Braspag::InvalidNumber
271
+ end
502
272
 
273
+ it "should raise an error when instructions is present and is greater than 512 chars" do
503
274
  expect {
504
- Braspag::Bill.info("sadpoakjspodqdouq09wduwq")
505
- }.to raise_error(Braspag::UnknownError)
275
+ params[:instructions] = "A" * 513
276
+ Braspag::Bill.check_params(params)
277
+ }.to raise_error Braspag::InvalidInstructions
278
+ end
506
279
 
280
+ it "should raise an error when expiration_date is present and is not in a valid format" do
281
+ expect {
282
+ params[:expiration_date] = "2011/19/19"
283
+ Braspag::Bill.check_params(params)
284
+ }.to raise_error Braspag::InvalidExpirationDate
507
285
 
508
286
  expect {
509
- Braspag::Bill.info("asdnasdniousa")
510
- }.to raise_error(Braspag::UnknownError)
287
+ params[:expiration_date] = "29/10/1991"
288
+ Braspag::Bill.check_params(params)
289
+ }.to_not raise_error Braspag::InvalidExpirationDate
290
+ end
511
291
 
512
- FakeWeb.clean_registry
292
+ it "should raise an error when payment_method is not invalid" do
293
+ expect {
294
+ params[:payment_method] = "non ecziste"
295
+ Braspag::Bill.check_params(params)
296
+ }.to raise_error Braspag::InvalidPaymentMethod
513
297
  end
298
+ end
514
299
 
515
- context "with correct data" do
300
+ describe ".valid_order_id?" do
301
+ it "should return false when order id is greater than 50 chars" do
302
+ Braspag::Bill.valid_order_id?("A"*51).should be_false
303
+ end
516
304
 
517
- let(:status) {
518
- xml = <<-EOXML
519
- <?xml version="1.0" encoding="utf-8"?>
520
- <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
521
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
522
- xmlns="http://www.pagador.com.br/">
523
- <NumeroDocumento>999</NumeroDocumento>
524
- <Sacado/>
525
- <NossoNumero>999</NossoNumero>
526
- <LinhaDigitavel>35690.00361 03962.070003 00000.009993 4 50160000001000</LinhaDigitavel>
527
- <DataDocumento>22/6/2011</DataDocumento>
528
- <DataVencimento>2/7/2011</DataVencimento>
529
- <Cedente>Gonow Tecnologia e Acessoria Empresarial Ltda</Cedente>
530
- <Banco>356-5</Banco>
531
- <Agencia>0003</Agencia>
532
- <Conta>6039620</Conta>
533
- <Carteira>57</Carteira>
534
- <ValorDocumento>10,00</ValorDocumento>
535
- </DadosBoleto>
536
- EOXML
305
+ it "should return false when order id is not a String or Fixnum" do
306
+ Braspag::Bill.valid_order_id?(nil).should be_false
307
+ end
537
308
 
538
- FakeWeb.register_uri(:post, "#{braspag_query_url}/GetDadosBoleto",
539
- :body => xml)
540
- status = Braspag::Bill.info("12345")
541
- FakeWeb.clean_registry
542
- status
543
- }
309
+ it "should return true" do
310
+ Braspag::Bill.valid_order_id?("A"*50).should be_true
311
+ Braspag::Bill.valid_order_id?(100).should be_true
312
+ end
313
+ end
544
314
 
545
- it "should return a Hash" do
546
- status.should be_kind_of Hash
547
- end
315
+ describe ".info" do
316
+ let(:info_url) { "http://braspag/bla" }
317
+ let(:invalid_xml) do
318
+ <<-EOXML
319
+ <?xml version="1.0" encoding="utf-8"?>
320
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
321
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
322
+ xsi:nil="true"
323
+ xmlns="http://www.pagador.com.br/" />
324
+ EOXML
325
+ end
548
326
 
549
- {
327
+ let(:valid_xml) do
328
+ <<-EOXML
329
+ <?xml version="1.0" encoding="utf-8"?>
330
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
331
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
332
+ xmlns="http://www.pagador.com.br/">
333
+ <NumeroDocumento>999</NumeroDocumento>
334
+ <Sacado/>
335
+ <NossoNumero>999</NossoNumero>
336
+ <LinhaDigitavel>35690.00361 03962.070003 00000.009993 4 50160000001000</LinhaDigitavel>
337
+ <DataDocumento>22/6/2011</DataDocumento>
338
+ <DataVencimento>2/7/2011</DataVencimento>
339
+ <Cedente>Gonow Tecnologia e Acessoria Empresarial Ltda</Cedente>
340
+ <Banco>356-5</Banco>
341
+ <Agencia>0003</Agencia>
342
+ <Conta>6039620</Conta>
343
+ <Carteira>57</Carteira>
344
+ <ValorDocumento>10,00</ValorDocumento>
345
+ </DadosBoleto>
346
+ EOXML
347
+ end
348
+
349
+ it "should raise an error when order id is not valid" do
350
+ Braspag::Bill.should_receive(:valid_order_id?)
351
+ .with("bla")
352
+ .and_return(false)
353
+
354
+ expect {
355
+ Braspag::Bill.info "bla"
356
+ }.to raise_error(Braspag::InvalidOrderId)
357
+ end
358
+
359
+ it "should raise an error when Braspag returned an invalid xml as response" do
360
+ FakeWeb.register_uri(:post, info_url, :body => invalid_xml)
361
+
362
+ Braspag::Bill.should_receive(:info_url)
363
+ .and_return(info_url)
364
+
365
+ expect {
366
+ Braspag::Bill.info("orderid")
367
+ }.to raise_error(Braspag::UnknownError)
368
+ end
369
+
370
+ it "should return a Hash when Braspag returned a valid xml as response" do
371
+ FakeWeb.register_uri(:post, info_url, :body => valid_xml)
372
+
373
+ Braspag::Bill.should_receive(:info_url)
374
+ .and_return(info_url)
375
+
376
+ response = Braspag::Bill.info("orderid")
377
+ response.should be_kind_of Hash
378
+
379
+ response.should == {
550
380
  :document_number => "999",
551
381
  :payer => nil,
552
382
  :our_number => "999",
@@ -559,19 +389,39 @@ describe Braspag::Bill do
559
389
  :account => "6039620",
560
390
  :wallet => "57",
561
391
  :amount => "10,00",
562
- }.each do |key, value|
392
+ :amount_invoice => nil,
393
+ :invoice_date => nil
394
+ }
395
+ end
396
+ end
563
397
 
564
- it "should return a Hash with :#{key.to_s} key" do
565
- status[key].should == value
566
- end
567
- end
398
+ describe ".creation_url" do
399
+ it "should return the correct bill creation url when connection environment is homologation" do
400
+ @connection.stub(:braspag_url => braspag_homologation_url)
401
+ Braspag::Bill.creation_url.should == "#{braspag_homologation_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
402
+ end
403
+
404
+ it "should return the correct bill creation url when connection environment is production" do
405
+ @connection.stub(:braspag_url => braspag_production_url)
406
+ Braspag::Bill.creation_url.should == "#{braspag_production_url}/webservices/pagador/Boleto.asmx/CreateBoleto"
568
407
  end
569
408
  end
570
409
 
571
- describe ".payment_method_from_id" do
572
- it 'Credit card amex' do
573
- Braspag::Bill::payment_method_from_id("06").should == :bradesco
574
- Braspag::Bill::payment_method_from_id("06").should be_kind_of Symbol
575
- end
410
+ describe ".info_url" do
411
+ it "should return the correct info url when connection environment is homologation" do
412
+ @connection.stub(:braspag_url => braspag_homologation_url)
413
+ @connection.should_receive(:production?)
414
+ .and_return(false)
415
+
416
+ Braspag::Bill.info_url.should == "#{braspag_homologation_url}/pagador/webservice/pedido.asmx/GetDadosBoleto"
417
+ end
418
+
419
+ it "should return the correct info url when connection environment is production" do
420
+ @connection.stub(:braspag_url => braspag_production_url)
421
+ @connection.should_receive(:production?)
422
+ .and_return(true)
423
+
424
+ Braspag::Bill.info_url.should == "#{braspag_production_url}/webservices/pagador/pedido.asmx/GetDadosBoleto"
425
+ end
576
426
  end
577
427
  end