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,172 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Customers do
4
+
5
+ before(:all) do
6
+ @merchant_id = 'mywvupjjs9xdnryxtplq'
7
+ @private_key = 'sk_92b25d3baec149e6b428d81abfe37006'
8
+ #LOG.level=Logger::DEBUG
9
+ @openpay = OpenpayApi.new(@merchant_id, @private_key, "mx")
10
+ @customers = @openpay.create(:customers)
11
+ end
12
+
13
+ after(:all) do
14
+ # @customers.delete_all
15
+ end
16
+
17
+ it 'has all required methods' do
18
+ %w(all each create get list delete).each do |meth|
19
+ expect(@customers).to respond_to(meth)
20
+ end
21
+ end
22
+
23
+ describe '.create' do
24
+ it 'creates a customer' do
25
+ #creates a new customer
26
+ name = 'Juan'
27
+ last_name = 'Perez'
28
+ #json as input
29
+ customer_json = FactoryBot.build(:customer, name: name, last_name: last_name).to_json
30
+ customer = @customers.create(customer_json)
31
+ #perform check
32
+ #json as output
33
+ expect(customer).to have_json_path('name')
34
+ #build hash out json string
35
+ customer_hash = JSON.parse customer
36
+ id = customer_hash['id']
37
+ #perform check
38
+ saved_customer = @customers.get(id)
39
+ expect(saved_customer['name']).to match(name)
40
+ expect(saved_customer['last_name']).to match(last_name)
41
+ #cleanup
42
+ @customers.delete(id)
43
+
44
+ end
45
+
46
+ it 'fails when passing invalid information' do
47
+ #check no errors
48
+ expect(@customers.errors?).to eq false
49
+ #invalid email
50
+ email = 'foo'
51
+ customer_hash = FactoryBot.build(:customer, email: email)
52
+ #perform check
53
+ expect { @customers.create(customer_hash) }.to raise_exception OpenpayTransactionException
54
+ begin
55
+ @customers.create(customer_hash)
56
+ rescue OpenpayTransactionException => e
57
+ expect(e.http_code).to be 400
58
+ expect(e.description).to match /'foo' is not a valid email address/
59
+ end
60
+ expect(@customers.errors?).to eq true
61
+ end
62
+ end
63
+
64
+ describe '.delete' do
65
+ it 'deletes an existing customer' do
66
+ #creates customer
67
+ customer_hash = FactoryBot.build(:customer, name: :delete_me)
68
+ customer = @customers.create(customer_hash)
69
+ id = customer['id']
70
+ #delete customer
71
+ @customers.delete(id)
72
+ #perform check
73
+ expect { @customers.get(id) }.to raise_exception OpenpayTransactionException
74
+ end
75
+ end
76
+
77
+ describe '.get' do
78
+ it 'get a customer' do
79
+ #create customer
80
+ name = 'get_test'
81
+ customer_hash = FactoryBot.build(:customer, name: name)
82
+ customer = @customers.create(customer_hash)
83
+ id = customer['id']
84
+ #perform check
85
+ expect(@customers.get(id)['name']).to match name
86
+ #cleanup
87
+ @customers.delete(id)
88
+ end
89
+ end
90
+
91
+ describe '.each' do
92
+ it 'list all customers' do
93
+ @customers.each do |cust|
94
+ expect(cust['id']).to match /.+/
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '.update' do
100
+ it 'updates an existing customer' do
101
+ # creates customer
102
+ name = 'customer_update_test'
103
+ customer_hash = FactoryBot.build(:customer, name: name)
104
+ customer = @customers.create(customer_hash)
105
+ id = customer['id']
106
+ #update customer
107
+ name = 'new_name'
108
+ customer_hash = FactoryBot.build(:customer, name: name)
109
+ @customers.update(customer_hash, id)
110
+ #perform check
111
+ expect(@customers.get(id)['name']).to match name
112
+ #cleanup
113
+ @customers.delete(id)
114
+ end
115
+
116
+ end
117
+ describe '.list' do
118
+ it 'list customers given the filter' do
119
+ # creates customer
120
+ name = 'customer_update_test'
121
+ customer_hash = FactoryBot.build(:customer, name: name)
122
+ customer = @customers.create(customer_hash)
123
+ id = customer['id']
124
+ search_params = OpenpayUtils::SearchParams.new
125
+ search_params.limit = 1
126
+ search_params.creation_gte = '2020-01-01'
127
+ #perform check
128
+ expect(@customers.list(search_params).size).to eq 1
129
+ #cleanup
130
+ @customers.delete(id)
131
+ end
132
+ end
133
+
134
+ describe '.all' do
135
+ it 'all the customers' do
136
+ #initial state check
137
+ search_params = OpenpayUtils::SearchParams.new
138
+ search_params.limit = 10000
139
+ initial_num = @customers.list(search_params).size
140
+ # creates customer
141
+ name = 'customer_update_test'
142
+ customer_hash = FactoryBot.build(:customer, name: name)
143
+ customer = @customers.create(customer_hash)
144
+ #performs check
145
+ expect(@customers.list(search_params).size).to eq initial_num + 1
146
+ #cleanup
147
+ @customers.delete(customer['id'])
148
+ end
149
+ end
150
+
151
+ # describe '.delete_all' do
152
+ # it 'deletes all customer records' do
153
+ # #create 5 customers
154
+ # name='customer_update_test'
155
+ # customer_hash = FactoryBot.build(:customer, name: name)
156
+ # 5.times do
157
+ # @customers.create(customer_hash)
158
+ # end
159
+ # #performs check
160
+ # expect(@customers.all.size).to be > 4
161
+ # @customers.delete_all
162
+ # expect(@customers.all.size).to be < 11
163
+ # end
164
+ # it 'raise an exception when used on Production' do
165
+ # @openpayprod=OpenpayApi.new(@merchant_id, @private_key, true)
166
+ # cust=@openpayprod.create(:customers)
167
+ # expect { cust.delete_all }.to raise_exception OpenpayException
168
+ # end
169
+ # end
170
+
171
+ end
172
+
@@ -0,0 +1,105 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'Openpay Exceptions' do
4
+
5
+ before(:all) do
6
+
7
+ @merchant_id = 'mywvupjjs9xdnryxtplq'
8
+ @private_key = 'sk_92b25d3baec149e6b428d81abfe37006'
9
+
10
+ #LOG.level=Logger::DEBUG
11
+
12
+ @openpay = OpenpayApi.new(@merchant_id, @private_key, "mx")
13
+ @customers = @openpay.create(:customers)
14
+ @cards = @openpay.create(:cards)
15
+
16
+ end
17
+
18
+ describe OpenpayException do
19
+
20
+ it 'should raise an OpenpayException when a non given resource is passed to the api factory' do
21
+ expect { @openpay.create(:foo) }.to raise_exception OpenpayException
22
+ end
23
+
24
+ it 'should raise an OpenpayException when the delete_all method is used on production' do
25
+ @openpayprod = OpenpayApi.new(@merchant_id, @private_key, "mx")
26
+ cust = @openpayprod.create(:customers)
27
+ expect { cust.delete_all }.to raise_exception OpenpayException
28
+ end
29
+
30
+ end
31
+
32
+ describe OpenpayTransactionException do
33
+
34
+ it 'should fail when an invalid field-value is passed in *email' do
35
+ #invalid email format
36
+ email = 'foo'
37
+ customer_hash = FactoryBot.build(:customer, email: email)
38
+
39
+ #perform checks
40
+ expect { @customers.create(customer_hash) }.to raise_exception OpenpayTransactionException
41
+ begin
42
+ @customers.create(customer_hash)
43
+ rescue OpenpayTransactionException => e
44
+ #should have the corresponding attributes coming from the json message
45
+ expect(e.http_code).to be 400
46
+ expect(e.error_code).to be 1001
47
+ #expect(e.description).to match 'not a well-formed email address'
48
+ expect(e.json_body).to have_json_path('category')
49
+ end
50
+ end
51
+
52
+ it ' raise an OpenpayTransactionException when trying to delete a non existing bank account ' do
53
+ #non existing resource
54
+ #perform checks
55
+ expect { @customers.delete('1111') }.to raise_exception OpenpayTransactionException
56
+ begin
57
+ @customers.delete('1111')
58
+ rescue OpenpayTransactionException => e
59
+ #should have the corresponding attributes coming from the json message
60
+ expect(e.http_code).to be 404
61
+ expect(e.error_code).to be 1005
62
+ expect(e.description).to match "The customer with id '1111' does not exist"
63
+ expect(e.json_body).to have_json_path('category')
64
+ end
65
+ end
66
+
67
+ it 'raise an OpenpayTransactionException when using an expired card' do
68
+ card_hash = FactoryBot.build(:expired_card)
69
+ expect { @cards.create(card_hash) }.to raise_error(OpenpayTransactionException)
70
+ begin
71
+ @cards.create(card_hash)
72
+ rescue OpenpayTransactionException => e
73
+ expect(e.description).to match 'The card has expired'
74
+ expect(e.error_code).to be 3002
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ describe OpenpayConnectionException do
82
+
83
+ it 'raise an OpenpayConnectionException when provided credentials are invalid' do
84
+
85
+ merchant_id = 'santa'
86
+ private_key = 'invalid'
87
+
88
+ openpay = OpenpayApi.new(merchant_id, private_key,"mx")
89
+ customers = openpay.create(:customers)
90
+ expect { customers.delete('1111') }.to raise_exception OpenpayConnectionException
91
+
92
+ begin
93
+ customers.delete('1111')
94
+ rescue OpenpayConnectionException => e
95
+ #should have the corresponding attributes coming from the json message
96
+ expect(e.http_code).to be 401
97
+ expect(e.error_code).to be 1002
98
+ expect(e.description).to match 'The api key or merchant id are invalid'
99
+ expect(e.json_body).to have_json_path('category')
100
+ end
101
+
102
+ end
103
+ end
104
+
105
+ end
@@ -0,0 +1,166 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Fees do
4
+
5
+ before(:all) do
6
+
7
+ @merchant_id='mywvupjjs9xdnryxtplq'
8
+ @private_key='sk_92b25d3baec149e6b428d81abfe37006'
9
+
10
+ #LOG.level=Logger::DEBUG
11
+
12
+ @openpay=OpenpayApi.new(@merchant_id, @private_key,"mx")
13
+ @customers=@openpay.create(:customers)
14
+ @cards=@openpay.create(:cards)
15
+ @charges=@openpay.create(:charges)
16
+
17
+ @bank_accounts=@openpay.create(:bankaccounts)
18
+
19
+ @fees=@openpay.create(:fees)
20
+
21
+ end
22
+
23
+ after(:all) do
24
+ @customers.delete_all
25
+ end
26
+
27
+ it 'has all required methods' do
28
+ %w(all each create get list delete).each do |meth|
29
+ expect(@customers).to respond_to(meth)
30
+ end
31
+ end
32
+
33
+ describe '.create' do
34
+
35
+ #In order to create a fee a charge should exists
36
+ it 'creates a fee ' do
37
+ #create new customer
38
+ customer_hash= FactoryBot.build(:customer)
39
+ customer=@customers.create(customer_hash)
40
+
41
+ #create customer card
42
+ card_hash=FactoryBot.build(:valid_card)
43
+ card=@cards.create(card_hash, customer['id'])
44
+
45
+ #create charge
46
+ charge_hash=FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
47
+ @charges.create(charge_hash, customer['id'])
48
+ sleep(50)
49
+ #create customer fee
50
+ fee_hash = FactoryBot.build(:fee, customer_id: customer['id'])
51
+ @fees.create(fee_hash)
52
+
53
+ #performs check
54
+ expect(@fees.all.first['customer_id']).to match customer['id']
55
+
56
+ #cleanup
57
+ @cards.delete(card['id'], customer['id'])
58
+ @customers.delete(customer['id'])
59
+
60
+ end
61
+
62
+ it 'creates a fee using a json message' do
63
+ #create new customer
64
+ customer_hash= FactoryBot.build(:customer)
65
+ customer=@customers.create(customer_hash)
66
+
67
+ #create customer card
68
+ card_hash=FactoryBot.build(:valid_card)
69
+ card=@cards.create(card_hash, customer['id'])
70
+
71
+ #create charge
72
+ charge_hash=FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
73
+ @charges.create(charge_hash, customer['id'])
74
+ sleep(50)
75
+
76
+ #create customer fee using json
77
+ fee_json =%^{"customer_id":"#{customer['id']}","amount":"12.50","description":"Cobro de Comision"}^
78
+ #performs check , it returns json
79
+ expect(@fees.create(fee_json)).to have_json_path('amount')
80
+
81
+ end
82
+
83
+ end
84
+
85
+ describe '.each' do
86
+
87
+ it 'iterates over all elements' do
88
+ @fees.each do |fee|
89
+ expect(fee['description']).to match /\s+/
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ describe '.list' do
96
+
97
+ it 'list fees with a given filter' do
98
+
99
+ #create new customer
100
+ customer_hash= FactoryBot.build(:customer)
101
+ customer=@customers.create(customer_hash)
102
+
103
+ #create customer card
104
+ card_hash=FactoryBot.build(:valid_card)
105
+ card=@cards.create(card_hash, customer['id'])
106
+
107
+ #create charge
108
+ charge_hash=FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
109
+ @charges.create(charge_hash, customer['id'])
110
+ sleep(50)
111
+
112
+ #create customer fee
113
+ fee_hash = FactoryBot.build(:fee, customer_id: customer['id'])
114
+ @fees.create(fee_hash)
115
+ sleep(50)
116
+
117
+ #create customer fee
118
+ fee_hash = FactoryBot.build(:fee, customer_id: customer['id'])
119
+ @fees.create(fee_hash)
120
+
121
+
122
+ #performs check
123
+ search_params = OpenpayUtils::SearchParams.new
124
+ search_params.limit = 1
125
+ expect(@fees.list(search_params).size).to eq 1
126
+
127
+
128
+ #cleanup
129
+ @cards.delete(card['id'], customer['id'])
130
+ @customers.delete(customer['id'])
131
+
132
+ end
133
+
134
+ end
135
+
136
+ describe '.all' do
137
+
138
+ it 'get all fees' do
139
+
140
+ #create new customer
141
+ customer_hash= FactoryBot.build(:customer)
142
+ customer=@customers.create(customer_hash)
143
+
144
+ #create customer card
145
+ card_hash=FactoryBot.build(:valid_card)
146
+ card=@cards.create(card_hash, customer['id'])
147
+
148
+ #create charge
149
+ charge_hash=FactoryBot.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
150
+ @charges.create(charge_hash, customer['id'])
151
+ sleep(50)
152
+
153
+ #create customer fee
154
+ fee_hash = FactoryBot.build(:fee, customer_id: customer['id'])
155
+ @fees.create(fee_hash)
156
+
157
+ #performs check
158
+ expect(@fees.all.first['amount']).to be_a Float
159
+
160
+ #cleanup
161
+ @cards.delete(card['id'], customer['id'])
162
+ @customers.delete(customer['id'])
163
+
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,54 @@
1
+ require_relative '../spec_helper'
2
+
3
+
4
+ describe 'OpenPayResource' do
5
+
6
+
7
+
8
+
9
+ before(:all) do
10
+
11
+ @merchant_id='mywvupjjs9xdnryxtplq'
12
+ @private_key='sk_92b25d3baec149e6b428d81abfe37006'
13
+
14
+ #LOG.level=Logger::DEBUG
15
+
16
+ @openpay=OpenpayApi.new(@merchant_id,@private_key,"mx")
17
+ @cards=@openpay.create(:cards)
18
+
19
+ end
20
+
21
+
22
+ describe '.hash2json' do
23
+
24
+
25
+ it 'converts a ruby hash into a json string' do
26
+ card_hash = FactoryBot.build(:valid_card, holder_name: 'Juan')
27
+ json=@cards.hash2json(card_hash)
28
+ expect(json).to have_json_path('holder_name')
29
+ expect(json).to have_json_path('expiration_year')
30
+ expect(json).to have_json_path('bank_code')
31
+
32
+ end
33
+
34
+
35
+ end
36
+
37
+
38
+ describe '.json2hash' do
39
+
40
+
41
+ it 'converts json into a ruby hash' do
42
+ card_hash = FactoryBot.build(:valid_card, holder_name: 'Pepe')
43
+ json=@cards.hash2json(card_hash)
44
+ jash=@cards.json2hash(json)
45
+ expect(jash).to be_a Hash
46
+ expect(jash['holder_name']).to match 'Pepe'
47
+
48
+
49
+ end
50
+
51
+ end
52
+
53
+
54
+ end