openpay_copemx 3.0.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 (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +10 -0
  5. data/LICENSE.txt +13 -0
  6. data/README.md +1984 -0
  7. data/Rakefile +16 -0
  8. data/lib/openpay/bankaccounts.rb +58 -0
  9. data/lib/openpay/cards.rb +73 -0
  10. data/lib/openpay/charges.rb +101 -0
  11. data/lib/openpay/colombia/cards_co.rb +73 -0
  12. data/lib/openpay/colombia/charges_co.rb +76 -0
  13. data/lib/openpay/colombia/customers_co.rb +166 -0
  14. data/lib/openpay/colombia/plans_co.rb +17 -0
  15. data/lib/openpay/colombia/pse_co.rb +17 -0
  16. data/lib/openpay/colombia/subscriptions_co.rb +50 -0
  17. data/lib/openpay/colombia/tokens_co.rb +5 -0
  18. data/lib/openpay/colombia/webhooks_co.rb +5 -0
  19. data/lib/openpay/customers.rb +200 -0
  20. data/lib/openpay/errors/openpay_connection_exception.rb +3 -0
  21. data/lib/openpay/errors/openpay_exception.rb +29 -0
  22. data/lib/openpay/errors/openpay_exception_factory.rb +56 -0
  23. data/lib/openpay/errors/openpay_transaction_exception.rb +5 -0
  24. data/lib/openpay/fees.rb +5 -0
  25. data/lib/openpay/open_pay_resource.rb +295 -0
  26. data/lib/openpay/open_pay_resource_factory.rb +17 -0
  27. data/lib/openpay/openpay_api.rb +72 -0
  28. data/lib/openpay/payouts.rb +55 -0
  29. data/lib/openpay/peru/cards_pe.rb +73 -0
  30. data/lib/openpay/peru/charges_pe.rb +76 -0
  31. data/lib/openpay/peru/checkouts_pe.rb +51 -0
  32. data/lib/openpay/peru/customers_pe.rb +79 -0
  33. data/lib/openpay/peru/tokens_pe.rb +5 -0
  34. data/lib/openpay/peru/webhooks_pe.rb +5 -0
  35. data/lib/openpay/plans.rb +17 -0
  36. data/lib/openpay/points.rb +10 -0
  37. data/lib/openpay/subscriptions.rb +50 -0
  38. data/lib/openpay/tokens.rb +7 -0
  39. data/lib/openpay/transfers.rb +39 -0
  40. data/lib/openpay/utils/country.rb +3 -0
  41. data/lib/openpay/utils/search_params.rb +24 -0
  42. data/lib/openpay/webhooks.rb +9 -0
  43. data/lib/openpay.rb +55 -0
  44. data/lib/version.rb +3 -0
  45. data/openpay.gemspec +30 -0
  46. data/test/Factories.rb +524 -0
  47. data/test/spec/bankaccounts_spec.rb +52 -0
  48. data/test/spec/cards_spec.rb +437 -0
  49. data/test/spec/charges_spec.rb +382 -0
  50. data/test/spec/colombia/cards_col_spec.rb +364 -0
  51. data/test/spec/colombia/charges_col_spec.rb +258 -0
  52. data/test/spec/colombia/customers_co_spec.rb +151 -0
  53. data/test/spec/colombia/plans_col_spec.rb +133 -0
  54. data/test/spec/colombia/pse_col_spec.rb +49 -0
  55. data/test/spec/colombia/subscriptions_col_spec.rb +230 -0
  56. data/test/spec/colombia/tokens_col_spec.rb +38 -0
  57. data/test/spec/customers_spec.rb +172 -0
  58. data/test/spec/exceptions_spec.rb +105 -0
  59. data/test/spec/fees_spec.rb +166 -0
  60. data/test/spec/openpayresource_spec.rb +54 -0
  61. data/test/spec/payouts_spec.rb +231 -0
  62. data/test/spec/peru/cards_pe_spec.rb +363 -0
  63. data/test/spec/peru/charges_pe_spec.rb +218 -0
  64. data/test/spec/peru/checkouts_pe_spec.rb +71 -0
  65. data/test/spec/peru/customers_pe_spec.rb +151 -0
  66. data/test/spec/peru/tokens_pe_spec.rb +38 -0
  67. data/test/spec/peru/webhook_pe_spec.rb +50 -0
  68. data/test/spec/plans_spec.rb +158 -0
  69. data/test/spec/points_spec.rb +26 -0
  70. data/test/spec/requesttimeout_spec.rb +22 -0
  71. data/test/spec/subscriptions_spec.rb +294 -0
  72. data/test/spec/transfers_spec.rb +219 -0
  73. data/test/spec/utils/search_params_spec.rb +36 -0
  74. data/test/spec_helper.rb +24 -0
  75. metadata +219 -0
@@ -0,0 +1,364 @@
1
+ require_relative '../../spec_helper'
2
+
3
+
4
+ describe Cards do
5
+
6
+ # %w(create delete get list each all fail).each do |meth|
7
+ # end
8
+
9
+ before(:all) do
10
+ @merchant_id='mwf7x79goz7afkdbuyqd'
11
+ @private_key='sk_94a89308b4d7469cbda762c4b392152a'
12
+ #LOG.level=Logger::DEBUG
13
+ @openpay=OpenpayApi.new(@merchant_id, @private_key,"co")
14
+ @cards=@openpay.create(:cards)
15
+ @customers=@openpay.create(:customers)
16
+ @cards.delete_all
17
+ end
18
+
19
+ after(:all) do
20
+ #each test should build and clean it's own mess.
21
+ end
22
+
23
+ it 'has all required methods' do
24
+ %w(all each create get list delete).each do |meth|
25
+ expect(@cards).to respond_to(meth)
26
+ end
27
+ end
28
+
29
+ describe '.create' do
30
+ it 'creates merchant card' do
31
+ #creates merchant card
32
+ card_hash = FactoryBot.build(:valid_card_col)
33
+ cards=@cards.create(card_hash)
34
+ expect(cards).to be_a(Hash)
35
+ id=cards['id']
36
+ name='Vicente Olmos'
37
+ #perform check
38
+ card=@cards.get(id)
39
+ expect(card['holder_name']).to match(name)
40
+ expect(card['card_number']).to match('1111')
41
+ #cleanup
42
+ @cards.delete(card['id'])
43
+ end
44
+
45
+ it 'creates a customer card' do
46
+ #creates a customer
47
+ card_hash = FactoryBot.build(:valid_card_col, holder_name: 'Pepe')
48
+ customers=@openpay.create(:customers)
49
+ customer_hash = FactoryBot.build(:customer_col)
50
+ customer=customers.create(customer_hash)
51
+ #creates a customer card
52
+ cards=@cards.create(card_hash, customer['id'])
53
+ expect(cards).to be_a(Hash)
54
+ id=cards['id']
55
+ #performs check
56
+ customer_cards=customers.all_cards(customer['id'])
57
+ expect(customer_cards.size).to be 1
58
+ expect(cards['holder_name']).to match 'Pepe'
59
+ stored_card=@cards.get(id, customer['id'])
60
+ expect(stored_card['holder_name']).to match 'Pepe'
61
+ expect(stored_card['id']).to match id
62
+ #cleanup
63
+ @cards.delete(id, customer['id'])
64
+ @customers.delete(customer['id'])
65
+ end
66
+
67
+ it 'create an existing merchant card' do
68
+ #creates merchant card
69
+ card_hash = FactoryBot.build(:valid_card_col)
70
+ card=@cards.create(card_hash)
71
+ #cleanup
72
+ @cards.delete(card['id'])
73
+
74
+ card=@cards.create(card_hash)
75
+ #cleanup
76
+ @cards.delete(card['id'])
77
+
78
+ end
79
+
80
+ it 'trying to create an existing customer card' do
81
+ #creates a customer
82
+ card_hash = FactoryBot.build(:valid_card_col, holder_name: 'Pepe')
83
+ customers=@openpay.create(:customers)
84
+ customer_hash = FactoryBot.build(:customer_col)
85
+ customer=customers.create(customer_hash)
86
+ #creates a customer card
87
+ card=@cards.create(card_hash, customer['id'])
88
+ expect(card).to be_a(Hash)
89
+ #cleanup
90
+ @cards.delete(card['id'], customer['id'])
91
+ @customers.delete(customer['id'])
92
+ end
93
+
94
+ it 'fails when using an expired card' do
95
+ card_hash = FactoryBot.build(:expired_card_col)
96
+ #check
97
+ expect { @cards.create(card_hash) }.to raise_error(OpenpayTransactionException)
98
+ #extended check
99
+ begin
100
+ @cards.create(card_hash)
101
+ rescue OpenpayTransactionException => e
102
+ expect(e.description).to match 'The card has expired'
103
+ expect(e.error_code).to be 3002
104
+ end
105
+ end
106
+
107
+ it 'fails when using a stolen card' do
108
+ card_json = FactoryBot.build(:valid_card_col, card_number: '4000000000000119')
109
+ expect { @cards.create(card_json) }.to raise_error(OpenpayTransactionException)
110
+ end
111
+ end
112
+
113
+ describe '.each' do
114
+
115
+ it 'iterates over all existing merchant cards' do
116
+ @cards.each do |card|
117
+ expect(card['expiration_year']).to match '20'
118
+ end
119
+ end
120
+
121
+ it 'iterates over all existing customer cards' do
122
+
123
+ #creates a customer
124
+ card_hash = FactoryBot.build(:valid_card, holder_name: 'Pepe')
125
+ customers=@openpay.create(:customers)
126
+ customer_hash = FactoryBot.build(:customer_col)
127
+ customer=customers.create(customer_hash)
128
+
129
+ #creates a customer card
130
+ card=@cards.create(card_hash, customer['id'])
131
+ expect(card).to be_a(Hash)
132
+
133
+ @cards.each(customer['id']) do |c|
134
+ expect(c['expiration_year']).to match '25'
135
+ end
136
+
137
+ #cleanup
138
+ @cards.delete(card['id'], customer['id'])
139
+ @customers.delete(customer['id'])
140
+
141
+ end
142
+
143
+ end
144
+
145
+ describe '.delete' do
146
+
147
+ it 'deletes a merchant card' do
148
+
149
+ #creates merchant card
150
+ card_hash = FactoryBot.build(:valid_card)
151
+ cards=@cards.create(card_hash)
152
+ expect(cards).to be_a(Hash)
153
+
154
+ id=cards['id']
155
+ name='Vicente Olmos'
156
+
157
+ #perform check
158
+ card=@cards.delete(id)
159
+ expect { @cards.get(id) }.to raise_exception(OpenpayTransactionException)
160
+
161
+ end
162
+
163
+ it 'fails to deletes a non existing card' do
164
+ expect { @cards.delete('1111111') }.to raise_exception(OpenpayTransactionException)
165
+ end
166
+
167
+ it 'deletes a customer card' do
168
+
169
+ #create customer
170
+ customers=@openpay.create(:customers)
171
+ customer_hash = FactoryBot.build(:customer_col, name: 'Juan', last_name: 'Paez')
172
+ customer=customers.create(customer_hash)
173
+
174
+ #create customer card
175
+ card_hash = FactoryBot.build(:valid_card)
176
+ card=@cards.create(card_hash, customer['id'])
177
+
178
+ #delete card
179
+ @cards.delete(card['id'], customer['id'])
180
+
181
+ #perform check
182
+ expect { @cards.get(card['id'], customer['id']) }.to raise_exception(OpenpayTransactionException)
183
+
184
+ end
185
+
186
+ it 'fails to deletes a non existing customer card' do
187
+ expect { @cards.delete('1111111', '1111') }.to raise_exception(OpenpayTransactionException)
188
+ end
189
+
190
+ end
191
+
192
+ describe '.get' do
193
+ it ' gets an existing merchant card' do
194
+ #create merchant card
195
+ bank_name='Banamex'
196
+ card_hash = FactoryBot.build(:valid_card_col)
197
+ card=@cards.create(card_hash)
198
+ id=card['id']
199
+ #pass just a single parameter for getting merchant cards
200
+ stored_card = @cards.get(id)
201
+ bank=stored_card['bank_name']
202
+ expect(bank).to match bank_name
203
+ #cleanup
204
+ @cards.delete(id)
205
+ end
206
+
207
+ it 'fails when getting a non existing card' do
208
+ expect { @cards.get('11111') }.to raise_exception(OpenpayTransactionException)
209
+ end
210
+
211
+ it ' gets an existing customer card' do
212
+ #create customer
213
+ customer_hash = FactoryBot.build(:customer_col)
214
+ customer=@customers.create(customer_hash)
215
+ #creates card
216
+ bank_name='Banamex'
217
+ card_hash = FactoryBot.build(:valid_card_col)
218
+ card=@cards.create(card_hash, customer['id'])
219
+ id=card['id']
220
+ #two parameters for getting customer cards
221
+ stored_cards = @cards.get(id, customer['id'])
222
+ bank=stored_cards['bank_name']
223
+ #perform check
224
+ expect(bank).to match bank_name
225
+ #cleanup
226
+ @cards.delete(id, customer['id'])
227
+ end
228
+
229
+ it 'fails when getting a non existing customer card' do
230
+ expect { @cards.get('11111', '1111') }.to raise_exception(OpenpayTransactionException)
231
+ end
232
+
233
+ end
234
+
235
+ describe '.all' do
236
+
237
+ it 'all merchant cards' do
238
+ #check initial state
239
+ expect(@cards.all.size).to be 0
240
+ #create merchant card
241
+ card_hash = FactoryBot.build(:valid_card_col)
242
+ card=@cards.create(card_hash)
243
+ id=card['id']
244
+ #perform check
245
+ expect(@cards.all.size).to be 1
246
+ #cleanup
247
+ @cards.delete(id)
248
+ end
249
+
250
+ it 'all customer cards' do
251
+ #create customer
252
+ customer_hash = FactoryBot.build(:customer_col)
253
+ customer=@customers.create(customer_hash)
254
+ #check initial state
255
+ expect(@cards.all(customer['id']).size).to be 0
256
+ #create customer card
257
+ card_hash = FactoryBot.build(:valid_card_col)
258
+ card=@cards.create(card_hash, customer['id'])
259
+ id=card['id']
260
+ #perform check
261
+ expect(@cards.all(customer['id']).size).to be 1
262
+ #cleanup
263
+ @cards.delete(id, customer['id'])
264
+ end
265
+
266
+ it 'cards for a non existing customer' do
267
+ expect { @cards.all('111111') }.to raise_exception OpenpayTransactionException
268
+ end
269
+
270
+ end
271
+
272
+ describe '.list' do
273
+ it 'list the merchant cards using a filter' do
274
+ #create merchant card
275
+ card_hash = FactoryBot.build(:valid_card_col)
276
+ card1 = @cards.create(card_hash)
277
+ card_hash = FactoryBot.build(:valid_card2_col)
278
+ card2 = @cards.create(card_hash)
279
+ search_params = OpenpayUtils::SearchParams.new
280
+ search_params.limit = 1
281
+ expect(@cards.list(search_params).size).to eq 1
282
+ @cards.delete_all
283
+ end
284
+
285
+ it 'list the customer cards using a filter' do
286
+ #create customer
287
+ customer_hash = FactoryBot.build(:customer_col)
288
+ customer = @customers.create(customer_hash)
289
+ #creates card
290
+ bank_name ='Banamex'
291
+ card_hash = FactoryBot.build(:valid_card_col)
292
+ card = @cards.create(card_hash, customer['id'])
293
+ id = card['id']
294
+ #creates card 2
295
+ bank_name = 'Bancomer'
296
+ card_hash = FactoryBot.build(:valid_card2_col)
297
+ card = @cards.create(card_hash, customer['id'])
298
+ id = card['id']
299
+ search_params = OpenpayUtils::SearchParams.new
300
+ search_params.limit = 1
301
+ expect(@cards.all(customer['id']).size).to eq 2
302
+ expect(@cards.list(search_params , customer['id']).size).to eq 1
303
+ @cards.delete_all(customer['id'])
304
+ end
305
+
306
+ it 'list the merchant cards using a filter creation and amount' do
307
+ #create merchant card
308
+ card_hash = FactoryBot.build(:valid_card_col)
309
+ card1 = @cards.create(card_hash)
310
+ card_hash = FactoryBot.build(:valid_card2_col)
311
+ card2 = @cards.create(card_hash)
312
+ search_params = OpenpayUtils::SearchParams.new
313
+ search_params.limit = 1
314
+ creation_date = "2013-11-01"
315
+ search_params.creation_gte = creation_date
316
+ expect(@cards.list(search_params).size).to eq 1
317
+ @cards.delete_all
318
+ end
319
+ end
320
+
321
+
322
+ describe '.delete_all' do
323
+
324
+ it 'raise an exception when used on Production' do
325
+ openpayprod=OpenpayApi.new(@merchant_id, @private_key, true,"co")
326
+ cust=openpayprod.create(:customers)
327
+ expect { cust.delete_all }.to raise_error
328
+ end
329
+
330
+ it 'deletes all existing merchant cards' do
331
+ #create merchant card
332
+ card_hash = FactoryBot.build(:valid_card_col)
333
+ @cards.create(card_hash)
334
+ #using json just for fun ...and test
335
+ card2_json = FactoryBot.build(:valid_card2_col).to_json
336
+ @cards.create(card2_json)
337
+ #perform check
338
+ expect(@cards.all.size).to be 2
339
+ #cleanup
340
+ @cards.delete_all
341
+ #perform check
342
+ expect(@cards.all.size).to be 0
343
+ end
344
+
345
+ it 'deletes all existing cards for a given customer' do
346
+ #creates customer
347
+ customer_hash = FactoryBot.build(:customer_col)
348
+ customer=@customers.create(customer_hash)
349
+ #check initial state
350
+ expect(@cards.all(customer['id']).size).to be 0
351
+ #create card
352
+ card_hash = FactoryBot.build(:valid_card_col)
353
+ card=@cards.create(card_hash, customer['id'])
354
+ #perform check
355
+ expect(@cards.all(customer['id']).size).to be 1
356
+ #cleanup
357
+ @cards.delete_all(customer['id'])
358
+ #check
359
+ expect(@cards.all(customer['id']).size).to be 0
360
+ end
361
+
362
+ end
363
+
364
+ end
@@ -0,0 +1,258 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe Charges do
4
+
5
+ before(:all) do
6
+ @merchant_id = 'mwf7x79goz7afkdbuyqd'
7
+ @private_key = 'sk_94a89308b4d7469cbda762c4b392152a'
8
+ @openpay = OpenpayApi.new(@merchant_id, @private_key, "co")
9
+ @customers = @openpay.create(:customers)
10
+ #LOG.level=Logger::DEBUG
11
+ @charges = @openpay.create(:charges)
12
+ @cards = @openpay.create(:cards)
13
+
14
+ end
15
+
16
+ it 'has all required methods' do
17
+ %w(all each create get list delete).each do |meth|
18
+ expect(@charges).to respond_to(meth)
19
+ end
20
+ end
21
+
22
+ describe '.create' do
23
+
24
+ it 'creates a new merchant charge using the card method using a pre-stored card' do
25
+ #create card
26
+ card_hash = FactoryBot.build(:valid_card_col)
27
+ card = @cards.create(card_hash)
28
+ #create charge attached to prev created card
29
+ charge_hash = FactoryBot.build(:card_charge_col, source_id: card['id'], order_id: card['id'], amount: 101)
30
+ charge = @charges.create(charge_hash)
31
+ #perform check
32
+ stored_charge = @charges.get(charge['id'])
33
+ expect(stored_charge['amount']).to be_within(0.1).of(101)
34
+ #clean up
35
+ @cards.delete(card['id'])
36
+ end
37
+
38
+ it 'creates a new customer charge using the card method using a pre-stored card' do
39
+ #create new customer
40
+ customer_hash = FactoryBot.build(:customer_col)
41
+ customer = @customers.create(customer_hash)
42
+ #create customer card
43
+ card_hash = FactoryBot.build(:valid_card_col)
44
+ card = @cards.create(card_hash, customer['id'])
45
+ #create charge
46
+ charge_hash = FactoryBot.build(:card_charge_col_2, source_id: card['id'], order_id: card['id'])
47
+ charge = @charges.create(charge_hash, customer['id'])
48
+ #perform check
49
+ stored_charge = @charges.get(charge['id'], customer['id'])
50
+ expect(stored_charge['amount']).to be_within(0.1).of(1000)
51
+ #clean up
52
+ @cards.delete(card['id'], customer['id'])
53
+ #no se puede borrar usuario por transacciones asociadas
54
+ # @customers.delete(customer['id'])
55
+ end
56
+
57
+ it 'creates a new merchant charge using the card method using a pre-stored card STORE' do
58
+ #create card
59
+ card_hash = FactoryBot.build(:valid_card_col)
60
+ card = @cards.create(card_hash)
61
+ #create charge attached to prev created card
62
+ charge_hash = FactoryBot.build(:card_charge_store_col, order_id: card['id'], amount: 101)
63
+ charge = @charges.create(charge_hash)
64
+ #perform check
65
+ stored_charge = @charges.get(charge['id'])
66
+ paymentMethod = stored_charge['payment_method']
67
+ expect(paymentMethod['type']).to eq('store')
68
+ #clean up
69
+ @cards.delete(card['id'])
70
+ end
71
+
72
+ it 'creates a new customer charge using the card method using a pre-stored card STORE' do
73
+ #create new customer
74
+ customer_hash = FactoryBot.build(:customer_col)
75
+ customer = @customers.create(customer_hash)
76
+ #create customer card
77
+ card_hash = FactoryBot.build(:valid_card_col)
78
+ card = @cards.create(card_hash, customer['id'])
79
+ #create charge
80
+ charge_hash = FactoryBot.build(:card_charge_store_col_2, method: 'store', order_id: card['id'])
81
+ charge = @charges.create(charge_hash, customer['id'])
82
+ #perform check
83
+ stored_charge = @charges.get(charge['id'], customer['id'])
84
+ paymentMethod = stored_charge['payment_method']
85
+ expect(paymentMethod['type']).to eq('store')
86
+ #clean up
87
+ @cards.delete(card['id'], customer['id'])
88
+ #no se puede borrar usuario por transacciones asociadas
89
+ # @customers.delete(customer['id'])
90
+ end
91
+
92
+ end
93
+
94
+ describe '.get' do
95
+
96
+ it 'gets a merchant charge' do
97
+ #create card
98
+ card_hash = FactoryBot.build(:valid_card_col)
99
+ card = @cards.create(card_hash)
100
+ #create charge attached to prev created card
101
+ charge_hash = FactoryBot.build(:card_charge_col, source_id: card['id'], order_id: card['id'], amount: 505)
102
+ charge = @charges.create(charge_hash)
103
+ #perform check
104
+ stored_charge = @charges.get(charge['id'])
105
+ expect(stored_charge['amount']).to be_within(0.1).of(505)
106
+ #clean up
107
+ @cards.delete(card['id'])
108
+
109
+ end
110
+
111
+ it 'gets a customer charge' do
112
+ #create new customer
113
+ customer_hash = FactoryBot.build(:customer_col)
114
+ customer = @customers.create(customer_hash)
115
+ #create customer card
116
+ card_hash = FactoryBot.build(:valid_card_col)
117
+ card = @cards.create(card_hash, customer['id'])
118
+ #create charge
119
+ charge_hash = FactoryBot.build(:card_charge_col_2, source_id: card['id'], order_id: card['id'], amount: 4000)
120
+ charge = @charges.create(charge_hash, customer['id'])
121
+ #perform check
122
+ stored_charge = @charges.get(charge['id'], customer['id'])
123
+ expect(stored_charge['amount']).to be_within(0.5).of(4000)
124
+ #clean up
125
+ @cards.delete(card['id'], customer['id'])
126
+ # no se puede borrar usuario por transacciones asociadas
127
+ # @customers.delete(customer['id'])
128
+
129
+ end
130
+
131
+ end
132
+
133
+ describe '.all' do
134
+
135
+ it 'all merchant charges' do
136
+ #TODO test can be improved, but since charges cannot be deleted it make this difficult
137
+ expect(@charges.all.size).to be_a Integer
138
+ end
139
+
140
+ it 'all customer charges' do
141
+ #create new customer
142
+ customer_hash = FactoryBot.build(:customer_col)
143
+ customer = @customers.create(customer_hash)
144
+ expect(@charges.all(customer['id']).size).to be 0
145
+ @customers.delete(customer['id'])
146
+
147
+ end
148
+
149
+ end
150
+
151
+ describe '.refund' do
152
+ #Refunds apply only for card charges
153
+ it 'refunds an existing merchant charge' do
154
+ #create card
155
+ card_hash = FactoryBot.build(:valid_card_col)
156
+ card = @cards.create(card_hash)
157
+ #create charge attached to prev created card
158
+ charge_hash = FactoryBot.build(:card_charge_col, source_id: card['id'], order_id: card['id'], amount: 505)
159
+ charge = @charges.create(charge_hash)
160
+ #creates refund_description
161
+ refund_description = FactoryBot.build(:refund_description)
162
+ expect(@charges.get(charge['id'])['refund']).to be nil
163
+ @charges.refund(charge['id'], refund_description)
164
+ expect(@charges.get(charge['id'])['refund']['amount']).to be_within(0.1).of(505)
165
+ #clean up
166
+ @cards.delete(card['id'])
167
+ end
168
+
169
+ it 'refunds an existing customer charge' do
170
+ #create new customer
171
+ customer_hash = FactoryBot.build(:customer_col)
172
+ customer = @customers.create(customer_hash)
173
+ #create customer card
174
+ card_hash = FactoryBot.build(:valid_card_col)
175
+ card = @cards.create(card_hash, customer['id'])
176
+ #create charge
177
+ charge_hash = FactoryBot.build(:card_charge_col_2, source_id: card['id'], order_id: card['id'], amount: 100)
178
+ charge = @charges.create(charge_hash, customer['id'])
179
+ #creates refund_description
180
+ refund_description = FactoryBot.build(:refund_description)
181
+ #perform check
182
+ expect(@charges.get(charge['id'], customer['id'])['refund']).to be nil
183
+ sleep(50)
184
+ @charges.refund(charge['id'], refund_description, customer['id'])
185
+ expect(@charges.get(charge['id'], customer['id'])['refund']['amount']).to be_within(0.1).of(100)
186
+ #clean up
187
+ @cards.delete(card['id'], customer['id'])
188
+ # No se puede borrar usuario por transacciones asociadas
189
+ # @customers.delete(customer['id'])
190
+ end
191
+
192
+ end
193
+
194
+ describe '.list' do
195
+
196
+ it 'list customer charges' do
197
+
198
+ #create new customer
199
+ customer_hash = FactoryBot.build(:customer_col)
200
+ customer = @customers.create(customer_hash)
201
+ #create customer card
202
+ card_hash = FactoryBot.build(:valid_card)
203
+ card = @cards.create(card_hash, customer['id'])
204
+ #create charge
205
+ charge_hash = FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'])
206
+ charge = @charges.create(charge_hash, customer['id'])
207
+ charge_hash = FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'] + "1")
208
+ charge2 = @charges.create(charge_hash, customer['id'])
209
+ #perform check
210
+ search_params = OpenpayUtils::SearchParams.new
211
+ search_params.limit = 1
212
+ search_params.creation_gte = '2000-01-01'
213
+ search_params.amount_gte = 1
214
+ expect(@charges.all(customer['id']).size).to eq 2
215
+ expect(@charges.list(search_params, customer['id']).size).to eq 1
216
+ #clean up
217
+ @cards.delete(card['id'], customer['id'])
218
+
219
+ # No se puede borrar usuario por transacciones asociadas
220
+ # @customers.delete(customer['id'])
221
+
222
+ end
223
+
224
+ end
225
+
226
+ describe '.each' do
227
+ it 'iterates over merchant charges' do
228
+ @charges.each do |charge|
229
+ #perform check.
230
+ expect(charge['amount']).to be_a Float
231
+ end
232
+ end
233
+ it 'iterate over customer charges' do
234
+ #create new customer
235
+ customer_hash = FactoryBot.build(:customer_col)
236
+ customer = @customers.create(customer_hash)
237
+ #create customer card
238
+ card_hash = FactoryBot.build(:valid_card_col)
239
+ card = @cards.create(card_hash, customer['id'])
240
+ #create charge
241
+ charge_hash = FactoryBot.build(:card_charge_col_2, source_id: card['id'], order_id: card['id'], amount: 4)
242
+ @charges.create(charge_hash, customer['id'])
243
+ charge2_hash = FactoryBot.build(:card_charge_col_2, source_id: card['id'], order_id: card['id' + "2"], amount: 4)
244
+ @charges.create(charge2_hash, customer['id'])
245
+ @charges.each(customer['id']) do |charge|
246
+ expect(charge['operation_type']).to match 'in'
247
+ expect(charge['amount']).to be_within(0.1).of(4)
248
+ end
249
+
250
+ #cleanup
251
+ @cards.delete(card['id'], customer['id'])
252
+ # No se puede borrar usuario por transacciones asociadas
253
+ # @customers.delete(customer['id'])
254
+ end
255
+
256
+ end
257
+
258
+ end