openpay 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/OpenPay.iml +614 -0
- data/.idea/dictionaries/ronnie.xml +7 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/runConfigurations/Run_spec__bankaccounts_spec___OpenPay.xml +37 -0
- data/.idea/runConfigurations/Run_spec__customers_spec___OpenPay.xml +37 -0
- data/.idea/runConfigurations/all_specs.xml +41 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +13 -0
- data/OpenPay.gemspec +24 -0
- data/README.md +370 -0
- data/Rakefile +1 -0
- data/lib/OpenPay/Cards.rb +76 -0
- data/lib/OpenPay/Charges.rb +79 -0
- data/lib/OpenPay/Customers.rb +195 -0
- data/lib/OpenPay/Fees.rb +5 -0
- data/lib/OpenPay/Payouts.rb +59 -0
- data/lib/OpenPay/Plans.rb +28 -0
- data/lib/OpenPay/Subscriptions.rb +58 -0
- data/lib/OpenPay/Transfers.rb +43 -0
- data/lib/OpenPay/bankaccounts.rb +66 -0
- data/lib/OpenPay/errors/open_pay_exception.rb +51 -0
- data/lib/OpenPay/errors/open_pay_exception_factory.rb +58 -0
- data/lib/OpenPay/errors/openpay_connection_exception.rb +3 -0
- data/lib/OpenPay/errors/openpay_transaction_exception.rb +5 -0
- data/lib/OpenPay/open_pay_resource.rb +242 -0
- data/lib/OpenPay/open_pay_resource_factory.rb +10 -0
- data/lib/OpenPay/openpay_api.rb +63 -0
- data/lib/OpenPay/version.rb +3 -0
- data/lib/openpay.rb +34 -0
- data/test/Factories.rb +258 -0
- data/test/spec/bankaccounts_spec.rb +187 -0
- data/test/spec/cards_spec.rb +411 -0
- data/test/spec/charges_spec.rb +377 -0
- data/test/spec/customers_spec.rb +230 -0
- data/test/spec/exceptions_spec.rb +138 -0
- data/test/spec/fees_spec.rb +113 -0
- data/test/spec/openpayresource_spec.rb +52 -0
- data/test/spec/payouts_spec.rb +197 -0
- data/test/spec/plans_spec.rb +229 -0
- data/test/spec/subscriptions_spec.rb +228 -0
- data/test/spec/transfers_spec.rb +221 -0
- data/test/spec_helper.rb +16 -0
- metadata +135 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
class OpenPayResourceFactory
|
2
|
+
def OpenPayResourceFactory::create(resource,merchant_id,private_key,production)
|
3
|
+
begin
|
4
|
+
Object.const_get(resource.capitalize).new(merchant_id,private_key,production)
|
5
|
+
rescue NameError
|
6
|
+
raise OpenpayException.new("Invalid resource name:#{resource}",false)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'open_pay_resource_factory'
|
3
|
+
require 'base64'
|
4
|
+
require 'rest-client'
|
5
|
+
require 'uri'
|
6
|
+
require 'errors/open_pay_exception'
|
7
|
+
|
8
|
+
|
9
|
+
LOG= Logger.new(STDOUT)
|
10
|
+
|
11
|
+
#change to Logger::DEBUG if need trace information
|
12
|
+
#due the nature of the information, we recommend to never use a log file when in debug
|
13
|
+
LOG.level=Logger::DEBUG
|
14
|
+
|
15
|
+
|
16
|
+
class OpenpayApi
|
17
|
+
|
18
|
+
|
19
|
+
#API Endpoints
|
20
|
+
API_DEV='https://sandbox-api.openpay.mx/v1/'
|
21
|
+
API_PROD='https://api.openpay.mx'
|
22
|
+
|
23
|
+
|
24
|
+
#by default the testing environment is used
|
25
|
+
def initialize(merchant_id, private_key,production=false)
|
26
|
+
@merchant_id=merchant_id
|
27
|
+
@private_key=private_key
|
28
|
+
@production=production
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
# @return [nil]
|
33
|
+
def create(resource)
|
34
|
+
klass=OpenPayResourceFactory::create(resource, @merchant_id,@private_key,@production)
|
35
|
+
#attach api hook to be able to refere to same API instance from created resources
|
36
|
+
#TODO we may move it to the initialize method
|
37
|
+
klass.api_hook=self
|
38
|
+
klass
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def OpenpayApi::base_url(production)
|
43
|
+
if production
|
44
|
+
API_PROD
|
45
|
+
else
|
46
|
+
API_DEV
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
def env
|
52
|
+
if @production
|
53
|
+
:production
|
54
|
+
else
|
55
|
+
:test
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
end
|
data/lib/openpay.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#openpay version
|
2
|
+
require 'openpay/version'
|
3
|
+
|
4
|
+
#external dependencies
|
5
|
+
require 'rest-client'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
module Openpay
|
9
|
+
|
10
|
+
#api setup / constants
|
11
|
+
require 'openpay/openpay_api'
|
12
|
+
|
13
|
+
#base class
|
14
|
+
require 'openpay/open_pay_resource'
|
15
|
+
|
16
|
+
#resource classes
|
17
|
+
require 'openpay/bankaccounts'
|
18
|
+
require 'openpay/cards'
|
19
|
+
require 'openpay/charges'
|
20
|
+
require 'openpay/customers'
|
21
|
+
require 'openpay/fees'
|
22
|
+
require 'openpay/payouts'
|
23
|
+
require 'openpay/plans'
|
24
|
+
require 'openpay/subscriptions'
|
25
|
+
require 'openpay/transfers'
|
26
|
+
require 'openpay/charges'
|
27
|
+
|
28
|
+
#exceptions
|
29
|
+
require 'openpay/errors/open_pay_exception_factory'
|
30
|
+
require 'openpay/errors/open_pay_exception'
|
31
|
+
require 'openpay/errors/openpay_transaction_exception'
|
32
|
+
require 'openpay/errors/openpay_connection_exception'
|
33
|
+
|
34
|
+
end
|
data/test/Factories.rb
ADDED
@@ -0,0 +1,258 @@
|
|
1
|
+
require 'factory_girl'
|
2
|
+
|
3
|
+
|
4
|
+
FactoryGirl.define do
|
5
|
+
|
6
|
+
factory :customer, class:Hash do
|
7
|
+
name 'Ronnie'
|
8
|
+
last_name 'Bermejo'
|
9
|
+
email 'ronnie.bermejo.mx@gmail.com'
|
10
|
+
phone_number '0180012345'
|
11
|
+
address {{
|
12
|
+
postal_code: '76190',
|
13
|
+
state: 'QRO',
|
14
|
+
line1: 'LINE1',
|
15
|
+
line2: 'LINE2',
|
16
|
+
line3: 'LINE3',
|
17
|
+
country_code: 'MX',
|
18
|
+
city: 'Queretaro',
|
19
|
+
}}
|
20
|
+
|
21
|
+
initialize_with { attributes }
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
factory :valid_card, class:Hash do
|
28
|
+
|
29
|
+
bank_name 'visa'
|
30
|
+
holder_name 'Vicente Olmos'
|
31
|
+
expiration_month '09'
|
32
|
+
card_number '4111111111111111'
|
33
|
+
expiration_year '14'
|
34
|
+
bank_code 'bmx'
|
35
|
+
cvv2 '111'
|
36
|
+
address {{
|
37
|
+
postal_code: '76190',
|
38
|
+
state: 'QRO',
|
39
|
+
line1: 'LINE1',
|
40
|
+
line2: 'LINE2',
|
41
|
+
line3: 'LINE3',
|
42
|
+
country_code: 'MX',
|
43
|
+
city: 'Queretaro',
|
44
|
+
}}
|
45
|
+
|
46
|
+
initialize_with { attributes }
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
factory :valid_card2, class:Hash do
|
52
|
+
|
53
|
+
bank_name 'visa'
|
54
|
+
holder_name 'Alma Olmos'
|
55
|
+
expiration_month '09'
|
56
|
+
card_number '4242424242424242'
|
57
|
+
expiration_year '14'
|
58
|
+
bank_code 'bmx'
|
59
|
+
cvv2 '111'
|
60
|
+
address {{
|
61
|
+
postal_code: '76190',
|
62
|
+
state: 'QRO',
|
63
|
+
line1: 'LINE1',
|
64
|
+
line2: 'LINE2',
|
65
|
+
line3: 'LINE3',
|
66
|
+
country_code: 'MX',
|
67
|
+
city: 'Queretaro',
|
68
|
+
}}
|
69
|
+
|
70
|
+
initialize_with { attributes }
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
factory :only_deposit, class:Hash do
|
77
|
+
|
78
|
+
bank_name 'visa'
|
79
|
+
holder_name 'Alma Olmos'
|
80
|
+
expiration_month '09'
|
81
|
+
card_number '4444444444444448'
|
82
|
+
expiration_year '14'
|
83
|
+
bank_code 'bmx'
|
84
|
+
cvv2 '111'
|
85
|
+
address {{
|
86
|
+
postal_code: '76190',
|
87
|
+
state: 'QRO',
|
88
|
+
line1: 'LINE1',
|
89
|
+
line2: 'LINE2',
|
90
|
+
line3: 'LINE3',
|
91
|
+
country_code: 'MX',
|
92
|
+
city: 'Queretaro',
|
93
|
+
}}
|
94
|
+
|
95
|
+
initialize_with { attributes }
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
factory :expired_card, class:Hash do
|
101
|
+
|
102
|
+
bank_name 'visa'
|
103
|
+
holder_name 'Vicente Olmos'
|
104
|
+
expiration_month '09'
|
105
|
+
card_number '4000000000000069'
|
106
|
+
expiration_year '14'
|
107
|
+
bank_code 'bmx'
|
108
|
+
cvv2 '111'
|
109
|
+
address {{
|
110
|
+
postal_code: '76190',
|
111
|
+
state: 'QRO',
|
112
|
+
line1: 'LINE1',
|
113
|
+
line2: 'LINE2',
|
114
|
+
line3: 'LINE3',
|
115
|
+
country_code: 'MX',
|
116
|
+
city: 'Queretaro',
|
117
|
+
}}
|
118
|
+
|
119
|
+
initialize_with { attributes }
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
factory :bank_account, class:Hash do
|
126
|
+
|
127
|
+
holder_name 'Juan Perez'
|
128
|
+
self.alias 'Cuenta principal'
|
129
|
+
clabe '032180000118359719'
|
130
|
+
|
131
|
+
initialize_with { attributes }
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
factory :card_charge, class:Hash do
|
138
|
+
|
139
|
+
|
140
|
+
amount "1000"
|
141
|
+
description "Cargo inicial a tarjeta"
|
142
|
+
source_id "string"
|
143
|
+
method "card"
|
144
|
+
order_id 'required'
|
145
|
+
|
146
|
+
initialize_with { attributes }
|
147
|
+
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
factory :bank_charge, class:Hash do
|
152
|
+
|
153
|
+
amount "10000"
|
154
|
+
description "Cargo con banco"
|
155
|
+
source_id "string"
|
156
|
+
order_id 'required'
|
157
|
+
method "bank_account"
|
158
|
+
|
159
|
+
|
160
|
+
initialize_with { attributes }
|
161
|
+
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
factory :refund_description, class:Hash do
|
168
|
+
description 'A petición del cliente'
|
169
|
+
initialize_with { attributes }
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
factory :fee, class:Hash do
|
175
|
+
customer_id 'dvocf97jd20es3tw5laz'
|
176
|
+
amount '12.50'
|
177
|
+
description 'Cobro de Comisión'
|
178
|
+
initialize_with { attributes }
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
factory :payout_card, class:Hash do
|
185
|
+
|
186
|
+
method 'card'
|
187
|
+
destination_id '4444444444444448'
|
188
|
+
amount '2'
|
189
|
+
description 'Retiro de saldo semanal'
|
190
|
+
|
191
|
+
initialize_with { attributes }
|
192
|
+
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
|
197
|
+
factory :payout_bank_account, class:Hash do
|
198
|
+
|
199
|
+
method 'bank_account'
|
200
|
+
amount '1000'
|
201
|
+
destination_id 'required'
|
202
|
+
description 'Retiro de saldo semanal'
|
203
|
+
|
204
|
+
initialize_with { attributes }
|
205
|
+
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
factory :plan, class:Hash do
|
213
|
+
|
214
|
+
amount '150.00'
|
215
|
+
status_after_retry 'cancelled'
|
216
|
+
retry_times 2
|
217
|
+
name 'Curso de ingles'
|
218
|
+
repeat_unit 'week'
|
219
|
+
trial_days 30
|
220
|
+
repeat_every 1
|
221
|
+
initialize_with { attributes }
|
222
|
+
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
factory :transfer, class:Hash do
|
229
|
+
|
230
|
+
customer_id 'required'
|
231
|
+
amount 12.50
|
232
|
+
description 'Transferencia entre cuentas'
|
233
|
+
initialize_with { attributes }
|
234
|
+
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
|
239
|
+
factory :subscription, class:Hash do
|
240
|
+
|
241
|
+
trial_days 30
|
242
|
+
card_id 'required'
|
243
|
+
plan_id 'required'
|
244
|
+
initialize_with { attributes }
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require '../spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Bankaccounts do
|
5
|
+
|
6
|
+
#bankaccounts for merchant cannot be created using the api
|
7
|
+
#the merchant bank account should be created using the Openpay dashboard
|
8
|
+
before(:all) do
|
9
|
+
|
10
|
+
@merchant_id='mywvupjjs9xdnryxtplq'
|
11
|
+
@private_key='sk_92b25d3baec149e6b428d81abfe37006'
|
12
|
+
|
13
|
+
@openpay=OpenpayApi.new(@merchant_id,@private_key)
|
14
|
+
@bank_accounts=@openpay.create(:bankaccounts)
|
15
|
+
@customers=@openpay.create(:customers)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
after(:all) do
|
21
|
+
@customers.delete_all
|
22
|
+
@bank_accounts.delete_all
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.create' do
|
26
|
+
|
27
|
+
it 'creates a customer bank account' do
|
28
|
+
customer_hash= FactoryGirl.build(:customer)
|
29
|
+
customer=@customers.create(customer_hash)
|
30
|
+
|
31
|
+
account_hash=FactoryGirl.build(:bank_account)
|
32
|
+
bank=@bank_accounts.create(account_hash,customer['id'])
|
33
|
+
|
34
|
+
bank_account=@bank_accounts.get(customer['id'],bank['id'])
|
35
|
+
expect(bank_account['alias']).to match 'Cuenta principal'
|
36
|
+
|
37
|
+
@bank_accounts.delete(customer['id'],bank['id'])
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
describe '.get' do
|
46
|
+
|
47
|
+
it 'get a given bank account for a given customer' do
|
48
|
+
|
49
|
+
customer_hash= FactoryGirl.build(:customer)
|
50
|
+
customer=@customers.create(customer_hash)
|
51
|
+
|
52
|
+
account_hash=FactoryGirl.build(:bank_account)
|
53
|
+
bank=@bank_accounts.create(account_hash,customer['id'])
|
54
|
+
|
55
|
+
bank_account=@bank_accounts.get(customer['id'],bank['id'])
|
56
|
+
expect(bank_account['alias']).to match 'Cuenta principal'
|
57
|
+
@bank_accounts.delete(customer['id'],bank['id'])
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
describe '.each' do
|
66
|
+
|
67
|
+
|
68
|
+
it 'iterator for all given customer bank accounts' do
|
69
|
+
|
70
|
+
customer_hash= FactoryGirl.build(:customer)
|
71
|
+
customer=@customers.create(customer_hash)
|
72
|
+
|
73
|
+
account_hash=FactoryGirl.build(:bank_account)
|
74
|
+
bank=@bank_accounts.create(account_hash,customer['id'])
|
75
|
+
|
76
|
+
@bank_accounts.each(customer['id']) do |bank_account|
|
77
|
+
expect( bank_account['alias']).to match 'Cuenta principal'
|
78
|
+
end
|
79
|
+
|
80
|
+
@bank_accounts.delete(customer['id'],bank['id'])
|
81
|
+
@customers.delete(customer['id'])
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
describe '.list' do
|
90
|
+
|
91
|
+
it 'should list the bank accounts using a given filter' do
|
92
|
+
pending
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
describe '.all' do
|
99
|
+
|
100
|
+
it 'list all bank accounts for a given customer' do
|
101
|
+
|
102
|
+
customer_hash= FactoryGirl.build(:customer)
|
103
|
+
customer=@customers.create(customer_hash)
|
104
|
+
expect(@bank_accounts.all(customer['id']).size).to be 0
|
105
|
+
|
106
|
+
account_hash=FactoryGirl.build(:bank_account)
|
107
|
+
bank=@bank_accounts.create(account_hash,customer['id'])
|
108
|
+
expect(@bank_accounts.all(customer['id']).size).to be 1
|
109
|
+
|
110
|
+
@bank_accounts.delete(customer['id'],bank['id'])
|
111
|
+
@customers.delete(customer['id'])
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'fails to list all bank accounts when a non existing customer is given' do
|
116
|
+
expect { @bank_accounts.all('11111') }.to raise_exception(OpenpayTransactionException)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
describe '.delete' do
|
123
|
+
|
124
|
+
it 'deletes a given bank account' do
|
125
|
+
|
126
|
+
customer_hash= FactoryGirl.build(:customer)
|
127
|
+
customer=@customers.create(customer_hash)
|
128
|
+
expect(@bank_accounts.all(customer['id']).size).to be 0
|
129
|
+
|
130
|
+
|
131
|
+
account_hash=FactoryGirl.build(:bank_account)
|
132
|
+
bank=@bank_accounts.create(account_hash,customer['id'])
|
133
|
+
expect(@bank_accounts.all(customer['id']).size).to be 1
|
134
|
+
|
135
|
+
@bank_accounts.delete(customer['id'],bank['id'])
|
136
|
+
@customers.delete(customer['id'])
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
it 'fails to delete a non existing bank accounts' do
|
142
|
+
expect { @customers.delete('1111') }.to raise_exception OpenpayTransactionException
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
describe '.delete_all' do
|
149
|
+
|
150
|
+
it 'deletes all bank accounts' do
|
151
|
+
|
152
|
+
customer_hash= FactoryGirl.build(:customer)
|
153
|
+
customer=@customers.create(customer_hash)
|
154
|
+
|
155
|
+
account_hash=FactoryGirl.build(:bank_account)
|
156
|
+
|
157
|
+
@bank_accounts.create(account_hash,customer['id'])
|
158
|
+
expect(@bank_accounts.all(customer['id']).size).to be 1
|
159
|
+
|
160
|
+
@bank_accounts.delete_all(customer['id'])
|
161
|
+
expect(@bank_accounts.all(customer['id']).size).to be 0
|
162
|
+
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'fails to deletes all bank accounts when used on PROD' do
|
167
|
+
|
168
|
+
@openpayprod=OpenpayApi.new(@merchant_id, @private_key, true)
|
169
|
+
bank_accounts=@openpayprod.create(:bankaccounts)
|
170
|
+
|
171
|
+
expect { bank_accounts.delete_all('111111') }.to raise_exception OpenpayException
|
172
|
+
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
end
|