rbraspag 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbraspag (0.0.2)
4
+ rbraspag (0.0.3)
5
5
  cs-httpi (>= 0.9.5.2)
6
6
  json
7
7
  nokogiri
data/lib/rbraspag/eft.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  module Braspag
2
2
  class Eft
3
+ PAYMENT_METHOD = {
4
+ :bradesco => "11",
5
+ }
6
+
3
7
  MAPPING = {
4
8
  :merchant_id => "Id_Loja",
5
9
  :order_id => "VendaId",
@@ -18,6 +22,11 @@ module Braspag
18
22
  @params = params
19
23
  @params[:merchant_id] = connection.merchant_id
20
24
  @crypto_strategy = crypto_strategy
25
+
26
+ if @params[:amount] && !@params[:amount].is_a?(BigDecimal)
27
+ @params[:amount] = BigDecimal.new(@params[:amount].to_s)
28
+ end
29
+
21
30
  ok?
22
31
  end
23
32
 
@@ -48,49 +57,57 @@ module Braspag
48
57
  raise InvalidHasInterest unless (@params[:has_interest].is_a?(TrueClass) || @params[:has_interest].is_a?(FalseClass))
49
58
  end
50
59
 
51
-
52
60
  true
53
61
  end
54
-
55
- def generate
56
- data = {}
57
- @params.each {|name, value|
58
- if MAPPING[name].nil?
59
- data[name] = value
60
- else
61
- data[MAPPING[name]] = value
62
- end
63
- }
64
-
65
- html = "<form id=\"form_tef_#{@params[:order_id]}\" name=\"form_tef_#{@params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
66
-
67
- if @crypto_strategy.nil?
68
- data.each do |key, value|
69
- html.concat "<input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
70
- end
71
- else
72
- data.delete("Id_Loja")
73
- html.concat "<input type=\"text\" name=\"crypt\" value=\"#{@crypto_strategy.encrypt(data)}\" />\n"
74
- html.concat "<input type=\"text\" name=\"Id_Loja\" value=\"#{@params[:merchant_id]}\" />\n"
75
- end
76
-
77
-
78
- html.concat <<-EOHTML
79
- </form>
80
- <script type="text/javascript" charset="utf-8">
81
- document.forms["form_tef_#{@params[:order_id]}"].submit();
82
- </script>
83
- EOHTML
84
-
85
- html
86
- end
87
-
88
- protected
89
-
90
- def uri
91
- "#{@connection.base_url}/pagador/passthru.asp"
92
- end
93
-
94
62
 
63
+ def generate
64
+ data = create_data_from_params
65
+
66
+ html = "<form id=\"form_tef_#{@params[:order_id]}\" name=\"form_tef_#{@params[:order_id]}\" action=\"#{self.uri}\" method=\"post\">\n"
67
+
68
+ if @crypto_strategy.nil?
69
+ data.each do |key, value|
70
+ html.concat " <input type=\"text\" name=\"#{key}\" value=\"#{value}\" />\n"
71
+ end
72
+ else
73
+ data.delete("Id_Loja")
74
+ html.concat " <input type=\"text\" name=\"crypt\" value=\"#{@crypto_strategy.encrypt(data)}\" />\n"
75
+ html.concat " <input type=\"text\" name=\"Id_Loja\" value=\"#{@params[:merchant_id]}\" />\n"
76
+ end
77
+
78
+ html.concat <<-EOHTML
79
+ </form>
80
+ <script type="text/javascript" charset="utf-8">
81
+ document.forms["form_tef_#{@params[:order_id]}"].submit();
82
+ </script>
83
+ EOHTML
84
+
85
+ html
86
+ end
87
+
88
+ protected
89
+ def create_data_from_params
90
+ MAPPING.inject({}) do |memo, k|
91
+ if k[0] == :payment_method
92
+ memo[k[1]] = PAYMENT_METHOD[@params[:payment_method]]
93
+ elsif k[0] == :amount
94
+ memo[k[1]] = convert_decimal_to_string(@params[:amount])
95
+ else
96
+ memo[k[1]] = @params[k[0]] || "";
97
+ end
98
+
99
+ memo
100
+ end
101
+ end
102
+
103
+ def convert_decimal_to_string(value)
104
+ cents = "0#{((value - value.to_i) * 100).to_i}".slice(-2,2)
105
+ integer = (value - (value - value.to_i)).to_i
106
+ "#{integer},#{cents}"
107
+ end
108
+
109
+ def uri
110
+ "#{@connection.base_url}/pagador/passthru.asp"
111
+ end
95
112
  end
96
113
  end
@@ -1,3 +1,3 @@
1
1
  module Braspag
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/spec/bill_spec.rb CHANGED
@@ -17,7 +17,7 @@ describe Braspag::Bill do
17
17
  it "should raise an error when :order_id is not present" do
18
18
  expect {
19
19
  Braspag::Bill.new(connection, {
20
- :amount => "10000",
20
+ :amount => "100.00",
21
21
  :payment_method => :bradesco
22
22
  })
23
23
  }.to raise_error(Braspag::IncompleteParams)
data/spec/eft_spec.rb CHANGED
@@ -18,8 +18,8 @@ describe Braspag::Eft do
18
18
  it "should raise an error when :order_id is not present" do
19
19
  expect {
20
20
  Braspag::Eft.new(connection, {
21
- :amount => "10000",
22
- :payment_method => "11"
21
+ :amount => "100.00",
22
+ :payment_method => :bradesco
23
23
  })
24
24
  }.to raise_error(Braspag::IncompleteParams)
25
25
  end
@@ -28,7 +28,7 @@ describe Braspag::Eft do
28
28
  expect {
29
29
  Braspag::Eft.new(connection, {
30
30
  :order_id => "12",
31
- :payment_method => "11"
31
+ :payment_method => :bradesco
32
32
  })
33
33
  }.to raise_error(Braspag::IncompleteParams)
34
34
  end
@@ -37,7 +37,7 @@ describe Braspag::Eft do
37
37
  expect {
38
38
  Braspag::Eft.new(connection, {
39
39
  :order_id => "13",
40
- :amount => "12000"
40
+ :amount => "120.00"
41
41
  })
42
42
  }.to raise_error(Braspag::IncompleteParams)
43
43
  end
@@ -46,8 +46,8 @@ describe Braspag::Eft do
46
46
  expect {
47
47
  Braspag::Eft.new(connection, {
48
48
  :order_id => "",
49
- :amount => "12300",
50
- :payment_method => "11"
49
+ :amount => "123.00",
50
+ :payment_method => :bradesco
51
51
  })
52
52
  }.to raise_error(Braspag::InvalidOrderId)
53
53
  end
@@ -56,8 +56,8 @@ describe Braspag::Eft do
56
56
  expect {
57
57
  Braspag::Eft.new(connection, {
58
58
  :order_id => "1" * 51,
59
- :amount => "1200",
60
- :payment_method => "11"
59
+ :amount => "12.00",
60
+ :payment_method => :bradesco
61
61
  })
62
62
  }.to raise_error(Braspag::InvalidOrderId)
63
63
  end
@@ -66,8 +66,8 @@ describe Braspag::Eft do
66
66
  expect {
67
67
  Braspag::Eft.new(connection, {
68
68
  :order_id => "102",
69
- :amount => "4200",
70
- :payment_method => "11",
69
+ :amount => "42.00",
70
+ :payment_method => :bradesco,
71
71
  :customer_name => ""
72
72
  })
73
73
  }.to raise_error(Braspag::InvalidCustomerName)
@@ -77,8 +77,8 @@ describe Braspag::Eft do
77
77
  expect {
78
78
  Braspag::Eft.new(connection, {
79
79
  :order_id => "112",
80
- :amount => "12100",
81
- :payment_method => "11",
80
+ :amount => "121.00",
81
+ :payment_method => :bradesco,
82
82
  :customer_name => "A" * 256
83
83
  })
84
84
  }.to raise_error(Braspag::InvalidCustomerName)
@@ -88,8 +88,8 @@ describe Braspag::Eft do
88
88
  expect {
89
89
  Braspag::Eft.new(connection, {
90
90
  :order_id => "23",
91
- :amount => "25100",
92
- :payment_method => "11",
91
+ :amount => "251.00",
92
+ :payment_method => :bradesco,
93
93
  :customer_id => "2" * 10
94
94
  })
95
95
  }.to raise_error(Braspag::InvalidCustomerId)
@@ -99,8 +99,8 @@ describe Braspag::Eft do
99
99
  expect {
100
100
  Braspag::Eft.new(connection, {
101
101
  :order_id => "90",
102
- :amount => "9000",
103
- :payment_method => "11",
102
+ :amount => "90.00",
103
+ :payment_method => :bradesco,
104
104
  :customer_id => "3" * 19
105
105
  })
106
106
  }.to raise_error(Braspag::InvalidCustomerId)
@@ -110,8 +110,8 @@ describe Braspag::Eft do
110
110
  expect {
111
111
  Braspag::Eft.new(connection, {
112
112
  :order_id => "900",
113
- :amount => "9200",
114
- :payment_method => "11",
113
+ :amount => "92.00",
114
+ :payment_method => :bradesco,
115
115
  :installments => ""
116
116
  })
117
117
  }.to raise_error(Braspag::InvalidInstallments)
@@ -121,8 +121,8 @@ describe Braspag::Eft do
121
121
  expect {
122
122
  Braspag::Eft.new(connection, {
123
123
  :order_id => "91",
124
- :amount => "8000",
125
- :payment_method => "11",
124
+ :amount => "80.00",
125
+ :payment_method => :bradesco,
126
126
  :installments => "5" * 3
127
127
  })
128
128
  }.to raise_error(Braspag::InvalidInstallments)
@@ -132,8 +132,8 @@ describe Braspag::Eft do
132
132
  expect {
133
133
  Braspag::Eft.new(connection, {
134
134
  :order_id => "91",
135
- :amount => "8000",
136
- :payment_method => "11",
135
+ :amount => "80.00",
136
+ :payment_method => :bradesco,
137
137
  :installments => "A" * 2
138
138
  })
139
139
  }.to raise_error(Braspag::InvalidInstallments)
@@ -143,8 +143,8 @@ describe Braspag::Eft do
143
143
  expect {
144
144
  Braspag::Eft.new(connection, {
145
145
  :order_id => "76",
146
- :amount => "5000",
147
- :payment_method => "11",
146
+ :amount => "50.00",
147
+ :payment_method => :bradesco,
148
148
  :has_interest => []
149
149
  })
150
150
  }.to raise_error(Braspag::InvalidHasInterest)
@@ -160,20 +160,24 @@ describe Braspag::Eft do
160
160
  it "should return form fields in strategy without crypto" do
161
161
  html = <<-EOHTML
162
162
  <form id="form_tef_1234123125" name="form_tef_1234123125" action="https://homologacao.pagador.com.br/pagador/passthru.asp" method="post">
163
- <input type="text" name="VendaId" value="1234123125" />
164
- <input type="text" name="Valor" value="12300" />
165
- <input type="text" name="codpagamento" value="11" />
166
- <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
167
- </form>
168
- <script type="text/javascript" charset="utf-8">
169
- document.forms["form_tef_1234123125"].submit();
170
- </script>
163
+ <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
164
+ <input type="text" name="VendaId" value="1234123125" />
165
+ <input type="text" name="Nome" value="" />
166
+ <input type="text" name="Cpf" value="" />
167
+ <input type="text" name="Valor" value="123,00" />
168
+ <input type="text" name="codpagamento" value="11" />
169
+ <input type="text" name="PARCELAS" value="" />
170
+ <input type="text" name="TIPOPARCELADO" value="" />
171
+ </form>
172
+ <script type="text/javascript" charset="utf-8">
173
+ document.forms["form_tef_1234123125"].submit();
174
+ </script>
171
175
  EOHTML
172
176
 
173
177
  Braspag::Eft.new(connection , {
174
178
  :order_id => "1234123125",
175
- :amount => "12300",
176
- :payment_method => "11"
179
+ :amount => "123.00",
180
+ :payment_method => :bradesco
177
181
  }).generate.should == html
178
182
  end
179
183
 
@@ -187,18 +191,18 @@ describe Braspag::Eft do
187
191
 
188
192
  html = <<-EOHTML
189
193
  <form id="form_tef_1234123125" name="form_tef_1234123125" action="https://homologacao.pagador.com.br/pagador/passthru.asp" method="post">
190
- <input type="text" name="crypt" value="5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7" />
191
- <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
192
- </form>
193
- <script type="text/javascript" charset="utf-8">
194
- document.forms["form_tef_1234123125"].submit();
195
- </script>
194
+ <input type="text" name="crypt" value="5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7" />
195
+ <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
196
+ </form>
197
+ <script type="text/javascript" charset="utf-8">
198
+ document.forms["form_tef_1234123125"].submit();
199
+ </script>
196
200
  EOHTML
197
201
 
198
202
  Braspag::Eft.new(connection , {
199
203
  :order_id => "1234123125",
200
- :amount => "12300",
201
- :payment_method => "11"
204
+ :amount => "123.00",
205
+ :payment_method => :bradesco
202
206
  } , braspag_crypto_jar_webservice ).generate.should == html
203
207
 
204
208
  FakeWeb.clean_registry
@@ -214,18 +218,18 @@ describe Braspag::Eft do
214
218
 
215
219
  html = <<-EOHTML
216
220
  <form id="form_tef_1234123125" name="form_tef_1234123125" action="https://homologacao.pagador.com.br/pagador/passthru.asp" method="post">
217
- <input type="text" name="crypt" value="#{key}" />
218
- <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
219
- </form>
220
- <script type="text/javascript" charset="utf-8">
221
- document.forms["form_tef_1234123125"].submit();
222
- </script>
221
+ <input type="text" name="crypt" value="#{key}" />
222
+ <input type="text" name="Id_Loja" value="{84BE7E7F-698A-6C74-F820-AE359C2A07C2}" />
223
+ </form>
224
+ <script type="text/javascript" charset="utf-8">
225
+ document.forms["form_tef_1234123125"].submit();
226
+ </script>
223
227
  EOHTML
224
228
 
225
229
  Braspag::Eft.new(connection , {
226
230
  :order_id => "1234123125",
227
- :amount => "12300",
228
- :payment_method => "11"
231
+ :amount => "123.00",
232
+ :payment_method => :bradesco
229
233
  } , braspag_crypto_webservice ).generate.should == html
230
234
 
231
235
  FakeWeb.clean_registry
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbraspag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,7 @@ date: 2011-07-06 00:00:00.000000000Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: cs-httpi
20
- requirement: &18748720 !ruby/object:Gem::Requirement
20
+ requirement: &15019380 !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ! '>='
@@ -25,10 +25,10 @@ dependencies:
25
25
  version: 0.9.5.2
26
26
  type: :runtime
27
27
  prerelease: false
28
- version_requirements: *18748720
28
+ version_requirements: *15019380
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: json
31
- requirement: &18748300 !ruby/object:Gem::Requirement
31
+ requirement: &15018960 !ruby/object:Gem::Requirement
32
32
  none: false
33
33
  requirements:
34
34
  - - ! '>='
@@ -36,10 +36,10 @@ dependencies:
36
36
  version: '0'
37
37
  type: :runtime
38
38
  prerelease: false
39
- version_requirements: *18748300
39
+ version_requirements: *15018960
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: nokogiri
42
- requirement: &18747840 !ruby/object:Gem::Requirement
42
+ requirement: &15018500 !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
45
  - - ! '>='
@@ -47,10 +47,10 @@ dependencies:
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
- version_requirements: *18747840
50
+ version_requirements: *15018500
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: rspec
53
- requirement: &18747420 !ruby/object:Gem::Requirement
53
+ requirement: &15018080 !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
56
  - - ! '>='
@@ -58,10 +58,10 @@ dependencies:
58
58
  version: '0'
59
59
  type: :development
60
60
  prerelease: false
61
- version_requirements: *18747420
61
+ version_requirements: *15018080
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: fakeweb
64
- requirement: &18747000 !ruby/object:Gem::Requirement
64
+ requirement: &15017660 !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
67
67
  - - ! '>='
@@ -69,10 +69,10 @@ dependencies:
69
69
  version: '0'
70
70
  type: :development
71
71
  prerelease: false
72
- version_requirements: *18747000
72
+ version_requirements: *15017660
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: shoulda-matchers
75
- requirement: &18746580 !ruby/object:Gem::Requirement
75
+ requirement: &15017240 !ruby/object:Gem::Requirement
76
76
  none: false
77
77
  requirements:
78
78
  - - ! '>='
@@ -80,10 +80,10 @@ dependencies:
80
80
  version: '0'
81
81
  type: :development
82
82
  prerelease: false
83
- version_requirements: *18746580
83
+ version_requirements: *15017240
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: guard-rspec
86
- requirement: &18773640 !ruby/object:Gem::Requirement
86
+ requirement: &15048380 !ruby/object:Gem::Requirement
87
87
  none: false
88
88
  requirements:
89
89
  - - ! '>='
@@ -91,10 +91,10 @@ dependencies:
91
91
  version: '0'
92
92
  type: :development
93
93
  prerelease: false
94
- version_requirements: *18773640
94
+ version_requirements: *15048380
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: guard-bundler
97
- requirement: &18773220 !ruby/object:Gem::Requirement
97
+ requirement: &15047960 !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements:
100
100
  - - ! '>='
@@ -102,10 +102,10 @@ dependencies:
102
102
  version: '0'
103
103
  type: :development
104
104
  prerelease: false
105
- version_requirements: *18773220
105
+ version_requirements: *15047960
106
106
  - !ruby/object:Gem::Dependency
107
107
  name: ruby-debug19
108
- requirement: &18772800 !ruby/object:Gem::Requirement
108
+ requirement: &15047540 !ruby/object:Gem::Requirement
109
109
  none: false
110
110
  requirements:
111
111
  - - ! '>='
@@ -113,7 +113,7 @@ dependencies:
113
113
  version: '0'
114
114
  type: :development
115
115
  prerelease: false
116
- version_requirements: *18772800
116
+ version_requirements: *15047540
117
117
  description: rbraspag gem to use Braspag gateway
118
118
  email:
119
119
  - tinorj@gmail.com