baby-braspag 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe Braspag::Order 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
+
9
+ before do
10
+ @connection = mock(:merchant_id => merchant_id)
11
+ Braspag::Connection.stub(:instance => @connection)
12
+ end
13
+
14
+ describe ".status" do
15
+ let(:order_id) { "um order id qualquer" }
16
+ let(:status_url) { "http://foo.com/bar/baz/assererre" }
17
+
18
+ context "with invalid order id" do
19
+ let(:invalid_xml) do
20
+ <<-EOXML
21
+ <?xml version="1.0" encoding="utf-8"?>
22
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
24
+ xsi:nil="true"
25
+ xmlns="http://www.pagador.com.br/" />
26
+ EOXML
27
+ end
28
+
29
+ it "should raise an error when order id is not valid" do
30
+ Braspag::PaymentMethod.should_receive(:valid_order_id?)
31
+ .with(order_id)
32
+ .and_return(false)
33
+
34
+ expect {
35
+ Braspag::Order.status(order_id)
36
+ }.to raise_error(Braspag::InvalidOrderId)
37
+ end
38
+
39
+ it "should raise an error when Braspag returns an invalid xml" do
40
+ FakeWeb.register_uri(:post, status_url, :body => invalid_xml)
41
+
42
+ Braspag::Order.should_receive(:status_url)
43
+ .and_return(status_url)
44
+
45
+ expect {
46
+ Braspag::Order.status(order_id)
47
+ }.to raise_error(Braspag::Order::InvalidData)
48
+ end
49
+ end
50
+
51
+ context "with valid order id" do
52
+ let(:valid_xml) do
53
+ <<-EOXML
54
+ <?xml version="1.0" encoding="utf-8"?>
55
+ <DadosPedido xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
56
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
57
+ xmlns="http://www.pagador.com.br/">
58
+ <CodigoAutorizacao>885796</CodigoAutorizacao>
59
+ <CodigoPagamento>18</CodigoPagamento>
60
+ <FormaPagamento>American Express 2P</FormaPagamento>
61
+ <NumeroParcelas>1</NumeroParcelas>
62
+ <Status>3</Status>
63
+ <Valor>0.01</Valor>
64
+ <DataPagamento>7/8/2011 1:19:38 PM</DataPagamento>
65
+ <DataPedido>7/8/2011 1:06:06 PM</DataPedido>
66
+ <TransId>398591</TransId>
67
+ <BraspagTid>5a1d4463-1d11-4571-a877-763aba0ef7ff</BraspagTid>
68
+ </DadosPedido>
69
+ EOXML
70
+ end
71
+
72
+ before do
73
+ Braspag::Order.should_receive(:status_url)
74
+ .and_return(status_url)
75
+
76
+ FakeWeb.register_uri(:post, status_url, :body => valid_xml)
77
+ @response = Braspag::Order.status(order_id)
78
+ end
79
+
80
+ it "should return a Hash" do
81
+ @response.should be_kind_of Hash
82
+ @response.should == {
83
+ :authorization => "885796",
84
+ :error_code => nil,
85
+ :error_message => nil,
86
+ :payment_method => "18",
87
+ :payment_method_name => "American Express 2P",
88
+ :installments => "1",
89
+ :status => "3",
90
+ :amount => "0.01",
91
+ :cancelled_at => nil,
92
+ :paid_at => "7/8/2011 1:19:38 PM",
93
+ :order_date => "7/8/2011 1:06:06 PM",
94
+ :transaction_id => "398591",
95
+ :tid => "5a1d4463-1d11-4571-a877-763aba0ef7ff"
96
+ }
97
+ end
98
+ end
99
+ end
100
+
101
+ describe ".status_url" do
102
+ it "should return the correct info url when connection environment is homologation" do
103
+ @connection.stub(:braspag_url => braspag_homologation_url)
104
+ @connection.should_receive(:production?)
105
+ .and_return(false)
106
+
107
+ Braspag::Order.status_url.should == "#{braspag_homologation_url}/pagador/webservice/pedido.asmx/GetDadosPedido"
108
+ end
109
+
110
+ it "should return the correct info url when connection environment is production" do
111
+ @connection.stub(:braspag_url => braspag_production_url)
112
+ @connection.should_receive(:production?)
113
+ .and_return(true)
114
+
115
+ Braspag::Order.status_url.should == "#{braspag_production_url}/webservices/pagador/pedido.asmx/GetDadosPedido"
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe Braspag::Poster do
5
+ let!(:request) { ::HTTPI::Request.new('http://foo/bar') }
6
+ let(:response) { mock(:body => 'success') }
7
+ let(:logger) { mock(:info => nil) }
8
+ subject { described_class.new('http://foo/bar') }
9
+ before { Braspag.logger = logger }
10
+
11
+ describe "#do_post" do
12
+ before do
13
+ ::HTTPI::Request.should_receive(:new).with('http://foo/bar').and_return(request)
14
+ ::HTTPI.should_receive(:post).with(request).and_return(response)
15
+ end
16
+
17
+ it "should log the request info" do
18
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {"foo"=>"bar", "egg"=>"span"}')
19
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
20
+ end
21
+
22
+ it "should log the request info removing the credit card sensitive info" do
23
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {"cardNumber"=>"************", "securityCode"=>"***"}')
24
+ subject.do_post(:doe, { 'cardNumber' => '123', 'securityCode' => '456' })
25
+ end
26
+
27
+ it "should log response info" do
28
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {"foo"=>"bar", "egg"=>"span"}')
29
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
30
+ end
31
+
32
+ it "should not raise an error if logger is not defined" do
33
+ Braspag.logger = nil
34
+ expect {
35
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
36
+ }.to_not raise_error
37
+ end
38
+
39
+ it "should not set the proxy if the proxy_address is not set" do
40
+ request.should_not_receive(:proxy=)
41
+ subject.do_post(:foo, {})
42
+ end
43
+
44
+ context "using a proxy" do
45
+ before { Braspag.proxy_address = 'http://proxy.com' }
46
+
47
+ it "should set the proxy if the proxy_address is set" do
48
+ request.should_receive(:proxy=).with('http://proxy.com')
49
+ subject.do_post(:foo, {})
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,296 @@
1
+ require 'spec_helper'
2
+
3
+ describe Braspag::ProtectedCreditCard do
4
+ let(:braspag_homologation_protected_card_url) { "https://cartaoprotegido.braspag.com.br" }
5
+ let(:braspag_production_protected_card_url) { "https://www.cartaoprotegido.com.br" }
6
+ let(:merchant_id) { "um id qualquer" }
7
+
8
+ before do
9
+ @connection = mock(:merchant_id => merchant_id, :protected_card_url => 'https://www.cartaoprotegido.com.br/Services/TestEnvironment', :homologation? => false)
10
+ Braspag::Connection.stub(:instance => @connection)
11
+ Braspag.proxy_address = nil
12
+ end
13
+ context "savon_client with proxy addres set" do
14
+ let (:savon_double) {double('Savon')}
15
+ before do
16
+ @http = double("HTTP")
17
+ Savon::Client.stub(:new).and_return savon_double
18
+ savon_double.stub(:http).and_return @http
19
+ Braspag.proxy_address = "http://some.proxy.com:3444"
20
+ end
21
+ it "should set the http proxy" do
22
+ @http.should_receive(:proxy=).with "http://some.proxy.com:3444"
23
+
24
+ Braspag::ProtectedCreditCard.savon_client("url")
25
+ end
26
+ end
27
+
28
+ describe ".save" do
29
+ let(:params) do
30
+ {
31
+ :customer_name => "W" * 21,
32
+ :holder => "Joao Maria Souza",
33
+ :card_number => "9" * 10,
34
+ :expiration => "10/12",
35
+ :order_id => "um order id",
36
+ :request_id => "00000000-0000-0000-0000-000000000044"
37
+ }
38
+ end
39
+
40
+ let(:params_with_merchant_id) do
41
+ params.merge!(:merchant_id => merchant_id)
42
+ end
43
+
44
+ let(:save_protected_card_url) { "http://braspag.com/bla" }
45
+
46
+ let(:savon_double) { double('Savon') }
47
+
48
+ before do
49
+ @connection.should_receive(:merchant_id)
50
+ end
51
+
52
+ context "with valid params" do
53
+ let(:valid_hash) do
54
+ {
55
+ :save_credit_card_response => {
56
+ :save_credit_card_result => {
57
+ :just_click_key => 'SAVE-PROTECTED-CARD-TOKEN',
58
+ :success => true
59
+ }
60
+ }
61
+ }
62
+ end
63
+
64
+ let(:response) do
65
+ double('Response', :to_hash => valid_hash)
66
+ end
67
+
68
+ before do
69
+ Braspag::ProtectedCreditCard.should_receive(:save_protected_card_url)
70
+ Braspag::ProtectedCreditCard.should_receive(:check_protected_card_params)
71
+ .and_return(true)
72
+ Savon::Client.should_receive(:new).and_return(savon_double)
73
+ savon_double.should_receive(:request).and_return(response)
74
+
75
+ @response = Braspag::ProtectedCreditCard.save(params)
76
+ end
77
+
78
+ it "should return a Hash" do
79
+ @response.should be_kind_of Hash
80
+ @response.should == {
81
+ :just_click_key => "SAVE-PROTECTED-CARD-TOKEN",
82
+ :success => true
83
+ }
84
+ end
85
+
86
+ end
87
+
88
+ context "with invalid params" do
89
+ let(:invalid_hash) do
90
+ {
91
+ :save_credit_card_response => {
92
+ :save_credit_card_result => {
93
+ :just_click_key => nil,
94
+ :success => false
95
+ }
96
+ }
97
+ }
98
+ end
99
+
100
+ let(:response) do
101
+ double('Response', :to_hash => invalid_hash)
102
+ end
103
+
104
+ before do
105
+ Braspag::ProtectedCreditCard.should_receive(:check_protected_card_params)
106
+ .and_return(true)
107
+ Braspag::ProtectedCreditCard.should_receive(:save_protected_card_url)
108
+ .and_return(save_protected_card_url)
109
+ Savon::Client.should_receive(:new).and_return(savon_double)
110
+ savon_double.should_receive(:request).and_return(response)
111
+
112
+ @response = Braspag::ProtectedCreditCard.save(params)
113
+ end
114
+
115
+ it "should return a Hash" do
116
+ @response.should be_kind_of Hash
117
+ @response.should == {
118
+ :just_click_key => nil,
119
+ :success => false
120
+ }
121
+ end
122
+ end
123
+ end
124
+
125
+ describe ".get" do
126
+ let(:get_protected_card_url) { "http://braspag/bla" }
127
+
128
+ let(:invalid_xml) do
129
+ <<-EOXML
130
+ <CartaoProtegidoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
131
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
132
+ xmlns="http://www.pagador.com.br/">
133
+ <CardHolder>Joao Maria Souza</CardHolder>
134
+ <CardNumber></CardNumber>
135
+ <CardExpiration>10/12</CardExpiration>
136
+ <MaskedCardNumber>******9999</MaskedCardNumber>
137
+ </CartaoProtegidoReturn>
138
+ EOXML
139
+ end
140
+
141
+ let(:valid_xml) do
142
+ <<-EOXML
143
+ <CartaoProtegidoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
144
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
145
+ xmlns="http://www.pagador.com.br/">
146
+ <CardHolder>Joao Maria Souza</CardHolder>
147
+ <CardNumber>9999999999</CardNumber>
148
+ <CardExpiration>10/12</CardExpiration>
149
+ <MaskedCardNumber>******9999</MaskedCardNumber>
150
+ </CartaoProtegidoReturn>
151
+ EOXML
152
+ end
153
+
154
+ before do
155
+ Braspag.logger.stub(:info)
156
+ end
157
+ it "should raise an error when just click key is not valid" do
158
+ Braspag::ProtectedCreditCard.should_receive(:valid_just_click_key?)
159
+ .with("bla")
160
+ .and_return(false)
161
+
162
+ expect {
163
+ Braspag::ProtectedCreditCard.get "bla"
164
+ }.to raise_error(Braspag::InvalidJustClickKey)
165
+ end
166
+
167
+ it "should raise an error when Braspag returned an invalid xml as response" do
168
+ FakeWeb.register_uri(:post, get_protected_card_url, :body => invalid_xml)
169
+
170
+ Braspag::ProtectedCreditCard.should_receive(:get_protected_card_url)
171
+ .and_return(get_protected_card_url)
172
+
173
+ expect {
174
+ Braspag::ProtectedCreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
175
+ }.to raise_error(Braspag::UnknownError)
176
+ end
177
+
178
+ it "should return a Hash when Braspag returned a valid xml as response" do
179
+ FakeWeb.register_uri(:post, get_protected_card_url, :body => valid_xml)
180
+
181
+ Braspag::ProtectedCreditCard.should_receive(:get_protected_card_url)
182
+ .and_return(get_protected_card_url)
183
+
184
+ response = Braspag::ProtectedCreditCard.get("b0b0b0b0-bbbb-4d4d-bd27-f1f1f1ededed")
185
+ response.should be_kind_of Hash
186
+
187
+ response.should == {
188
+ :holder => "Joao Maria Souza",
189
+ :expiration => "10/12",
190
+ :card_number => "9" * 10,
191
+ :masked_card_number => "*" * 6 + "9" * 4
192
+ }
193
+ end
194
+
195
+ end
196
+
197
+ describe ".just_click_shop" do
198
+ context "body" do
199
+ let(:params) { {
200
+ :request_id => "123",
201
+ :customer_name => "Joao Silva",
202
+ :order_id => "999",
203
+ :amount => 10.50,
204
+ :payment_method => :redecard,
205
+ :number_installments => 3,
206
+ :payment_type => "test",
207
+ :just_click_key => "key",
208
+ :security_code => "123"
209
+ } }
210
+
211
+ class SavonClientTest
212
+ attr_accessor :response
213
+ attr_reader :method
214
+
215
+ def request(web, method, &block)
216
+ @method = method
217
+ instance_eval &block
218
+
219
+ @response
220
+ end
221
+
222
+ def soap
223
+ @soap ||= OpenStruct.new
224
+ end
225
+ end
226
+
227
+ before :each do
228
+ @savon_client_test = SavonClientTest.new
229
+ @savon_client_test.response = {:just_click_shop_response => {}}
230
+ Savon::Client.stub(:new).with('https://www.cartaoprotegido.com.br/Services/TestEnvironment/CartaoProtegido.asmx?wsdl').and_return(@savon_client_test)
231
+ end
232
+
233
+ after :each do
234
+ Savon::Client.unstub(:new)
235
+ end
236
+
237
+ it "should have RequestId" do
238
+ described_class.just_click_shop(params)
239
+ @savon_client_test.soap.body['justClickShopRequestWS']['RequestId'].should eq '123'
240
+ end
241
+
242
+ it "should have MerchantKey" do
243
+ described_class.just_click_shop(params)
244
+ @savon_client_test.soap.body['justClickShopRequestWS']['MerchantKey'].should eq 'um id qualquer'
245
+ end
246
+
247
+ it "should have CustomerName" do
248
+ described_class.just_click_shop(params)
249
+ @savon_client_test.soap.body['justClickShopRequestWS']['CustomerName'].should eq 'Joao Silva'
250
+ end
251
+
252
+ it "should have OrderId" do
253
+ described_class.just_click_shop(params)
254
+ @savon_client_test.soap.body['justClickShopRequestWS']['OrderId'].should eq '999'
255
+ end
256
+
257
+ it "should have Amount" do
258
+ described_class.just_click_shop(params)
259
+ @savon_client_test.soap.body['justClickShopRequestWS']['Amount'].should eq "1050"
260
+ end
261
+
262
+ it "should have PaymentMethod" do
263
+ described_class.just_click_shop(params)
264
+ @savon_client_test.soap.body['justClickShopRequestWS']['PaymentMethod'].should eq 20
265
+ end
266
+
267
+ it "should have PaymentType" do
268
+ described_class.just_click_shop(params)
269
+ @savon_client_test.soap.body['justClickShopRequestWS']['PaymentType'].should eq 'test'
270
+ end
271
+
272
+ it "should have NumberInstallments" do
273
+ described_class.just_click_shop(params)
274
+ @savon_client_test.soap.body['justClickShopRequestWS']['NumberInstallments'].should eq 3
275
+ end
276
+
277
+ it "should have JustClickKey" do
278
+ described_class.just_click_shop(params)
279
+ @savon_client_test.soap.body['justClickShopRequestWS']['JustClickKey'].should eq 'key'
280
+ end
281
+
282
+ it "should have SecurityCode" do
283
+ described_class.just_click_shop(params)
284
+ @savon_client_test.soap.body['justClickShopRequestWS']['SecurityCode'].should eq '123'
285
+ end
286
+ end
287
+
288
+ it ".save_protected_card_url .get_protected_card_url" do
289
+ @connection.stub(:protected_card_url => braspag_homologation_protected_card_url)
290
+
291
+ Braspag::ProtectedCreditCard.save_protected_card_url.should == "#{braspag_homologation_protected_card_url}/CartaoProtegido.asmx?wsdl"
292
+ Braspag::ProtectedCreditCard.get_protected_card_url.should == "#{braspag_homologation_protected_card_url}/CartaoProtegido.asmx/GetCreditCard"
293
+ end
294
+
295
+ end
296
+ end