openpay 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.idea/.name +1 -0
  4. data/.idea/.rakeTasks +7 -0
  5. data/.idea/OpenPay.iml +614 -0
  6. data/.idea/dictionaries/ronnie.xml +7 -0
  7. data/.idea/encodings.xml +5 -0
  8. data/.idea/misc.xml +5 -0
  9. data/.idea/modules.xml +9 -0
  10. data/.idea/runConfigurations/Run_spec__bankaccounts_spec___OpenPay.xml +37 -0
  11. data/.idea/runConfigurations/Run_spec__customers_spec___OpenPay.xml +37 -0
  12. data/.idea/runConfigurations/all_specs.xml +41 -0
  13. data/.idea/scopes/scope_settings.xml +5 -0
  14. data/.idea/vcs.xml +7 -0
  15. data/Gemfile +11 -0
  16. data/LICENSE.txt +13 -0
  17. data/OpenPay.gemspec +24 -0
  18. data/README.md +370 -0
  19. data/Rakefile +1 -0
  20. data/lib/OpenPay/Cards.rb +76 -0
  21. data/lib/OpenPay/Charges.rb +79 -0
  22. data/lib/OpenPay/Customers.rb +195 -0
  23. data/lib/OpenPay/Fees.rb +5 -0
  24. data/lib/OpenPay/Payouts.rb +59 -0
  25. data/lib/OpenPay/Plans.rb +28 -0
  26. data/lib/OpenPay/Subscriptions.rb +58 -0
  27. data/lib/OpenPay/Transfers.rb +43 -0
  28. data/lib/OpenPay/bankaccounts.rb +66 -0
  29. data/lib/OpenPay/errors/open_pay_exception.rb +51 -0
  30. data/lib/OpenPay/errors/open_pay_exception_factory.rb +58 -0
  31. data/lib/OpenPay/errors/openpay_connection_exception.rb +3 -0
  32. data/lib/OpenPay/errors/openpay_transaction_exception.rb +5 -0
  33. data/lib/OpenPay/open_pay_resource.rb +242 -0
  34. data/lib/OpenPay/open_pay_resource_factory.rb +10 -0
  35. data/lib/OpenPay/openpay_api.rb +63 -0
  36. data/lib/OpenPay/version.rb +3 -0
  37. data/lib/openpay.rb +34 -0
  38. data/test/Factories.rb +258 -0
  39. data/test/spec/bankaccounts_spec.rb +187 -0
  40. data/test/spec/cards_spec.rb +411 -0
  41. data/test/spec/charges_spec.rb +377 -0
  42. data/test/spec/customers_spec.rb +230 -0
  43. data/test/spec/exceptions_spec.rb +138 -0
  44. data/test/spec/fees_spec.rb +113 -0
  45. data/test/spec/openpayresource_spec.rb +52 -0
  46. data/test/spec/payouts_spec.rb +197 -0
  47. data/test/spec/plans_spec.rb +229 -0
  48. data/test/spec/subscriptions_spec.rb +228 -0
  49. data/test/spec/transfers_spec.rb +221 -0
  50. data/test/spec_helper.rb +16 -0
  51. metadata +135 -0
@@ -0,0 +1,230 @@
1
+ require '../spec_helper'
2
+
3
+
4
+ describe Customers do
5
+
6
+
7
+ before(:all) do
8
+
9
+ @merchant_id='mywvupjjs9xdnryxtplq'
10
+ @private_key='sk_92b25d3baec149e6b428d81abfe37006'
11
+
12
+ @openpay=OpenpayApi.new(@merchant_id, @private_key)
13
+ @customers=@openpay.create(:customers)
14
+
15
+ end
16
+
17
+
18
+ describe '.create' do
19
+
20
+ it 'creates a customer' do
21
+
22
+
23
+ #creates a new customer
24
+ name='Juan'
25
+ last_name='Perez'
26
+
27
+ #json as input
28
+ customer_json= FactoryGirl.build(:customer, name: name, last_name: last_name).to_json
29
+ customer=@customers.create(customer_json)
30
+
31
+ #perform check
32
+ #json as output
33
+ expect(customer).to have_json_path('name')
34
+
35
+ #build hash out json string
36
+ customer_hash=JSON.parse customer
37
+ id=customer_hash['id']
38
+
39
+ #perform check
40
+ saved_customer=@customers.get(id)
41
+ expect(saved_customer['name']).to match(name)
42
+ expect(saved_customer['last_name']).to match(last_name)
43
+
44
+ #cleanup
45
+ @customers.delete(id)
46
+
47
+ end
48
+
49
+
50
+ it 'fails when passing invalid information' do
51
+
52
+ #check no errors
53
+ expect(@customers.errors?).to be_false
54
+
55
+ #invalid email
56
+ email='foo'
57
+ customer_hash = FactoryGirl.build(:customer, email: email)
58
+
59
+ #perform check
60
+ expect { @customers.create(customer_hash) }.to raise_exception OpenpayTransactionException
61
+ begin
62
+ @customers.create(customer_hash)
63
+ rescue OpenpayTransactionException => e
64
+ expect(e.http_code).to be 400
65
+ expect(e.description).to match 'email\' not a well-formed email address'
66
+
67
+ end
68
+
69
+ expect(@customers.errors?).to be_true
70
+
71
+ end
72
+
73
+ end
74
+
75
+
76
+ describe '.delete' do
77
+
78
+
79
+ it 'deletes an existing customer' do
80
+ #creates customer
81
+ customer_hash = FactoryGirl.build(:customer, name: :delete_me)
82
+ customer=@customers.create(customer_hash)
83
+ id=customer['id']
84
+ #delete customer
85
+ @customers.delete(id)
86
+ #perform check
87
+ expect { @customers.get(id) }.to raise_exception OpenpayTransactionException
88
+ end
89
+
90
+ end
91
+
92
+
93
+ describe '.get' do
94
+
95
+ it 'get a customer' do
96
+
97
+ #create customer
98
+ name='get_test'
99
+ customer_hash = FactoryGirl.build(:customer, name: name)
100
+ customer=@customers.create(customer_hash)
101
+ id=customer['id']
102
+ #perform check
103
+ expect(@customers.get(id)['name']).to match name
104
+ #cleanup
105
+ @customers.delete(id)
106
+ end
107
+
108
+
109
+ end
110
+
111
+
112
+ describe '.each' do
113
+ it 'list all customers' do
114
+ #clean state just in case
115
+ @customers.delete_all
116
+
117
+ #create customers
118
+ name='cust1'
119
+ customer_hash = FactoryGirl.build(:customer, name: name)
120
+ customer=@customers.create(customer_hash)
121
+ id=customer['id']
122
+ customer2=@customers.create(customer_hash)
123
+ id2=customer2['id']
124
+ #perform check
125
+ @customers.each do |cust|
126
+ expect(cust['name']).to match 'cust1'
127
+ end
128
+ #cleanup
129
+ @customers.delete(id)
130
+ @customers.delete(id2)
131
+ end
132
+ end
133
+
134
+
135
+ describe '.update' do
136
+
137
+ it 'updates an existing customer' do
138
+
139
+ # creates customer
140
+ name='customer_update_test'
141
+ customer_hash = FactoryGirl.build(:customer, name: name)
142
+
143
+ customer=@customers.create(customer_hash)
144
+ id=customer['id']
145
+
146
+ #update customer
147
+ name='new_name'
148
+ customer_hash = FactoryGirl.build(:customer, name: name)
149
+ @customers.update(customer_hash, id)
150
+ #perform check
151
+ expect(@customers.get(id)['name']).to match name
152
+
153
+ #cleanup
154
+ @customers.delete(id)
155
+
156
+ end
157
+
158
+ end
159
+
160
+
161
+
162
+
163
+ describe '.all' do
164
+
165
+ it 'list all the customers' do
166
+
167
+ #initial state check
168
+ expect(@customers.all.size).to be 0
169
+
170
+ # creates customer
171
+ name='customer_update_test'
172
+ customer_hash = FactoryGirl.build(:customer, name: name)
173
+ customer=@customers.create(customer_hash)
174
+
175
+ #performs check
176
+ expect(@customers.all.size).to be 1
177
+
178
+ #cleanup
179
+ @customers.delete(customer['id'])
180
+
181
+ #performs check
182
+ expect(@customers.all.size).to be 0
183
+
184
+
185
+ end
186
+
187
+
188
+ end
189
+
190
+
191
+ describe '.delete_all' do
192
+
193
+ it 'deletes all customer records' do
194
+
195
+ #create 5 customers
196
+ name='customer_update_test'
197
+ customer_hash = FactoryGirl.build(:customer, name: name)
198
+ 5.times do
199
+ @customers.create(customer_hash)
200
+ end
201
+
202
+ #performs check
203
+ expect(@customers.all.size).to be 5
204
+ @customers.delete_all
205
+ expect(@customers.all.size).to be 0
206
+ end
207
+
208
+ it 'raise an exception when used on Production' do
209
+
210
+ @openpayprod=OpenpayApi.new(@merchant_id, @private_key, true)
211
+ cust=@openpayprod.create(:customers)
212
+ expect { cust.delete_all }.to raise_exception OpenpayException
213
+
214
+
215
+ end
216
+
217
+
218
+ end
219
+
220
+
221
+ end
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
@@ -0,0 +1,138 @@
1
+ require '../spec_helper'
2
+
3
+
4
+ describe 'Openpay Exceptions' do
5
+
6
+
7
+ before(:all) do
8
+
9
+
10
+ @merchant_id='mywvupjjs9xdnryxtplq'
11
+ @private_key='sk_92b25d3baec149e6b428d81abfe37006'
12
+
13
+ @openpay=OpenpayApi.new(@merchant_id, @private_key)
14
+ @customers=@openpay.create(:customers)
15
+ @cards=@openpay.create(:cards)
16
+
17
+
18
+ end
19
+
20
+ describe OpenpayException do
21
+
22
+
23
+ it 'should raise an OpenpayException when a non given resource is passed to the api factory' do
24
+ expect { @openpay.create(:foo) }.to raise_exception OpenpayException
25
+ end
26
+
27
+ it 'should raise an OpenpayException when the delete_all method is used on production' do
28
+ @openpayprod=OpenpayApi.new(@merchant_id,@private_key,true)
29
+ cust=@openpayprod.create(:customers)
30
+ expect { cust.delete_all }.to raise_exception OpenpayException
31
+ end
32
+
33
+
34
+ end
35
+
36
+
37
+ describe OpenpayTransactionException do
38
+
39
+ it 'should fail when an invalid field-value is passed in *email' do
40
+ #invalid email format
41
+ email='foo'
42
+ customer_hash = FactoryGirl.build(:customer, email: email)
43
+
44
+ #perform checks
45
+ expect { @customers.create(customer_hash) }.to raise_exception OpenpayTransactionException
46
+ begin
47
+ @customers.create(customer_hash)
48
+ rescue OpenpayTransactionException => e
49
+ #should have the corresponding attributes coming from the json message
50
+ expect(e.http_code).to be 400
51
+ expect(e.error_code).to be 1001
52
+ expect(e.description).to match 'email\' not a well-formed email address'
53
+ expect(e.json_body).to have_json_path('category')
54
+ end
55
+ end
56
+
57
+
58
+ it ' raise an OpenpayTransactionException when trying to delete a non existing bank account ' do
59
+ #non existing resource
60
+ #perform checks
61
+ expect { @customers.delete('1111') }.to raise_exception OpenpayTransactionException
62
+ begin
63
+ @customers.delete('1111')
64
+ rescue OpenpayTransactionException => e
65
+ #should have the corresponding attributes coming from the json message
66
+ expect(e.http_code).to be 404
67
+ expect(e.error_code).to be 1005
68
+ expect(e.description).to match "The customer with id '1111' does not exist"
69
+ expect(e.json_body).to have_json_path('category')
70
+ end
71
+ end
72
+
73
+
74
+
75
+ it 'fails when trying to create an existing card' do
76
+
77
+ #create customer
78
+ customers=@openpay.create(:customers)
79
+ customer_hash = FactoryGirl.build(:customer, name: 'Juan', last_name: 'Perez')
80
+ customer=customers.create(customer_hash)
81
+ #create card using json
82
+ card_json = FactoryGirl.build(:valid_card).to_json
83
+ card=@cards.create(card_json)
84
+ #perform check
85
+ expect { @cards.create(card_json) }.to raise_error(OpenpayTransactionException)
86
+
87
+ #cleanup
88
+ @customers.delete(customer['id'])
89
+ card_hash=JSON.parse card
90
+ @cards.delete card_hash['id']
91
+ end
92
+
93
+
94
+ it 'raise an OpenpayTransactionException when using an expired card' do
95
+ card_hash = FactoryGirl.build(:expired_card)
96
+ expect { @cards.create(card_hash) }.to raise_error(OpenpayTransactionException)
97
+ begin
98
+ @cards.create(card_hash)
99
+ rescue OpenpayTransactionException => e
100
+ expect(e.description).to match 'The card has expired.'
101
+ expect(e.error_code).to be 3002
102
+
103
+ end
104
+
105
+ end
106
+
107
+
108
+ end
109
+
110
+
111
+ describe OpenpayConnectionException do
112
+
113
+ it 'raise an OpenpayConnectionException when provided credentials are invalid' do
114
+
115
+ merchant_id='santa'
116
+ private_key='invalid'
117
+
118
+ openpay=OpenpayApi.new(merchant_id, private_key)
119
+ customers=openpay.create(:customers)
120
+ expect { customers.delete('1111') }.to raise_exception OpenpayConnectionException
121
+
122
+
123
+ begin
124
+ customers.delete('1111')
125
+ rescue OpenpayConnectionException => e
126
+ #should have the corresponding attributes coming from the json message
127
+ expect(e.http_code).to be 401
128
+ expect(e.error_code).to be 1002
129
+ expect(e.description).to match 'The api key or merchant id are invalid.'
130
+ expect(e.json_body).to have_json_path('category')
131
+ end
132
+
133
+
134
+ end
135
+ end
136
+
137
+
138
+ end
@@ -0,0 +1,113 @@
1
+ require '../spec_helper'
2
+
3
+ describe Fees do
4
+
5
+ before(:all) do
6
+
7
+ @merchant_id='mywvupjjs9xdnryxtplq'
8
+ @private_key='sk_92b25d3baec149e6b428d81abfe37006'
9
+
10
+ @openpay=OpenpayApi.new(@merchant_id, @private_key)
11
+ @customers=@openpay.create(:customers)
12
+ @cards=@openpay.create(:cards)
13
+ @charges=@openpay.create(:charges)
14
+
15
+ @bank_accounts=@openpay.create(:bankaccounts)
16
+
17
+ @fees=@openpay.create(:fees)
18
+
19
+ end
20
+
21
+
22
+ after(:all) do
23
+ @bank_accounts.delete_all
24
+ @customers.delete_all
25
+ end
26
+
27
+
28
+ describe '.create' do
29
+
30
+ #In order to create a fee a charge should exists
31
+ it 'creates a fee ' do
32
+ #create new customer
33
+ customer_hash= FactoryGirl.build(:customer)
34
+ customer=@customers.create(customer_hash)
35
+
36
+ #create customer card
37
+ card_hash=FactoryGirl.build(:valid_card)
38
+ card=@cards.create(card_hash, customer['id'])
39
+
40
+ #create charge
41
+ charge_hash=FactoryGirl.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
42
+ @charges.create(charge_hash, customer['id'])
43
+
44
+ #create customer fee
45
+ fee_hash = FactoryGirl.build(:fee, customer_id: customer['id'])
46
+ @fees.create(fee_hash)
47
+
48
+ #performs check
49
+ expect(@fees.all.first['customer_id']).to match customer['id']
50
+
51
+ #cleanup
52
+ @cards.delete(card['id'], customer['id'])
53
+ @customers.delete(customer['id'])
54
+
55
+ end
56
+
57
+
58
+ it 'creates a fee using a json message' do
59
+ #create new customer
60
+ customer_hash= FactoryGirl.build(:customer)
61
+ customer=@customers.create(customer_hash)
62
+
63
+ #create customer card
64
+ card_hash=FactoryGirl.build(:valid_card)
65
+ card=@cards.create(card_hash, customer['id'])
66
+
67
+ #create charge
68
+ charge_hash=FactoryGirl.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
69
+ @charges.create(charge_hash, customer['id'])
70
+
71
+ #create customer fee using json
72
+ fee_json =%^{"customer_id":"#{customer['id']}","amount":"12.50","description":"Cobro de Comisión"}^
73
+ #performs check , it returns json
74
+ expect(@fees.create(fee_json)).to have_json_path('amount')
75
+
76
+ end
77
+
78
+ end
79
+
80
+ describe '.all' do
81
+
82
+ it 'get all fees' do
83
+
84
+ #create new customer
85
+ customer_hash= FactoryGirl.build(:customer)
86
+ customer=@customers.create(customer_hash)
87
+
88
+ #create customer card
89
+ card_hash=FactoryGirl.build(:valid_card)
90
+ card=@cards.create(card_hash, customer['id'])
91
+
92
+ #create charge
93
+ charge_hash=FactoryGirl.build(:card_charge, source_id: card['id'], order_id: card['id'], amount: 4000)
94
+ @charges.create(charge_hash, customer['id'])
95
+
96
+ #create customer fee
97
+ fee_hash = FactoryGirl.build(:fee, customer_id: customer['id'])
98
+ @fees.create(fee_hash)
99
+
100
+ #performs check
101
+ expect(@fees.all.first['customer_id']).to match customer['id']
102
+
103
+ #cleanup
104
+ @cards.delete(card['id'], customer['id'])
105
+ @customers.delete(customer['id'])
106
+
107
+ end
108
+ end
109
+ end
110
+
111
+
112
+
113
+