tray-checkout 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tray::Checkout::AccountResponseParser do
5
+ describe "#parse" do
6
+ context "with success response from get_info" do
7
+ let(:xml) { body_for :get_account_info }
8
+ let(:parser) { Tray::Checkout::AccountResponseParser.new(xml) }
9
+ let(:response) { parser.parse }
10
+
11
+ context "returns people" do
12
+ {
13
+ contact_primary: "08000059230",
14
+ name: "Vendedor Loja Modelo",
15
+ trade_name: "Loja Modelo",
16
+ email: "lojamodelo@tray.com.br",
17
+ url_logo: nil,
18
+ css_url: "http://default.com/default.css"
19
+ }.each do |param, value|
20
+ it param do
21
+ response.people[param].should == value
22
+ end
23
+ end
24
+ end
25
+
26
+ context "returns service_contact" do
27
+ {
28
+ service_phone: "(14)3412-1377",
29
+ email_service: "lojamodelo@tray.com.br",
30
+ service_phone_status: "true",
31
+ email_service_status: "true"
32
+ }.each do |param, value|
33
+ it param do
34
+ response.service_contact[param].should == value
35
+ end
36
+ end
37
+ end
38
+
39
+ context "returns seconds_redirect" do
40
+ it 'returns the seconds_redirect parameter' do
41
+ response.seconds_redirect.should be_nil
42
+ end
43
+ end
44
+ end
45
+
46
+ context "with error response" do
47
+ let(:xml) { body_for :get_failure_not_found }
48
+ let(:parser) { Tray::Checkout::ResponseParser.new(xml) }
49
+ let(:response) { parser.parse }
50
+
51
+ [:people, :service_contact, :seconds_redirect].each do |info|
52
+ it "returns nil #{info}" do
53
+ response.send(info).should be_nil
54
+ end
55
+ end
56
+ end
57
+
58
+ context "with validation error response" do
59
+ let(:xml) { body_for :create_failure_validation_errors }
60
+ let(:parser) { Tray::Checkout::ResponseParser.new(xml) }
61
+ let(:response) { parser.parse }
62
+
63
+ [:people, :service_contact, :seconds_redirect].each do |info|
64
+ it "returns nil #{info}" do
65
+ response.send(info).should be_nil
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tray::Checkout::Account do
5
+ let(:token_account) { '8bfe5ddcb77207b' }
6
+
7
+ before :all do
8
+ Tray::Checkout.environment = :sandbox
9
+ end
10
+
11
+ after :all do
12
+ Tray::Checkout.environment = :production
13
+ end
14
+
15
+ describe "#get_by_token" do
16
+ let(:account) { Tray::Checkout::Account.new }
17
+
18
+ context 'when token account is valid/exists' do
19
+ before do
20
+ VCR.use_cassette 'model/valid_token' do
21
+ @response = account.get_by_token(token_account)
22
+ end
23
+ end
24
+
25
+ it 'response returns true' do
26
+ @response.success?.should be_true
27
+ end
28
+
29
+ it 'response has name attribute' do
30
+ @response.people[:name].should == 'Vendedor Loja Modelo '
31
+ end
32
+
33
+ it 'response has service_contact attribute' do
34
+ @response.service_contact.should_not be_blank
35
+ end
36
+ end
37
+
38
+ context "when token account is not valid/doesn't exists" do
39
+ before do
40
+ VCR.use_cassette 'model/invalid_token' do
41
+ @response = account.get_by_token("9q8w7e6r5t")
42
+ end
43
+ end
44
+
45
+ it 'response returns false' do
46
+ @response.success?.should be_false
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,190 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tray::Checkout::ParamsParser do
5
+ context 'transaction' do
6
+ let :params do
7
+ {
8
+ token_account: "949u5uu9ef36f7u",
9
+ customer: {
10
+ name: "Pedro Bonamides",
11
+ cpf: "18565842673",
12
+ email: "pedro@bo.com.br",
13
+ sex: :male,
14
+ marital_status: :single,
15
+ contacts: [
16
+ { type: :home, number: "1142360873" },
17
+ { type: :mobile, number: "11987654321" },
18
+ { type: :work, number: "1134567890" }
19
+ ],
20
+ addresses: [
21
+ { type: :billing,
22
+ street: "Avenida Pedro Alvares Cabral",
23
+ number: "123",
24
+ neighborhood: "Parque Ibirapuera",
25
+ postal_code: "04094050",
26
+ city: "São Paulo",
27
+ state: "SP"
28
+ },
29
+ { type: :delivery,
30
+ street: "Avenida Pedro Alvares Cabral",
31
+ number: "123",
32
+ neighborhood: "Parque Ibirapuera",
33
+ postal_code: "04094050",
34
+ city: "São Paulo",
35
+ state: "SP"
36
+ }
37
+ ]
38
+ },
39
+ transaction: {
40
+ order_number: "R1245",
41
+ shipping_type: "Sedex",
42
+ shipping_price: 13.94,
43
+ url_notification: "http://prodis.blog.br/tray_notification",
44
+ products: [
45
+ { code: "LOGO-8278",
46
+ quantity: 2,
47
+ price_unit: 100.99,
48
+ description: "Logo Prodis"
49
+ },
50
+ { code: "877",
51
+ quantity: 1,
52
+ price_unit: 10.00,
53
+ description: "Outro produto"
54
+ }
55
+ ]
56
+ },
57
+ payment: {
58
+ method: :mastercard,
59
+ split: 3,
60
+ card: {
61
+ name: "ZEFINHA NOCEGA",
62
+ number: "5105105105105100",
63
+ expdate_month: "09",
64
+ expdate_year: "2015",
65
+ cvv: "123"
66
+ }
67
+ }
68
+ }
69
+ end
70
+
71
+ let(:parser) { Tray::Checkout::ParamsParser.new(params) }
72
+ let(:response_params) { parser.parse }
73
+
74
+ describe "#parse" do
75
+ it "sets customer gender expect API value" do
76
+ response_params[:customer][:gender].should == "M"
77
+ end
78
+
79
+ it "sets customer relationship expect API value" do
80
+ response_params[:customer][:relationship].should == "S"
81
+ end
82
+
83
+ it "sets customer contact type expect API value" do
84
+ response_params[:customer][:contacts][0][:type_contact].should == "H"
85
+ response_params[:customer][:contacts][1][:type_contact].should == "M"
86
+ response_params[:customer][:contacts][2][:type_contact].should == "W"
87
+ end
88
+
89
+ it "sets customer contact number expect API value" do
90
+ response_params[:customer][:contacts][0][:number_contact].should == "1142360873"
91
+ response_params[:customer][:contacts][1][:number_contact].should == "11987654321"
92
+ response_params[:customer][:contacts][2][:number_contact].should == "1134567890"
93
+ end
94
+
95
+ it "sets customer address type expect API value" do
96
+ response_params[:customer][:addresses][0][:type_address].should == "B"
97
+ response_params[:customer][:addresses][1][:type_address].should == "D"
98
+ end
99
+
100
+ it "sets payment method ID expect API value" do
101
+ response_params[:payment][:payment_method_id].should == 4
102
+ end
103
+
104
+ { card_name: "ZEFINHA NOCEGA",
105
+ card_number: "5105105105105100",
106
+ card_expdate_month: "09",
107
+ card_expdate_year: "2015",
108
+ card_cvv: "123"
109
+ }.each do |param, value|
110
+ it "sets payment #{param}" do
111
+ response_params[:payment][param].should == value
112
+ end
113
+ end
114
+
115
+ it "sets products as expect API data structure" do
116
+ response_params[:transaction_product][0][:code].should == "LOGO-8278"
117
+ response_params[:transaction_product][0][:quantity].should == 2
118
+ response_params[:transaction_product][0][:price_unit].should == 100.99
119
+ response_params[:transaction_product][0][:description].should == "Logo Prodis"
120
+
121
+ response_params[:transaction_product][1][:code].should == "877"
122
+ response_params[:transaction_product][1][:quantity].should == 1
123
+ response_params[:transaction_product][1][:price_unit].should == 10.00
124
+ response_params[:transaction_product][1][:description].should == "Outro produto"
125
+ end
126
+
127
+ it "keeps token account supplied in params" do
128
+ response_params[:token_account].should == params[:token_account]
129
+ end
130
+
131
+ context "when sets token account in configuration" do
132
+ around do |example|
133
+ Tray::Checkout.configure { |config| config.token_account = "1q2w3e4r5t6y7u8" }
134
+ example.run
135
+ Tray::Checkout.configure { |config| config.token_account = nil }
136
+ end
137
+
138
+ context "and token account is supplied in params" do
139
+ it "keeps token account supplied in params" do
140
+ response_params[:token_account].should == params[:token_account]
141
+ end
142
+ end
143
+
144
+ context "and token account is not supplied in params" do
145
+ it "uses token account from configuration" do
146
+ params.delete(:token_account)
147
+ response_params = Tray::Checkout::ParamsParser.new(params).parse
148
+ response_params[:token_account].should == "1q2w3e4r5t6y7u8"
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ context 'account' do
156
+ let :params do
157
+ {
158
+ contact_primary: "08000059230",
159
+ name: "Vendedor Loja Modelo",
160
+ trade_name: "Loja Modelo",
161
+ email: "lojamodelo@tray.com.br",
162
+ url_logo: nil,
163
+ css_url: "http://default.com/default.css",
164
+ service_contact: {
165
+ service_phone: "(14)3412-1377",
166
+ email_service: "lojamodelo@tray.com.br",
167
+ service_phone_status: "true",
168
+ email_service_status: "true"
169
+ },
170
+ seconds_redirect: nil
171
+ }
172
+ end
173
+
174
+ let(:parser) { Tray::Checkout::ParamsParser.new(params) }
175
+ let(:account_params) { parser.parse }
176
+
177
+ describe "#parse" do
178
+ it "sets service_contact expect API value" do
179
+ account_params[:service_contact][:service_phone].should == "(14)3412-1377"
180
+ account_params[:service_contact][:email_service].should == "lojamodelo@tray.com.br"
181
+ account_params[:service_contact][:service_phone_status].should == "true"
182
+ account_params[:service_contact][:email_service_status].should == "true"
183
+ end
184
+
185
+ it "sets seconds_redirect expect API value" do
186
+ account_params[:seconds_redirect].should be_nil
187
+ end
188
+ end
189
+ end
190
+ end
@@ -23,30 +23,30 @@ describe Tray::Checkout::Parser do
23
23
  end
24
24
  end
25
25
 
26
- describe "#transaction_params" do
26
+ describe "#response_params" do
27
27
  before :each do
28
28
  @params = {
29
29
  token_account: "949u5uu9ef36f7u",
30
30
  customer: { name: "Pedro Bonamides", sex: :male },
31
31
  payment: { payment_method: :boleto_bancario }
32
32
  }
33
- @transaction_params = {
33
+ @response_params = {
34
34
  token_account: "949u5uu9ef36f7u",
35
35
  customer: { name: "Pedro Bonamides", sex: :male, gender: "M" },
36
36
  payment: { payment_method: :boleto, payment_method_id: "6" }
37
37
  }
38
- @transaction_params_parser = Tray::Checkout::TransactionParamsParser.new(@params)
39
- Tray::Checkout::TransactionParamsParser.stub(:new).and_return(@transaction_params_parser)
38
+ @params_parser = Tray::Checkout::ParamsParser.new(@params)
39
+ Tray::Checkout::ParamsParser.stub(:new).and_return(@params_parser)
40
40
  end
41
41
 
42
42
  it "creates transaction params parser" do
43
- Tray::Checkout::TransactionParamsParser.should_receive(:new).with(@params)
44
- parser.transaction_params(@params)
43
+ Tray::Checkout::ParamsParser.should_receive(:new).with(@params)
44
+ parser.response_params(@params)
45
45
  end
46
46
 
47
47
  it "parses params" do
48
- @transaction_params_parser.should_receive(:parse).and_return(@transaction_params)
49
- parser.transaction_params(@params).should == @transaction_params
48
+ @params_parser.should_receive(:parse).and_return(@response_params)
49
+ parser.response_params(@params).should == @response_params
50
50
  end
51
51
  end
52
52
  end
@@ -3,53 +3,6 @@ require 'spec_helper'
3
3
 
4
4
  describe Tray::Checkout::ResponseParser do
5
5
  describe "#parse" do
6
- context "with success response from temp_transaction" do
7
- let(:xml) { body_for :create_temp_transaction_with_token }
8
- let(:parser) { Tray::Checkout::ResponseParser.new(xml) }
9
- let(:response) { parser.parse }
10
-
11
- it "returns success" do
12
- response.success?.should be_true
13
- end
14
-
15
- context "returns transaction" do
16
- {
17
- token: "a906bf32cb59060dfc90769524f99d5a",
18
- url_car: "\n\t\t\thttp://checkout.sandbox.tray.com.br/payment/car/v1/\n\t\t"
19
- }.each do |param, value|
20
- it param do
21
- response.transaction[param].should == value
22
- end
23
- end
24
- end
25
-
26
- context "returns products" do
27
- {
28
- code: "teste",
29
- img: "\n\t\t\t\t\thttp://catnross.com/wp-content/uploads/2011/08/product1.jpg\n\t\t\t\t",
30
- sku_code: nil,
31
- description: "produto teste",
32
- extra: nil,
33
- id: 4502,
34
- type_product: nil
35
- }.each do |param, value|
36
- it param do
37
- response.transaction[:products].first[param].should == value
38
- end
39
- end
40
-
41
- {
42
- price_unit: "10.0",
43
- quantity: "5.0",
44
- weight: "300.0"
45
- }.each do |param, value|
46
- it param do
47
- response.transaction[:products].first[param].to_s.should == value
48
- end
49
- end
50
- end
51
- end
52
-
53
6
  context "with success response" do
54
7
  let(:xml) { body_for :get_success_boleto }
55
8
  let(:parser) { Tray::Checkout::ResponseParser.new(xml) }
@@ -58,102 +11,6 @@ describe Tray::Checkout::ResponseParser do
58
11
  it "returns success" do
59
12
  response.success?.should be_true
60
13
  end
61
-
62
- it "returns empty errors" do
63
- response.errors.should be_empty
64
- end
65
-
66
- context "returns transaction" do
67
- {
68
- order_number: "1234567",
69
- free: "Texto Interno",
70
- status_name: "Em Recupera\\xC3\\xA7\\xC3\\xA3o",
71
- status: :recovering,
72
- id: 3856,
73
- token: "4761d2e198ba6b60b45900a4d95482d5"
74
- }.each do |param, value|
75
- it param do
76
- response.transaction[param].should == value
77
- end
78
- end
79
- end
80
-
81
- context "returns payment" do
82
- {
83
- tid: nil,
84
- split: 1,
85
- linha_digitavel: "34191.76007 00536.260144 50565.600009 6 58630000219999",
86
- method: :boleto,
87
- method_name: "Boleto Bancario"
88
- }.each do |param, value|
89
- it param do
90
- response.payment[param].should == value
91
- end
92
- end
93
-
94
- {
95
- price_original: '2199.99',
96
- price: '2199.99'
97
- }.each do |param, value|
98
- it param do
99
- response.payment[param].to_s.should == value
100
- end
101
- end
102
- end
103
-
104
- context "returns customer" do
105
- {
106
- name: "Nome do Cliente",
107
- cpf: "98489882380",
108
- email: "emaildo@cliente.com.br",
109
- company_name: nil,
110
- trade_name: nil,
111
- cnpj: nil
112
- }.each do |param, value|
113
- it param do
114
- response.customer[param].should == value
115
- end
116
- end
117
-
118
- context "address with" do
119
- {
120
- street: "Av Paulista",
121
- number: "1001",
122
- neighborhood: "Centro",
123
- postal_code: "04001001",
124
- completion: nil,
125
- city: "S\\xC3\\xA3o Paulo",
126
- state: "SP"
127
- }.each do |param, value|
128
- it param do
129
- response.customer[:addresses].first[param].should == value
130
- end
131
- end
132
- end
133
-
134
- context "contacts with" do
135
- [ {
136
- number: "11998761234",
137
- id: nil,
138
- type: :mobile
139
- }
140
- ].each_with_index do |contacts, i|
141
- contacts.each do |param, value|
142
- it param do
143
- response.customer[:contacts][i][param].should == value
144
- end
145
- end
146
- end
147
- end
148
-
149
- context "without contacts" do
150
- it "returns empty array" do
151
- parser = Tray::Checkout::ResponseParser.new(body_for(:get_success_boleto_without_contacts))
152
- response = parser.parse
153
- response.customer[:contacts].should == []
154
- end
155
- end
156
- end
157
14
  end
158
15
 
159
16
  context "with error response" do
@@ -165,12 +22,6 @@ describe Tray::Checkout::ResponseParser do
165
22
  response.success?.should be_false
166
23
  end
167
24
 
168
- [:transaction, :payment, :customer].each do |info|
169
- it "returns nil #{info}" do
170
- response.send(info).should be_nil
171
- end
172
- end
173
-
174
25
  context "returns error" do
175
26
  { code: "001001",
176
27
  message: "Token inválido ou não encontrado"
@@ -191,12 +42,6 @@ describe Tray::Checkout::ResponseParser do
191
42
  response.success?.should be_false
192
43
  end
193
44
 
194
- [:transaction, :payment, :customer].each do |info|
195
- it "returns nil #{info}" do
196
- response.send(info).should be_nil
197
- end
198
- end
199
-
200
45
  context "returns error" do
201
46
  { code: "13",
202
47
  message: "Tipo de Contato não está incluído na lista"
@@ -208,4 +53,22 @@ describe Tray::Checkout::ResponseParser do
208
53
  end
209
54
  end
210
55
  end
56
+
57
+ describe("transaction?") do
58
+ context "response is from a transaction" do
59
+ let(:xml) { body_for :get_success_boleto }
60
+
61
+ it "returns true" do
62
+ Tray::Checkout::ResponseParser.transaction?(xml).should be_true
63
+ end
64
+ end
65
+
66
+ context "response is not from a transaction" do
67
+ let(:xml) { body_for :get_account_info }
68
+
69
+ it "returns false" do
70
+ Tray::Checkout::ResponseParser.transaction?(xml).should be_false
71
+ end
72
+ end
73
+ end
211
74
  end