tray-checkout 0.2.1 → 0.3.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.
Files changed (33) hide show
  1. data/README.rdoc +123 -141
  2. data/lib/tray/checkout/base_transaction.rb +25 -0
  3. data/lib/tray/checkout/config.rb +10 -2
  4. data/lib/tray/checkout/response_parser.rb +22 -14
  5. data/lib/tray/checkout/temp_transaction.rb +22 -0
  6. data/lib/tray/checkout/transaction.rb +8 -18
  7. data/lib/tray/checkout/transaction_params_parser.rb +1 -0
  8. data/lib/tray/checkout/version.rb +1 -1
  9. data/lib/tray-checkout.rb +2 -0
  10. data/spec/spec_helper.rb +1 -2
  11. data/spec/support/mock_request.rb +2 -1
  12. data/spec/support/responses/create_failure_validation_errors.xml +10 -48
  13. data/spec/support/responses/create_success_boleto.xml +35 -75
  14. data/spec/support/responses/create_temp_transaction_with_token.xml +28 -0
  15. data/spec/support/responses/get_failure_not_found.xml +4 -4
  16. data/spec/support/responses/get_success_boleto.xml +36 -76
  17. data/spec/support/responses/get_success_boleto_without_contacts.xml +37 -67
  18. data/spec/support/responses/get_success_mastercard.xml +36 -76
  19. data/spec/support/vcr/model/create_cart.yml +59 -0
  20. data/spec/support/vcr/model/create_failure_validation_errors.yml +75 -0
  21. data/spec/support/vcr/model/create_success_boleto.yml +93 -0
  22. data/spec/support/vcr/model/create_success_mastercard.yml +90 -0
  23. data/spec/support/vcr/model/get_failure_not_found.yml +72 -0
  24. data/spec/support/vcr/model/get_success_boleto.yml +114 -0
  25. data/spec/support/vcr/model/update_cart.yml +59 -0
  26. data/spec/support/vcr_setup.rb +7 -0
  27. data/spec/tray/checkout/parser_spec.rb +1 -1
  28. data/spec/tray/checkout/response_parser_spec.rb +83 -67
  29. data/spec/tray/checkout/temp_transaction_spec.rb +94 -0
  30. data/spec/tray/checkout/transaction_spec.rb +146 -23
  31. data/spec/tray/checkout_spec.rb +2 -2
  32. data/tray-checkout.gemspec +1 -0
  33. metadata +77 -14
@@ -3,6 +3,53 @@ 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
+
6
53
  context "with success response" do
7
54
  let(:xml) { body_for :get_success_boleto }
8
55
  let(:parser) { Tray::Checkout::ResponseParser.new(xml) }
@@ -17,70 +64,51 @@ describe Tray::Checkout::ResponseParser do
17
64
  end
18
65
 
19
66
  context "returns transaction" do
20
- { token: "db9b3265af6e7e19af8dd70e00d77383x",
21
- id: 530,
22
- status: :approved,
23
- status_name: "Aprovada",
24
- order_number: "F2456",
25
- price_original: 33.21,
26
- price_payment: 33.21,
27
- price_seller: 30.69,
28
- price_additional: 0.00,
29
- price_discount: 0.00,
30
- shipping_type: "Sedex",
31
- shipping_price: 1.23,
32
- split: 1,
33
- url_notification: "http://prodis.blog.br/tray_notification",
34
- transaction_token: nil,
35
- transaction_id: nil,
36
- status_id: nil
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"
37
74
  }.each do |param, value|
38
75
  it param do
39
76
  response.transaction[param].should == value
40
77
  end
41
78
  end
42
-
43
- it "date_transaction" do
44
- date_transaction = response.transaction[:date_transaction]
45
- date_transaction.should be_a(Time)
46
- date_transaction.to_s.should == "2012-12-13 02:49:42 UTC"
47
- end
48
79
  end
49
80
 
50
81
  context "returns payment" do
51
- { method: :boleto,
52
- method_name: "Boleto Bancario",
53
- price: 33.21,
82
+ {
83
+ tid: nil,
54
84
  split: 1,
55
- number_proccess: 750,
56
- response: "Texto de resposta fake.",
57
- url: "http://checkout.sandbox.tray.com.br/payment/billet/d2baa84c13f23addde401c8e1426396e",
58
- linha_digitavel: "34191.76007 00075.091140 53021.450001 1 55510000003321",
59
- payment_method_id: nil,
60
- payment_method_name: nil,
61
- price_payment: nil,
62
- url_payment: nil,
63
- payment_response: nil
85
+ linha_digitavel: "34191.76007 00536.260144 50565.600009 6 58630000219999",
86
+ method: :boleto,
87
+ method_name: "Boleto Bancario"
64
88
  }.each do |param, value|
65
89
  it param do
66
90
  response.payment[param].should == value
67
91
  end
68
92
  end
69
93
 
70
- { date_approval: "2012-12-15 15:24:38 UTC",
71
- date_payment: "2012-12-15 15:23:05 UTC"
94
+ {
95
+ price_original: '2199.99',
96
+ price: '2199.99'
72
97
  }.each do |param, value|
73
98
  it param do
74
- response.payment[param].should be_a(Time)
75
99
  response.payment[param].to_s.should == value
76
100
  end
77
101
  end
78
102
  end
79
103
 
80
104
  context "returns customer" do
81
- { name: "Pedro Bonamides",
82
- email: "pedro@bo.com.br",
83
- cpf: "18565842673"
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
84
112
  }.each do |param, value|
85
113
  it param do
86
114
  response.customer[param].should == value
@@ -88,12 +116,13 @@ describe Tray::Checkout::ResponseParser do
88
116
  end
89
117
 
90
118
  context "address with" do
91
- { street: "Avenida Pedro Alvares Cabral",
92
- number: "123",
93
- neighborhood: "Parque Ibirapuera",
94
- postal_code: "04094050",
119
+ {
120
+ street: "Av Paulista",
121
+ number: "1001",
122
+ neighborhood: "Centro",
123
+ postal_code: "04001001",
95
124
  completion: nil,
96
- city: "São Paulo",
125
+ city: "S\\xC3\\xA3o Paulo",
97
126
  state: "SP"
98
127
  }.each do |param, value|
99
128
  it param do
@@ -103,21 +132,10 @@ describe Tray::Checkout::ResponseParser do
103
132
  end
104
133
 
105
134
  context "contacts with" do
106
- [ { id: 345,
107
- number: "1137654321",
108
- type: :home,
109
- primary: true,
110
- contact_id: nil,
111
- value: nil,
112
- type_contact: nil
113
- },
114
- { id: 348,
115
- number: "11987654321",
116
- type: :mobile,
117
- primary: false,
118
- contact_id: nil,
119
- value: nil,
120
- type_contact: nil
135
+ [ {
136
+ number: "11998761234",
137
+ id: nil,
138
+ type: :mobile
121
139
  }
122
140
  ].each_with_index do |contacts, i|
123
141
  contacts.each do |param, value|
@@ -154,8 +172,8 @@ describe Tray::Checkout::ResponseParser do
154
172
  end
155
173
 
156
174
  context "returns error" do
157
- { code: "003042",
158
- message: "Transação não encontrada"
175
+ { code: "001001",
176
+ message: "Token inválido ou não encontrado"
159
177
  }.each do |param, value|
160
178
  it param do
161
179
  response.errors.first[param].should == value
@@ -180,10 +198,8 @@ describe Tray::Checkout::ResponseParser do
180
198
  end
181
199
 
182
200
  context "returns error" do
183
- { code: "1",
184
- message: "não pode ficar em branco",
185
- message_complete: "Tipo não pode ficar em branco",
186
- field: "person_addresses.type_address"
201
+ { code: "13",
202
+ message: "Tipo de Contato não está incluído na lista"
187
203
  }.each do |param, value|
188
204
  it param do
189
205
  response.errors.first[param].should == value
@@ -0,0 +1,94 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tray::Checkout::TempTransaction do
5
+ let(:token) { 'a906bf32cb59060dfc90769524f99d5a' }
6
+ let(:transaction) { Tray::Checkout::TempTransaction.new }
7
+ let(:params) {
8
+ {
9
+ token_account: "8bfe5ddcb77207b",
10
+ postal_code_seller: "18523698",
11
+ transaction: {
12
+ order_number: "1234567",
13
+ free: "Texto Interno",
14
+ url_notification: "http://prodis.blog.br/tray_notification",
15
+ products: [
16
+ {
17
+ code: "teste",
18
+ description: "Notebook Branco",
19
+ quantity: "1",
20
+ price_unit: "2199.99",
21
+ weight: "300",
22
+ url_img: "http://catnross.com/wp-content/uploads/2011/08/product1.jpg"
23
+ }
24
+ ]
25
+ }
26
+ }
27
+ }
28
+
29
+ before :all do
30
+ Tray::Checkout.environment = :sandbox
31
+ end
32
+
33
+ after :all do
34
+ Tray::Checkout.environment = :production
35
+ end
36
+
37
+ describe "#cart_url" do
38
+ context 'when there is a cart created' do
39
+ let(:transaction) { Tray::Checkout::TempTransaction.new(token) }
40
+
41
+ it 'returns the URL' do
42
+ url = transaction.cart_url
43
+
44
+ url.should == "http://checkout.sandbox.tray.com.br/payment/car/v1/#{token}"
45
+ end
46
+ end
47
+
48
+ context "when there is no cart created" do
49
+ it 'returns nil' do
50
+ url = transaction.cart_url
51
+
52
+ url.should == nil
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#add_to_cart' do
58
+ context 'when there is a cart created' do
59
+ let(:transaction) { Tray::Checkout::TempTransaction.new(token) }
60
+
61
+ before do
62
+ params['token_transaction'] = token
63
+
64
+ VCR.use_cassette 'model/update_cart' do
65
+ @response = transaction.add_to_cart(params)
66
+ end
67
+ end
68
+
69
+ it 'updates the same cart' do
70
+ @response.transaction[:token].should == token
71
+ end
72
+
73
+ it "adds quantity to cart's quantity" do
74
+ @response.transaction[:products].first[:quantity].to_i.should > 1
75
+ end
76
+ end
77
+
78
+ context "when there is no cart created" do
79
+ before do
80
+ VCR.use_cassette 'model/create_cart' do
81
+ @response = transaction.add_to_cart(params)
82
+ end
83
+ end
84
+
85
+ it 'creates a cart and stores the token' do
86
+ @response.transaction[:token].should_not == token
87
+ end
88
+
89
+ it "cart's quantity is the same as sent" do
90
+ @response.transaction[:products].first[:quantity].to_i.should == 1
91
+ end
92
+ end
93
+ end
94
+ end
@@ -3,12 +3,103 @@ require 'spec_helper'
3
3
 
4
4
  describe Tray::Checkout::Transaction do
5
5
  let(:transaction) { Tray::Checkout::Transaction.new }
6
+ let(:params) {
7
+ {
8
+ token_account: "8bfe5ddcb77207b",
9
+ customer: {
10
+ name: "Nome do Cliente",
11
+ cpf: "12312312312",
12
+ email: "emaildo@cliente.com.br",
13
+ birth_date: '01/01/1900'.to_date,
14
+ sex: :male,
15
+ marital_status: :single,
16
+ contacts: [
17
+ { type: :mobile,
18
+ number: "11998761234"
19
+ }
20
+ ],
21
+ addresses: [
22
+ { type: :billing,
23
+ street: "Av Paulista",
24
+ number: "1001",
25
+ neighborhood: "Centro",
26
+ postal_code: "04001001",
27
+ completion: "apto 11",
28
+ city: "São Paulo",
29
+ state: "SP"
30
+ }
31
+ ]
32
+ },
33
+ transaction: {
34
+ available_payment_methods: '6',
35
+ order_number: "123456789",
36
+ shipping_type: "Sedex",
37
+ shipping_price: "15.00",
38
+ price_discount: "1.00",
39
+ price_additional: "1.00",
40
+ url_notification: "http://prodis.blog.br/tray_notification",
41
+ free: "Texto Interno",
42
+ sub_store: "2",
43
+ products: [
44
+ {
45
+ description: "Notebook Branco",
46
+ quantity: "1",
47
+ price_unit: "1999.99",
48
+ code: "123",
49
+ sku_code: "72628",
50
+ extra: "Produto novo"
51
+ }
52
+ ]
53
+ },
54
+ payment: {
55
+ method: :mastercard,
56
+ split: 12,
57
+ card_name: "Nome Impresso",
58
+ card_number: "4073020000000002",
59
+ card_expdate_month: "01",
60
+ card_expdate_year: "2017",
61
+ cvv: "123"
62
+ },
63
+ affiliates: [
64
+ {
65
+ email: "affiliates@test.com",
66
+ percentage: "1"
67
+ }
68
+ ]
69
+ }
70
+ }
71
+
72
+ before :all do
73
+ Tray::Checkout.environment = :sandbox
74
+ end
75
+
76
+ after :all do
77
+ Tray::Checkout.environment = :production
78
+ end
6
79
 
7
80
  describe "#get" do
81
+ context "token account is defined on the configuration" do
82
+ before :each do
83
+ Tray::Checkout.configure { |config| config.token_account = "8bfe5ddcb77207b" }
84
+ VCR.use_cassette 'model/get_success_boleto' do
85
+ @response = transaction.get("4761d2e198ba6b60b45900a4d95482d5")
86
+ end
87
+ end
88
+
89
+ it "returns success" do
90
+ @response.success?.should be_true
91
+ end
92
+
93
+ it "returns the transaction with only one parameter" do
94
+ @response.transaction[:token].should == "4761d2e198ba6b60b45900a4d95482d5"
95
+ end
96
+ end
97
+
8
98
  context "when transaction is found" do
9
99
  before :each do
10
- mock_request_for(:get_success_boleto)
11
- @response = transaction.get("db9b3265af6e7e19af8dd70e00d77383x")
100
+ VCR.use_cassette 'model/get_success_boleto' do
101
+ @response = transaction.get("4761d2e198ba6b60b45900a4d95482d5", "8bfe5ddcb77207b")
102
+ end
12
103
  end
13
104
 
14
105
  it "returns success" do
@@ -16,25 +107,25 @@ describe Tray::Checkout::Transaction do
16
107
  end
17
108
 
18
109
  it "returns transaction data" do
19
- @response.transaction[:token].should == "db9b3265af6e7e19af8dd70e00d77383x"
20
- @response.transaction[:id].should == 530
110
+ @response.transaction[:token].should == "4761d2e198ba6b60b45900a4d95482d5"
111
+ @response.transaction[:id].should == 3856
21
112
  end
22
113
 
23
114
  it "returns payment data" do
24
115
  @response.payment[:method_name].should == "Boleto Bancario"
25
- @response.payment[:url].should == "http://checkout.sandbox.tray.com.br/payment/billet/d2baa84c13f23addde401c8e1426396e"
26
116
  end
27
117
 
28
118
  it "returns customer data" do
29
- @response.customer[:name].should == "Pedro Bonamides"
30
- @response.customer[:email].should == "pedro@bo.com.br"
119
+ @response.customer[:name].should == "Nome do Cliente"
120
+ @response.customer[:email].should == "emaildo@cliente.com.br"
31
121
  end
32
122
  end
33
123
 
34
124
  context "when transaction is not found" do
35
125
  before :each do
36
- mock_request_for(:get_failure_not_found)
37
- @response = transaction.get("987asd654lkj321qwe098poi")
126
+ VCR.use_cassette 'model/get_failure_not_found' do
127
+ @response = transaction.get("987asd654lkj321qwe098poi", "8bfe5ddcb77207b")
128
+ end
38
129
  end
39
130
 
40
131
  it "does not return success" do
@@ -49,10 +140,40 @@ describe Tray::Checkout::Transaction do
49
140
  end
50
141
 
51
142
  describe "#create" do
52
- context "successful" do
143
+ context "successful payment slip" do
144
+ before :each do
145
+ VCR.use_cassette 'model/create_success_boleto' do
146
+ new_params = params
147
+ new_params[:payment] = { method: :boleto }
148
+
149
+ @response = transaction.create(new_params)
150
+ end
151
+ end
152
+
153
+ it "returns success" do
154
+ @response.success?.should be_true
155
+ end
156
+
157
+ it "returns transaction data" do
158
+ @response.transaction[:token].should == "756c0bb313b7e86caf59cf8f1ddfb281"
159
+ @response.transaction[:status].should == :recovering
160
+ end
161
+
162
+ it "returns payment data" do
163
+ @response.payment[:method].should == :boleto
164
+ @response.payment[:tid].should == nil
165
+ end
166
+
167
+ it "returns customer data" do
168
+ @response.customer[:name].should == "Nome do Cliente"
169
+ end
170
+ end
171
+
172
+ context "successful credit card" do
53
173
  before :each do
54
- mock_request_for(:create_success_mastercard)
55
- @response = transaction.create(fake: "fake params")
174
+ VCR.use_cassette 'model/create_success_mastercard' do
175
+ @response = transaction.create(params)
176
+ end
56
177
  end
57
178
 
58
179
  it "returns success" do
@@ -60,25 +181,27 @@ describe Tray::Checkout::Transaction do
60
181
  end
61
182
 
62
183
  it "returns transaction data" do
63
- @response.transaction[:token].should == "fc739f786425e34010481dcc2939e4bdx"
64
- @response.transaction[:status].should == :approved
184
+ @response.transaction[:token].should == "756c0bb313b7e86caf59cf8f1ddfb281"
185
+ @response.transaction[:status].should == :recovering
65
186
  end
66
187
 
67
188
  it "returns payment data" do
68
189
  @response.payment[:method].should == :mastercard
69
- @response.payment[:tid].should == "1355409331"
190
+ @response.payment[:tid].should == '1233'
70
191
  end
71
192
 
72
193
  it "returns customer data" do
73
- @response.customer[:name].should == "Pedro Bonamides"
74
- @response.customer[:email].should == "pedro@bo.com.br"
194
+ @response.customer[:name].should == "Nome do Cliente"
75
195
  end
76
196
  end
77
197
 
78
- context "unsuccess" do
198
+ context "unsuccess payment slip" do
79
199
  before :each do
80
- mock_request_for(:create_failure_validation_errors)
81
- @response = transaction.create(fake: "fake params")
200
+ params[:customer][:contacts][0].delete(:type)
201
+
202
+ VCR.use_cassette 'model/create_failure_validation_errors' do
203
+ @response = transaction.create(params)
204
+ end
82
205
  end
83
206
 
84
207
  it "does not return success" do
@@ -86,9 +209,9 @@ describe Tray::Checkout::Transaction do
86
209
  end
87
210
 
88
211
  it "returns error" do
89
- @response.errors.first[:code].should == "1"
90
- @response.errors.first[:message].should == "não pode ficar em branco"
91
- @response.errors.first[:message_complete].should == "Tipo não pode ficar em branco"
212
+ @response.errors.first[:code].should == "13"
213
+ @response.errors.first[:message].should == "não está incluído na lista"
214
+ @response.errors.first[:message_complete].should == "Tipo de Contato não está incluído na lista"
92
215
  end
93
216
  end
94
217
  end
@@ -43,8 +43,8 @@ describe Tray::Checkout do
43
43
  Tray::Checkout.environment.should == :production
44
44
  end
45
45
 
46
- { production: "https://api.checkout.tray.com.br/api/transactions",
47
- sandbox: "http://api.sandbox.checkout.tray.com.br/api/v1/transactions"
46
+ { production: "https://api.traycheckout.com.br/",
47
+ sandbox: "http://api.sandbox.traycheckout.com.br/"
48
48
  }.each do |environment, url|
49
49
  context "when environment is #{environment}" do
50
50
  around do |example|
@@ -26,4 +26,5 @@ Gem::Specification.new do |gem|
26
26
  gem.add_development_dependency "rake"
27
27
  gem.add_development_dependency "rspec", "~> 2.12"
28
28
  gem.add_development_dependency "webmock", "~> 1.9"
29
+ gem.add_development_dependency "vcr", "~> 2.6.0"
29
30
  end