catarse_pagarme 2.13.0 → 2.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/Gemfile.lock +28 -16
- data/app/controllers/catarse_pagarme/credit_cards_controller.rb +2 -3
- data/app/models/catarse_pagarme/antifraud_order_wrapper.rb +148 -0
- data/app/models/catarse_pagarme/balance_transfer_delegator.rb +5 -1
- data/app/models/catarse_pagarme/credit_card_transaction.rb +48 -11
- data/app/models/catarse_pagarme/payment_delegator.rb +7 -3
- data/catarse_pagarme.gemspec +2 -1
- data/lib/catarse_pagarme.rb +2 -1
- data/lib/catarse_pagarme/configuration.rb +2 -1
- data/lib/catarse_pagarme/version.rb +1 -1
- data/spec/dummy/app/models/payment.rb +4 -0
- data/spec/dummy/app/models/payment_engines.rb +4 -0
- data/spec/dummy/config/initializers/pagarme.rb +1 -0
- data/spec/models/catarse_pagarme/antifraud_order_wrapper_spec.rb +366 -0
- data/spec/models/catarse_pagarme/credit_card_transaction_spec.rb +225 -37
- data/spec/models/catarse_pagarme/payment_delegator_spec.rb +33 -15
- data/spec/support/factories.rb +5 -5
- metadata +34 -2
@@ -1,12 +1,13 @@
|
|
1
1
|
module CatarsePagarme
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :api_key, :slip_tax, :credit_card_tax, :interest_rate, :host, :subdomain, :protocol,
|
3
|
+
attr_accessor :api_key, :konduto_api_key, :slip_tax, :credit_card_tax, :interest_rate, :host, :subdomain, :protocol,
|
4
4
|
:max_installments, :minimum_value_for_installment, :credit_card_cents_fee, :pagarme_tax, :stone_tax,
|
5
5
|
:stone_installment_tax, :cielo_tax, :cielo_installment_diners_tax, :cielo_installment_not_diners_tax,
|
6
6
|
:cielo_installment_amex_tax, :cielo_installment_not_amex_tax, :ecr_key, :slip_week_day_interval, :antifraud_tax, :use_simility
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
self.api_key = ''
|
10
|
+
self.konduto_api_key = ''
|
10
11
|
self.ecr_key = ''
|
11
12
|
self.slip_tax = 0
|
12
13
|
self.credit_card_tax = 0
|
@@ -0,0 +1,366 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CatarsePagarme::AntifraudOrderWrapper do
|
4
|
+
let(:attributes) { double }
|
5
|
+
let(:transaction) { double }
|
6
|
+
|
7
|
+
subject { described_class.new(attributes, transaction) }
|
8
|
+
|
9
|
+
describe '#send' do
|
10
|
+
let(:order) { double }
|
11
|
+
let(:client) { double }
|
12
|
+
let(:response) { double }
|
13
|
+
let(:analyze) { true }
|
14
|
+
|
15
|
+
before do
|
16
|
+
allow(subject).to receive(:build_order).with(analyze: analyze).and_return(order)
|
17
|
+
allow(subject).to receive(:client).and_return(client)
|
18
|
+
allow(client).to receive(:analyze).with(order).and_return(response)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'sends to antifraud' do
|
22
|
+
expect(subject.send(analyze: analyze)).to eq response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#client' do
|
27
|
+
context 'when @client is present' do
|
28
|
+
it 'returns client' do
|
29
|
+
client = double
|
30
|
+
subject.instance_variable_set('@client', client)
|
31
|
+
|
32
|
+
expect(subject.__send__(:client)).to eq client
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when @client isn`t present' do
|
37
|
+
let(:client) { double }
|
38
|
+
|
39
|
+
before do
|
40
|
+
allow(CatarsePagarme).to receive_message_chain('configuration.konduto_api_key').and_return('some-key')
|
41
|
+
allow(KondutoRuby).to receive(:new).with('some-key').and_return(client)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'initialize a new client' do
|
45
|
+
subject.instance_variable_set('@client', nil)
|
46
|
+
|
47
|
+
expect(subject.__send__(:client)).to eq client
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#build_order' do
|
53
|
+
let(:order_attributes) { { total_amount: 10 } }
|
54
|
+
let(:customer) { KondutoCustomer.new }
|
55
|
+
let(:payment) { [KondutoPayment.new] }
|
56
|
+
let(:billing) { KondutoAddress.new }
|
57
|
+
let(:shipping) { KondutoAddress.new }
|
58
|
+
let(:shopping_cart) { [KondutoItem.new] }
|
59
|
+
let(:seller) { KondutoSeller.new }
|
60
|
+
|
61
|
+
before do
|
62
|
+
allow(subject).to receive(:order_attributes).and_return(order_attributes)
|
63
|
+
allow(subject).to receive(:build_customer).and_return(customer)
|
64
|
+
allow(subject).to receive(:build_payment).and_return(payment)
|
65
|
+
allow(subject).to receive(:build_billing_address).and_return(billing)
|
66
|
+
allow(subject).to receive(:build_shipping_address).and_return(shipping)
|
67
|
+
allow(subject).to receive(:build_shopping_cart).and_return(shopping_cart)
|
68
|
+
allow(subject).to receive(:build_seller).and_return(seller)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'builds an order' do
|
72
|
+
order = subject.__send__(:build_order, analyze: false)
|
73
|
+
expect(order.total_amount).to eq order_attributes[:total_amount]
|
74
|
+
expect(order.analyze).to eq false
|
75
|
+
expect(order.customer).to eq customer
|
76
|
+
expect(order.payment).to eq payment
|
77
|
+
expect(order.billing).to eq billing
|
78
|
+
expect(order.shipping).to eq shipping
|
79
|
+
expect(order.shopping_cart).to eq shopping_cart
|
80
|
+
expect(order.seller).to eq seller
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#build_customer' do
|
85
|
+
let(:customer_attributes) { { name: 'John Appleseed' } }
|
86
|
+
|
87
|
+
before { allow(subject).to receive(:customer_attributes).and_return(customer_attributes) }
|
88
|
+
|
89
|
+
it 'builds a customer' do
|
90
|
+
customer = subject.__send__(:build_customer)
|
91
|
+
expect(customer.name).to eq customer_attributes[:name]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#build_payment' do
|
96
|
+
let(:payment_attributes) { { bin: '012345' } }
|
97
|
+
|
98
|
+
before { allow(subject).to receive(:payment_attributes).and_return(payment_attributes) }
|
99
|
+
|
100
|
+
it 'builds a payment' do
|
101
|
+
payment = subject.__send__(:build_payment)
|
102
|
+
expect(payment.first.bin).to eq payment_attributes[:bin]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '#build_billing_address' do
|
107
|
+
let(:billing_address_attributes) { { address1: 'R. Tree' } }
|
108
|
+
|
109
|
+
before { allow(subject).to receive(:billing_address_attributes).and_return(billing_address_attributes) }
|
110
|
+
|
111
|
+
it 'builds an address' do
|
112
|
+
address = subject.__send__(:build_billing_address)
|
113
|
+
expect(address.address1).to eq billing_address_attributes[:address1]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '#build_shipping_address' do
|
118
|
+
let(:shipping_address_attributes) { { address1: 'R. Tree' } }
|
119
|
+
|
120
|
+
before { allow(subject).to receive(:shipping_address_attributes).and_return(shipping_address_attributes) }
|
121
|
+
|
122
|
+
it 'builds an address' do
|
123
|
+
address = subject.__send__(:build_shipping_address)
|
124
|
+
expect(address.address1).to eq shipping_address_attributes[:address1]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe '#build_shopping_cart' do
|
129
|
+
let(:item_attributes) { { sku: '0102' } }
|
130
|
+
|
131
|
+
before { allow(subject).to receive(:item_attributes).and_return(item_attributes) }
|
132
|
+
|
133
|
+
it 'builds an item' do
|
134
|
+
item = subject.__send__(:build_shopping_cart)
|
135
|
+
expect(item.first.sku).to eq item_attributes[:sku]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe '#build_seller' do
|
140
|
+
let(:seller_attributes) { { name: 'Catarse' } }
|
141
|
+
|
142
|
+
before { allow(subject).to receive(:seller_attributes).and_return(seller_attributes) }
|
143
|
+
|
144
|
+
it 'builds a seller' do
|
145
|
+
seller = subject.__send__(:build_seller)
|
146
|
+
expect(seller.name).to eq seller_attributes[:name]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe '#customer_attributes' do
|
151
|
+
let(:attributes) do
|
152
|
+
{
|
153
|
+
customer: {
|
154
|
+
document_number: '1234',
|
155
|
+
name: 'John Appleseed',
|
156
|
+
email: 'john@example.com',
|
157
|
+
phone: { ddi: '85', ddd: '85', number: '85858585' }
|
158
|
+
},
|
159
|
+
antifraud_metadata: {
|
160
|
+
register: {
|
161
|
+
registered_at: '2019-01-01 01:01:01'
|
162
|
+
}
|
163
|
+
}
|
164
|
+
}
|
165
|
+
end
|
166
|
+
|
167
|
+
before { subject.attributes = attributes }
|
168
|
+
|
169
|
+
it 'builds customer attributes' do
|
170
|
+
customer_attributes = subject.__send__(:customer_attributes)
|
171
|
+
|
172
|
+
expect(customer_attributes[:id]).to eq '1234'
|
173
|
+
expect(customer_attributes[:name]).to eq 'John Appleseed'
|
174
|
+
expect(customer_attributes[:email]).to eq 'john@example.com'
|
175
|
+
expect(customer_attributes[:phone1]).to eq '858585858585'
|
176
|
+
expect(customer_attributes[:created_at]).to eq '2019-01-01 01:01:01'
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
describe '#payment_attributes' do
|
181
|
+
let(:attributes) do
|
182
|
+
{
|
183
|
+
customer: {
|
184
|
+
document_number: '1234',
|
185
|
+
name: 'John Appleseed',
|
186
|
+
email: 'john@example.com',
|
187
|
+
phone: { ddi: '85', ddd: '85', number: '85858585' }
|
188
|
+
},
|
189
|
+
antifraud_metadata: {
|
190
|
+
register: {
|
191
|
+
registered_at: '2019-01-01 01:01:01'
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}
|
195
|
+
end
|
196
|
+
|
197
|
+
let(:transaction) do
|
198
|
+
double(status: 'authorized', card: double(first_digits: '123456', last_digits: '7890', expiration_date: '1122'))
|
199
|
+
end
|
200
|
+
|
201
|
+
before do
|
202
|
+
subject.attributes = attributes
|
203
|
+
subject.transaction = transaction
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'builds customer attributes' do
|
207
|
+
payment_attributes = subject.__send__(:payment_attributes)
|
208
|
+
|
209
|
+
expect(payment_attributes[:type]).to eq 'credit'
|
210
|
+
expect(payment_attributes[:bin]).to eq '123456'
|
211
|
+
expect(payment_attributes[:last4]).to eq '7890'
|
212
|
+
expect(payment_attributes[:expiration_date]).to eq '112022'
|
213
|
+
end
|
214
|
+
|
215
|
+
context 'when transaction status is authorized' do
|
216
|
+
it 'build with approved status' do
|
217
|
+
payment_attributes = subject.__send__(:payment_attributes)
|
218
|
+
expect(payment_attributes[:status]).to eq 'approved'
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
context 'when transaction status isn`t authorized' do
|
223
|
+
let(:transaction) do
|
224
|
+
double(status: 'refused', card: double(first_digits: '123456', last_digits: '7890', expiration_date: '1122'))
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'build with declined status' do
|
228
|
+
payment_attributes = subject.__send__(:payment_attributes)
|
229
|
+
expect(payment_attributes[:status]).to eq 'declined'
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe '#billing_address_attributes' do
|
235
|
+
let(:attributes) do
|
236
|
+
{
|
237
|
+
customer: {
|
238
|
+
name: 'John Appleseed'
|
239
|
+
},
|
240
|
+
antifraud_metadata: {
|
241
|
+
billing: {
|
242
|
+
address: {
|
243
|
+
street: 'R. A',
|
244
|
+
city: 'New City',
|
245
|
+
state: 'NC',
|
246
|
+
zipcode: '1245'
|
247
|
+
}
|
248
|
+
}
|
249
|
+
}
|
250
|
+
}
|
251
|
+
end
|
252
|
+
|
253
|
+
let(:transaction) { double(card: double(country: 'BRAZIL')) }
|
254
|
+
|
255
|
+
before do
|
256
|
+
subject.attributes = attributes
|
257
|
+
subject.transaction = transaction
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'builds customer attributes' do
|
261
|
+
billing_address_attributes = subject.__send__(:billing_address_attributes)
|
262
|
+
|
263
|
+
expect(billing_address_attributes[:name]).to eq 'John Appleseed'
|
264
|
+
expect(billing_address_attributes[:address1]).to eq 'R. A'
|
265
|
+
expect(billing_address_attributes[:city]).to eq 'New City'
|
266
|
+
expect(billing_address_attributes[:state]).to eq 'NC'
|
267
|
+
expect(billing_address_attributes[:zip]).to eq '1245'
|
268
|
+
expect(billing_address_attributes[:country]).to eq 'BR'
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
describe '#shipping_address_attributes' do
|
273
|
+
let(:attributes) do
|
274
|
+
{
|
275
|
+
antifraud_metadata: {
|
276
|
+
shipping: {
|
277
|
+
customer: {
|
278
|
+
name: 'John Appleseed'
|
279
|
+
},
|
280
|
+
address: {
|
281
|
+
street: 'R. A',
|
282
|
+
city: 'New City',
|
283
|
+
state: 'NC',
|
284
|
+
zipcode: '1245'
|
285
|
+
}
|
286
|
+
}
|
287
|
+
}
|
288
|
+
}
|
289
|
+
end
|
290
|
+
|
291
|
+
let(:transaction) { double(card: double(country: 'BRAZIL')) }
|
292
|
+
|
293
|
+
before do
|
294
|
+
subject.attributes = attributes
|
295
|
+
subject.transaction = transaction
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'builds customer attributes' do
|
299
|
+
shipping_address_attributes = subject.__send__(:shipping_address_attributes)
|
300
|
+
|
301
|
+
expect(shipping_address_attributes[:name]).to eq 'John Appleseed'
|
302
|
+
expect(shipping_address_attributes[:address1]).to eq 'R. A'
|
303
|
+
expect(shipping_address_attributes[:city]).to eq 'New City'
|
304
|
+
expect(shipping_address_attributes[:state]).to eq 'NC'
|
305
|
+
expect(shipping_address_attributes[:zip]).to eq '1245'
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe '#item_attributes' do
|
310
|
+
let(:attributes) do
|
311
|
+
{
|
312
|
+
amount: 1000,
|
313
|
+
metadata: {
|
314
|
+
contribution_id: 'contribution-id',
|
315
|
+
project_online: '2019-01-01 01:01:01'
|
316
|
+
},
|
317
|
+
antifraud_metadata: {
|
318
|
+
shopping_cart: [
|
319
|
+
{ name: 'Book' }
|
320
|
+
]
|
321
|
+
}
|
322
|
+
}
|
323
|
+
end
|
324
|
+
|
325
|
+
before { subject.attributes = attributes }
|
326
|
+
|
327
|
+
it 'builds customer attributes' do
|
328
|
+
item_attributes = subject.__send__(:item_attributes)
|
329
|
+
|
330
|
+
expect(item_attributes[:sku]).to eq 'contribution-id'
|
331
|
+
expect(item_attributes[:product_code]).to eq 'contribution-id'
|
332
|
+
expect(item_attributes[:category]).to eq 9999
|
333
|
+
expect(item_attributes[:name]).to eq 'Book'
|
334
|
+
expect(item_attributes[:unit_cost]).to eq 10.0
|
335
|
+
expect(item_attributes[:quantity]).to eq 1
|
336
|
+
expect(item_attributes[:created_at]).to eq '2019-01-01'
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
|
341
|
+
describe '#seller_attributes' do
|
342
|
+
let(:attributes) do
|
343
|
+
{
|
344
|
+
antifraud_metadata: {
|
345
|
+
events: [
|
346
|
+
{
|
347
|
+
id: 'event-id',
|
348
|
+
venue_name: 'Autor',
|
349
|
+
date: '2019-01-01'
|
350
|
+
}
|
351
|
+
]
|
352
|
+
}
|
353
|
+
}
|
354
|
+
end
|
355
|
+
|
356
|
+
before { subject.attributes = attributes }
|
357
|
+
|
358
|
+
it 'builds customer attributes' do
|
359
|
+
seller_attributes = subject.__send__(:seller_attributes)
|
360
|
+
|
361
|
+
expect(seller_attributes[:id]).to eq 'event-id'
|
362
|
+
expect(seller_attributes[:name]).to eq 'Autor'
|
363
|
+
expect(seller_attributes[:created_at]).to eq '2019-01-01'
|
364
|
+
end
|
365
|
+
end
|
366
|
+
end
|
@@ -3,19 +3,6 @@ require 'spec_helper'
|
|
3
3
|
describe CatarsePagarme::CreditCardTransaction do
|
4
4
|
let(:payment) { create(:payment, value: 100) }
|
5
5
|
|
6
|
-
let(:pagarme_transaction) {
|
7
|
-
double({
|
8
|
-
id: 'abcd',
|
9
|
-
charge: true,
|
10
|
-
status: 'paid',
|
11
|
-
boleto_url: nil,
|
12
|
-
installments: 3,
|
13
|
-
acquirer_name: 'stone',
|
14
|
-
tid: '123123',
|
15
|
-
card_brand: 'visa'
|
16
|
-
})
|
17
|
-
}
|
18
|
-
|
19
6
|
let(:valid_attributes) do
|
20
7
|
{
|
21
8
|
payment_method: 'credit_card',
|
@@ -33,54 +20,255 @@ describe CatarsePagarme::CreditCardTransaction do
|
|
33
20
|
let(:card_transaction) { CatarsePagarme::CreditCardTransaction.new(valid_attributes, payment) }
|
34
21
|
|
35
22
|
before do
|
36
|
-
PagarMe::Transaction.stub(:new).and_return(pagarme_transaction)
|
23
|
+
# PagarMe::Transaction.stub(:new).and_return(pagarme_transaction)
|
37
24
|
CatarsePagarme::PaymentDelegator.any_instance.stub(:change_status_by_transaction).and_return(true)
|
38
25
|
CatarsePagarme.configuration.stub(:credit_card_tax).and_return(0.01)
|
39
26
|
end
|
40
27
|
|
41
|
-
describe '#
|
42
|
-
|
28
|
+
describe '#process!' do
|
29
|
+
let(:transaction) { double(status: 'refused') }
|
30
|
+
|
31
|
+
before do
|
32
|
+
allow(card_transaction).to receive(:transaction).and_return(transaction)
|
33
|
+
allow(card_transaction).to receive(:authorize!).and_return(true)
|
34
|
+
allow(card_transaction).to receive(:change_payment_state).and_return(true)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'authorizes transaction' do
|
38
|
+
expect(card_transaction).to receive(:authorize!)
|
39
|
+
card_transaction.process!
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when transaction is authorized' do
|
43
|
+
let(:transaction) { double(status: 'authorized', capture: true, refund: true) }
|
44
|
+
|
45
|
+
context 'when credit card was used before' do
|
46
|
+
before { allow(card_transaction).to receive(:was_credit_card_used_before?).and_return(true) }
|
47
|
+
|
48
|
+
it 'captures transaction' do
|
49
|
+
expect(card_transaction.transaction).to receive(:capture)
|
50
|
+
|
51
|
+
card_transaction.process!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when is a new credit credit card' do
|
56
|
+
let(:antifraud_outcome) { double(recommendation: :APPROVE) }
|
57
|
+
|
58
|
+
before do
|
59
|
+
allow(card_transaction).to receive(:was_credit_card_used_before?).and_return(false)
|
60
|
+
allow(card_transaction).to receive(:process_antifraud).and_return(antifraud_outcome)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'processes antifraud' do
|
64
|
+
expect(card_transaction).to receive(:process_antifraud)
|
65
|
+
|
66
|
+
card_transaction.process!
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when antifraud recommends approve transaction' do
|
70
|
+
let(:antifraud_outcome) { double(recommendation: :APPROVE) }
|
71
|
+
|
72
|
+
it 'captures transaction' do
|
73
|
+
expect(card_transaction.transaction).to receive(:capture)
|
74
|
+
|
75
|
+
card_transaction.process!
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'when antifraude recommends decline transaction' do
|
80
|
+
let(:antifraud_outcome) { double(recommendation: :DECLINE) }
|
81
|
+
|
82
|
+
it 'refunds transaction' do
|
83
|
+
expect(card_transaction.transaction).to receive(:refund)
|
84
|
+
|
85
|
+
card_transaction.process!
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'changes payment state' do
|
92
|
+
expect(card_transaction).to receive(:change_payment_state)
|
93
|
+
|
94
|
+
card_transaction.process!
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe '#authorize!' do
|
99
|
+
let(:attributes) do
|
100
|
+
{ amount: 10, card_hash: 'capgojepaogejpeoajgpeaoj124pih3p4h32p', postback_url: 'https://example.com/postback' }
|
101
|
+
end
|
102
|
+
|
103
|
+
let(:transaction) do
|
104
|
+
PagarMe::Transaction.new(
|
105
|
+
amount: attributes[:amount],
|
106
|
+
card_hash: attributes[:card_hash],
|
107
|
+
capture: false,
|
108
|
+
async: false,
|
109
|
+
postback_url: attributes[:postback_url]
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
before do
|
114
|
+
card_transaction.attributes = attributes
|
115
|
+
allow_any_instance_of(PagarMe::Transaction).to receive(:charge).and_return(true)
|
116
|
+
allow(card_transaction).to receive(:change_payment_state).and_return(true)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'build a new pagarme transaction' do
|
120
|
+
transaction_attributes = {
|
121
|
+
amount: attributes[:amount],
|
122
|
+
card_hash: attributes[:card_hash],
|
123
|
+
capture: false,
|
124
|
+
async: false,
|
125
|
+
postback_url: attributes[:postback_url]
|
126
|
+
}
|
127
|
+
|
128
|
+
allow(PagarMe::Transaction).to receive(:new).with(transaction_attributes).and_return(transaction)
|
129
|
+
|
130
|
+
card_transaction.authorize!
|
131
|
+
|
132
|
+
expect(card_transaction.transaction).to eq transaction
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'updates payment gateway and payment_method' do
|
136
|
+
card_transaction.authorize!
|
137
|
+
|
138
|
+
expect(payment.gateway).to eq 'Pagarme'
|
139
|
+
expect(payment.payment_method).to eq 'CartaoDeCredito'
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'charges transaction' do
|
143
|
+
allow(PagarMe::Transaction).to receive(:new).and_return(transaction)
|
144
|
+
expect(transaction).to receive(:charge)
|
145
|
+
|
146
|
+
card_transaction.authorize!
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'changes payment state' do
|
150
|
+
expect(card_transaction).to receive(:change_payment_state)
|
151
|
+
|
152
|
+
card_transaction.authorize!
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'when transaction status is refused' do
|
43
156
|
before do
|
44
|
-
|
45
|
-
|
46
|
-
|
157
|
+
allow_any_instance_of(PagarMe::Transaction).to receive(:status).and_return('refused')
|
158
|
+
allow(card_transaction.antifraud_wrapper).to receive(:send).with(analyze: false).and_return(true)
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'sends to antifraud' do
|
162
|
+
expect(card_transaction.antifraud_wrapper).to receive(:send).with(analyze: false)
|
47
163
|
|
48
|
-
card_transaction.
|
49
|
-
payment.reload
|
164
|
+
card_transaction.authorize! rescue nil
|
50
165
|
end
|
51
166
|
|
52
|
-
it
|
53
|
-
expect
|
167
|
+
it 'raises PagarMeError exception' do
|
168
|
+
expect do
|
169
|
+
card_transaction.authorize!
|
170
|
+
end.to raise_error(PagarMe::PagarMeError)
|
54
171
|
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'when save_card attribute is true' do
|
175
|
+
it 'saves credit card' do
|
176
|
+
card_transaction.attributes[:save_card] = true
|
177
|
+
expect(card_transaction).to receive(:save_user_credit_card)
|
55
178
|
|
56
|
-
|
57
|
-
expect(payment.gateway_fee).to eq(payment.pagarme_delegator.get_fee)
|
179
|
+
card_transaction.authorize!
|
58
180
|
end
|
181
|
+
end
|
182
|
+
|
183
|
+
context 'when save_card attribute is false' do
|
184
|
+
it 'does`t save credit card' do
|
185
|
+
card_transaction.attributes[:save_card] = false
|
186
|
+
expect(card_transaction).to_not receive(:save_user_credit_card)
|
59
187
|
|
60
|
-
|
61
|
-
expect(payment.gateway).to eq('Pagarme')
|
188
|
+
card_transaction.authorize!
|
62
189
|
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe '#was_credit_card_used_before?' do
|
194
|
+
let(:transaction) { double(card: double(id: '123')) }
|
63
195
|
|
64
|
-
|
65
|
-
|
196
|
+
before { card_transaction.transaction = transaction }
|
197
|
+
|
198
|
+
context 'when credit card was used before' do
|
199
|
+
before do
|
200
|
+
allow(PaymentEngines).to receive(:was_credit_card_used_before?)
|
201
|
+
.with(transaction.card.id)
|
202
|
+
.and_return(true)
|
66
203
|
end
|
67
204
|
|
68
|
-
it
|
69
|
-
expect(
|
205
|
+
it 'returns true' do
|
206
|
+
expect(card_transaction.was_credit_card_used_before?).to be_truthy
|
70
207
|
end
|
208
|
+
end
|
71
209
|
|
72
|
-
|
73
|
-
|
210
|
+
context 'when credit card wasn`t used before' do
|
211
|
+
before do
|
212
|
+
allow(PaymentEngines).to receive(:was_credit_card_used_before?)
|
213
|
+
.with(transaction.card.id)
|
214
|
+
.and_return(false)
|
74
215
|
end
|
75
216
|
|
76
|
-
it
|
77
|
-
expect(
|
217
|
+
it 'returns false' do
|
218
|
+
expect(card_transaction.was_credit_card_used_before?).to be_falsey
|
78
219
|
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
describe '#process_antifraud' do
|
224
|
+
it 'sends antifraud order' do
|
225
|
+
expect(card_transaction.antifraud_wrapper).to receive(:send).with(analyze: true).once
|
226
|
+
|
227
|
+
card_transaction.process_antifraud
|
228
|
+
end
|
229
|
+
|
230
|
+
context 'when there is an error' do
|
231
|
+
let(:exception) { RuntimeError.new('Error') }
|
232
|
+
before { allow(card_transaction.antifraud_wrapper).to receive(:send).and_raise(exception) }
|
79
233
|
|
80
|
-
it
|
81
|
-
expect(
|
234
|
+
it 'captures exception with Raven' do
|
235
|
+
expect(Raven).to receive(:capture_exception).with(exception)
|
236
|
+
|
237
|
+
card_transaction.process_antifraud
|
238
|
+
end
|
239
|
+
|
240
|
+
it 'returns a struct with decline recommendation' do
|
241
|
+
outcome = card_transaction.process_antifraud
|
242
|
+
|
243
|
+
expect(outcome.recommendation).to eq :DECLINE
|
82
244
|
end
|
83
245
|
end
|
84
246
|
end
|
85
247
|
|
248
|
+
describe '#antifraud_wrapper' do
|
249
|
+
let(:antifraud_wrapper) { double }
|
250
|
+
|
251
|
+
context 'when @antifraud_wrapper isn`t nil' do
|
252
|
+
it 'returns @antifraude_wrapper' do
|
253
|
+
card_transaction.instance_variable_set('@antifraud_wrapper', antifraud_wrapper)
|
254
|
+
expect(card_transaction.antifraud_wrapper).to eq antifraud_wrapper
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context 'when @antifraud_wrapper is nil' do
|
259
|
+
let(:transaction) { double }
|
260
|
+
|
261
|
+
before do
|
262
|
+
card_transaction.instance_variable_set('@antifraud_wrapper', nil)
|
263
|
+
card_transaction.transaction = transaction
|
264
|
+
allow(CatarsePagarme::AntifraudOrderWrapper).to receive(:new)
|
265
|
+
.with(valid_attributes, transaction)
|
266
|
+
.and_return(antifraud_wrapper)
|
267
|
+
end
|
268
|
+
|
269
|
+
it 'returns a new antifraud order wrapper' do
|
270
|
+
expect(card_transaction.antifraud_wrapper).to eq antifraud_wrapper
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
86
274
|
end
|