openpay 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/.rakeTasks +1 -1
  3. data/.idea/OpenPay.iml +63 -28
  4. data/.idea/workspace.xml +1501 -0
  5. data/Gemfile +7 -2
  6. data/README.md +6 -15
  7. data/Rakefile +10 -7
  8. data/lib/OpenPay/Cards.rb +1 -2
  9. data/lib/OpenPay/Customers.rb +0 -1
  10. data/lib/OpenPay/Plans.rb +0 -5
  11. data/lib/OpenPay/bankaccounts.rb +0 -7
  12. data/lib/OpenPay/errors/openpay_exception_factory.rb +2 -0
  13. data/lib/OpenPay/open_pay_resource.rb +3 -3
  14. data/lib/OpenPay/openpay_api.rb +3 -6
  15. data/lib/openpay/bankaccounts.rb +59 -0
  16. data/lib/openpay/cards.rb +75 -0
  17. data/lib/openpay/charges.rb +77 -0
  18. data/lib/openpay/customers.rb +194 -0
  19. data/lib/openpay/errors/openpay_connection_exception.rb +3 -0
  20. data/lib/openpay/errors/openpay_exception.rb +29 -0
  21. data/lib/openpay/errors/openpay_exception_factory.rb +60 -0
  22. data/lib/openpay/errors/openpay_transaction_exception.rb +5 -0
  23. data/lib/openpay/fees.rb +5 -0
  24. data/lib/openpay/open_pay_resource.rb +242 -0
  25. data/lib/openpay/open_pay_resource_factory.rb +10 -0
  26. data/lib/openpay/openpay_api.rb +60 -0
  27. data/lib/openpay/payouts.rb +59 -0
  28. data/lib/openpay/plans.rb +23 -0
  29. data/lib/openpay/subscriptions.rb +58 -0
  30. data/lib/openpay/transfers.rb +43 -0
  31. data/lib/openpay.rb +5 -5
  32. data/lib/version.rb +2 -2
  33. data/openpay.gemspec +17 -5
  34. data/test/spec/bankaccounts_spec.rb +1 -2
  35. data/test/spec/cards_spec.rb +1 -1
  36. data/test/spec/charges_spec.rb +1 -2
  37. data/test/spec/customers_spec.rb +2 -2
  38. data/test/spec/exceptions_spec.rb +1 -1
  39. data/test/spec/fees_spec.rb +2 -2
  40. data/test/spec/openpayresource_spec.rb +1 -1
  41. data/test/spec/payouts_spec.rb +1 -1
  42. data/test/spec/plans_spec.rb +1 -1
  43. data/test/spec/subscriptions_spec.rb +1 -1
  44. data/test/spec/transfers_spec.rb +1 -1
  45. data/test/spec_helper.rb +7 -2
  46. metadata +91 -3
@@ -0,0 +1,60 @@
1
+ class OpenpayExceptionFactory
2
+
3
+
4
+
5
+ def OpenpayExceptionFactory::create(exception)
6
+
7
+ LOG.warn("An exception has been raised (original exception class: #{exception.class})")
8
+ LOG.warn("An exception has been raised (original exception class: #{exception.message })")
9
+
10
+
11
+ case exception
12
+
13
+ #resource not found
14
+ #malformed jason, invalid data, invalid request
15
+ when RestClient::BadRequest , RestClient::ResourceNotFound ,
16
+ RestClient::Conflict , RestClient::PaymentRequired ,
17
+ RestClient::UnprocessableEntity
18
+
19
+ oe=OpenpayTransactionException.new exception.http_body
20
+ LOG.warn "-OpenpayTransactionException: #{exception.http_body}"
21
+ @errors=true
22
+ raise oe
23
+
24
+
25
+
26
+ #connection, timeouts, network related errors
27
+ when Errno::EADDRINUSE , Errno::ETIMEDOUT ,OpenSSL::SSL::SSLError
28
+ #since this execeptions are not based on the rest api exeptions
29
+ #we do not have the json message so we just build the exception
30
+ #with the original exception message set in e.description and e.message
31
+ oe=OpenpayConnectionException.new(exception.message,false)
32
+ LOG.warn exception.message
33
+ @errors=true
34
+ raise oe
35
+ #badgateway-connection error, invalid credentials
36
+ when RestClient::BadGateway, RestClient::Unauthorized
37
+ oe=OpenpayConnectionException.new exception.http_body
38
+ LOG.warn exception.http_body
39
+ @errors=true
40
+ raise oe
41
+
42
+ when RestClient::Exception , RestClient::InternalServerError
43
+ oe=OpenpayException.new exception.http_body
44
+ LOG.warn exception.http_body
45
+ @errors=true
46
+ raise oe
47
+ else
48
+ #We won't hide unknown exceptions , those should be raised
49
+ LOG.warn exception.message
50
+ raise exception
51
+ end
52
+ end
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ end
@@ -0,0 +1,5 @@
1
+ require 'openpay/errors/openpay_exception'
2
+
3
+ class OpenpayTransactionException < OpenpayException
4
+
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'open_pay_resource'
2
+
3
+ class Fees < OpenPayResource
4
+
5
+ end
@@ -0,0 +1,242 @@
1
+
2
+
3
+ #This class is the abstract base class for other Openpay resources
4
+ #This class defines the basic rest verbs making use of the rest-api gem.
5
+ #Method aliases are created to provide friendly names.
6
+ class OpenPayResource
7
+
8
+ attr_accessor :api_hook
9
+
10
+ def initialize(merchant_id,private_key,production=false)
11
+ @merchant_id=merchant_id
12
+ @private_key=private_key
13
+ #assigns base url depending the requested env
14
+ @base_url=OpenpayApi::base_url(production)
15
+ @errors=false
16
+ @production=production
17
+ @timeout=90
18
+ #created resources should have a hook with the base class to keep control of created resources
19
+ @api_hook=nil
20
+ end
21
+
22
+
23
+ #returns the env set
24
+ def env
25
+ if @production
26
+ :production
27
+ else
28
+ :test
29
+ end
30
+ end
31
+
32
+ #errors on last call
33
+ def errors?
34
+ @errors
35
+ end
36
+
37
+
38
+ def list(args='')
39
+ @base_url+ "#{@merchant_id}/"+ self.class.name.to_s.downcase+"/"+args
40
+ end
41
+
42
+ def each
43
+ all.each do |line|
44
+ yield line
45
+ end
46
+ end
47
+
48
+ def delete_all
49
+
50
+ if env == :production
51
+ raise OpenpayException.new('delete_all method cannot be used on production',false)
52
+ end
53
+
54
+ each do |res|
55
+ self.delete(res['id'])
56
+ end
57
+
58
+ end
59
+
60
+ def get(args='')
61
+
62
+ @errors=false
63
+
64
+ LOG.debug("#{self.class.name.downcase}:")
65
+ LOG.debug(" GET Resource URL:#{url(args)}")
66
+ res=RestClient::Request.new(
67
+ :method => :get,
68
+ :url => url(args),
69
+ :user => @private_key,
70
+ :timeout => @timeout,
71
+ :headers => {:accept => :json,
72
+ :content_type => :json,
73
+ :user_agent => 'Openpay/v1 Ruby-API',
74
+ }
75
+ )
76
+ json_out=nil
77
+ begin
78
+ json_out=res.execute
79
+ #exceptions
80
+ rescue Exception => e
81
+ @errors=true
82
+ #will raise the appropriate exception and return
83
+ OpenpayExceptionFactory::create(e)
84
+ end
85
+
86
+ JSON[json_out]
87
+
88
+ end
89
+
90
+ def delete(args)
91
+
92
+ @errors=false
93
+
94
+ LOG.debug("#{self.class.name.downcase}:")
95
+ LOG.debug(" DELETE URL:#{url(args)}")
96
+
97
+ res=''
98
+ req=RestClient::Request.new(
99
+ :method => :delete,
100
+ :url => url(args),
101
+ :user => @private_key,
102
+ :timeout => @timeout,
103
+ :headers => {:accept => :json,
104
+ :content_type => :json,
105
+ :user_agent => 'Openpay/v1 Ruby-API',
106
+ }
107
+ )
108
+
109
+ begin
110
+ res=req.execute
111
+ #exceptions
112
+ rescue Exception => e
113
+ @errors=true
114
+ #will raise the appropriate exception and return
115
+ OpenpayExceptionFactory::create(e)
116
+ end
117
+
118
+ #returns a hash
119
+ JSON[res] if not res.empty?
120
+
121
+ end
122
+
123
+ def post(message,args='')
124
+
125
+ return_hash=false
126
+ @errors=false
127
+
128
+ if message.is_a?(Hash)
129
+ return_hash=true
130
+ json= hash2json message
131
+ else
132
+ json=message
133
+ return_hash=false
134
+ end
135
+
136
+ LOG.debug("#{self.class.name.downcase}:")
137
+ LOG.debug " POST URL:#{url(args)}"
138
+ #For security reasons we keep it hide
139
+ #LOG.debug " json: #{json}"
140
+
141
+ begin
142
+
143
+ #request
144
+ res= RestClient::Request.new(
145
+ :method => :post,
146
+ :url => url(args) ,
147
+ :user => @private_key,
148
+ :timeout => @timeout,
149
+ :payload => json,
150
+ :headers => {:accept => :json,
151
+ :content_type => :json,
152
+ :user_agent => 'Openpay/v1 Ruby-API',
153
+ :json => json}
154
+ ) .execute
155
+
156
+ #exceptions
157
+ rescue Exception => e
158
+ @errors=true
159
+ #will raise the appropriate exception and return
160
+ OpenpayExceptionFactory::create(e)
161
+ end
162
+
163
+ #return
164
+ if return_hash
165
+ JSON.parse res
166
+ else
167
+ res
168
+ end
169
+
170
+ end
171
+
172
+
173
+
174
+ def put (message,args='')
175
+
176
+ return_hash=false
177
+
178
+ if message.is_a?(Hash)
179
+ return_hash=true
180
+ json= hash2json message
181
+ else
182
+ json=message
183
+ return_hash=false
184
+ end
185
+
186
+
187
+ LOG.info "PUT URL:#{url}"
188
+ #LOG.info " json: #{json}"
189
+
190
+ begin
191
+ res= RestClient::Request.new(
192
+ :method => :put,
193
+ :url => url(args),
194
+ :user => @private_key,
195
+ :timeout => @timeout,
196
+ :payload => json,
197
+ :headers => {:accept => :json,
198
+ :content_type => :json,
199
+ :user_agent => 'Openpay/v1 Ruby-API',
200
+ :json => json}
201
+ ) .execute
202
+ rescue RestClient::BadRequest => e
203
+ warn e.http_body
204
+ @errors=true
205
+ return JSON.parse e.http_body
206
+ end
207
+
208
+
209
+ if return_hash
210
+ JSON.parse res
211
+ else
212
+ res
213
+ end
214
+
215
+ end
216
+
217
+
218
+ #aliases for rest verbs
219
+ alias_method :all, :get
220
+ alias_method :list, :get
221
+ alias_method :update, :put
222
+ alias_method :create , :post
223
+
224
+
225
+ def hash2json(jash)
226
+ JSON.generate(jash)
227
+ end
228
+
229
+ def json2hash(json)
230
+ JSON[json]
231
+ end
232
+
233
+
234
+ private
235
+ def url(args='')
236
+ @base_url+"#{@merchant_id}/"+ self.class.name.to_s.downcase+ '/'+args
237
+ end
238
+
239
+
240
+
241
+
242
+ end
@@ -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,60 @@
1
+ require 'logger'
2
+ require 'base64'
3
+ require 'rest-client'
4
+ require 'uri'
5
+
6
+ require 'openpay/open_pay_resource_factory'
7
+ require 'errors/openpay_exception'
8
+
9
+ LOG= Logger.new(STDOUT)
10
+ #change to Logger::DEBUG if need trace information
11
+ #due the nature of the information, we recommend to never use a log file when in debug
12
+ LOG.level=Logger::ERROR
13
+
14
+
15
+ class OpenpayApi
16
+
17
+ #API Endpoints
18
+ API_DEV='https://sandbox-api.openpay.mx/v1/'
19
+ API_PROD='https://api.openpay.mx'
20
+
21
+ #by default the testing environment is used
22
+ def initialize(merchant_id, private_key,production=false)
23
+ @merchant_id=merchant_id
24
+ @private_key=private_key
25
+ @production=production
26
+ end
27
+
28
+
29
+ # @return [nil]
30
+ def create(resource)
31
+ klass=OpenPayResourceFactory::create(resource, @merchant_id,@private_key,@production)
32
+ #attach api hook to be able to refere to same API instance from created resources
33
+ #TODO we may move it to the initialize method
34
+ klass.api_hook=self
35
+ klass
36
+ end
37
+
38
+
39
+ def OpenpayApi::base_url(production)
40
+ if production
41
+ API_PROD
42
+ else
43
+ API_DEV
44
+ end
45
+ end
46
+
47
+
48
+ def env
49
+ if @production
50
+ :production
51
+ else
52
+ :test
53
+ end
54
+ end
55
+
56
+
57
+
58
+
59
+
60
+ end
@@ -0,0 +1,59 @@
1
+ require 'open_pay_resource'
2
+
3
+
4
+
5
+ class Payouts < OpenPayResource
6
+
7
+
8
+
9
+
10
+
11
+ def all(customer_id=nil)
12
+ if customer_id
13
+ customers=@api_hook.create(:customers)
14
+ customers.all_payouts(customer_id)
15
+ else
16
+ super ''
17
+ end
18
+ end
19
+
20
+ def get(payout='', customer_id=nil)
21
+ if customer_id
22
+ customers=@api_hook.create(:customers)
23
+ customers.get_payout(customer_id, payout)
24
+ else
25
+ super payout
26
+ end
27
+ end
28
+
29
+
30
+ def each(customer_id=nil)
31
+ if customer_id
32
+ customers=@api_hook.create(:customers)
33
+ customers.each_payout(customer_id) do |cust|
34
+ yield cust
35
+ end
36
+ else
37
+ all.each do |cust|
38
+ yield cust
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+
47
+ def create(payout, customer_id=nil)
48
+ if customer_id
49
+ customers=@api_hook.create(:customers)
50
+ customers.create_payout(customer_id, payout)
51
+ else
52
+ super payout
53
+ end
54
+ end
55
+
56
+
57
+
58
+
59
+ end
@@ -0,0 +1,23 @@
1
+ require 'open_pay_resource'
2
+
3
+ class Plans < OpenPayResource
4
+
5
+ def update(plan,plan_id)
6
+ put(plan, "#{plan_id}")
7
+ end
8
+
9
+ def each_subscription(plan_id)
10
+ get("#{plan_id}/subscriptions")
11
+ end
12
+
13
+
14
+ def all_subscriptions(plan_id)
15
+ get("#{plan_id}/subscriptions")
16
+ end
17
+
18
+
19
+ def each
20
+ end
21
+
22
+
23
+ end
@@ -0,0 +1,58 @@
1
+ require 'open_pay_resource'
2
+
3
+ class Subscriptions < OpenPayResource
4
+
5
+
6
+
7
+ def create(subscription,plan_id)
8
+ customers=@api_hook.create(:customers)
9
+ customers.create_subscription(subscription,plan_id)
10
+ end
11
+
12
+
13
+ def delete(subscription_id,customer_id)
14
+ customers=@api_hook.create(:customers)
15
+ customers.delete_subscription(customer_id, subscription_id)
16
+ end
17
+
18
+
19
+ def get(subscription_id,customer_id)
20
+
21
+ customers=@api_hook.create(:customers)
22
+ customers.get_subscription(customer_id, subscription_id)
23
+
24
+ end
25
+
26
+
27
+ def all(customer_id)
28
+
29
+ customers=@api_hook.create(:customers)
30
+ customers.all_subscriptions(customer_id)
31
+
32
+ end
33
+
34
+
35
+ def each(customer_id)
36
+ customers=@api_hook.create(:customers)
37
+ customers.each_subscription(customer_id) do |c|
38
+ yield c
39
+ end
40
+ end
41
+
42
+
43
+ def delete_all(customer_id)
44
+ customers=@api_hook.create(:customers)
45
+ customers.delete_all_subscriptions(customer_id)
46
+
47
+ end
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+ end
@@ -0,0 +1,43 @@
1
+ require 'open_pay_resource'
2
+
3
+ class Transfers < OpenPayResource
4
+
5
+
6
+ def create(transfer,customer_id)
7
+
8
+ customers=@api_hook.create(:customers)
9
+ customers.create_transfer(customer_id,transfer)
10
+
11
+ end
12
+
13
+ def all(customer_id)
14
+ customers=@api_hook.create(:customers)
15
+ customers.all_transfers(customer_id)
16
+ end
17
+
18
+
19
+
20
+ def get(transfer,customer_id)
21
+
22
+ customers=@api_hook.create(:customers)
23
+ customers.get_transfer(customer_id,transfer)
24
+
25
+ end
26
+
27
+
28
+ def each(customer_id)
29
+ customers=@api_hook.create(:customers)
30
+ customers.each_transfer(customer_id) do |tran|
31
+ yield tran
32
+ end
33
+
34
+ end
35
+
36
+
37
+
38
+
39
+
40
+ end
41
+
42
+
43
+
data/lib/openpay.rb CHANGED
@@ -8,7 +8,7 @@ require 'json'
8
8
  module Openpay
9
9
 
10
10
  #api setup / constants
11
- require 'openpay/openpay_api'
11
+ require 'openpay/openpay_api'
12
12
 
13
13
  #base class
14
14
  require 'openpay/open_pay_resource'
@@ -26,9 +26,9 @@ module Openpay
26
26
  require 'openpay/charges'
27
27
 
28
28
  #exceptions
29
- require 'openpay/errors/openpay_exception_factory'
30
- require 'openpay/errors/openpay_exception'
31
- require 'openpay/errors/openpay_transaction_exception'
32
- require 'openpay/errors/openpay_connection_exception'
29
+ require 'errors/openpay_exception_factory'
30
+ require 'errors/openpay_exception'
31
+ require 'errors/openpay_transaction_exception'
32
+ require 'errors/openpay_connection_exception'
33
33
 
34
34
  end
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Openpay
2
2
 
3
3
  # Dec 31 2014, 6:00 PM Qro Local time.
4
- # 97 test cases passed / 275.232 secs
5
- VERSION = '1.0.1'
4
+ # 97 test cases passed / 220.232 secs
5
+ VERSION = '1.0.2'
6
6
  end
data/openpay.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.version = Openpay::VERSION
11
11
  spec.authors = ["ronnie_bermejo"]
12
12
  spec.email = ["ronnie.bermejo.mx@gmail.com"]
13
- spec.description = %q{ruby client for Openpay API services (version 1.0.1)}
13
+ spec.description = %q{ruby client for Openpay API services (version 1.0.2)}
14
14
  spec.summary = %q{ruby api for openpay resources}
15
15
  spec.homepage = "http://openpay.mx/"
16
16
  spec.license = "Apache"
@@ -18,9 +18,21 @@ Gem::Specification.new do |spec|
18
18
  spec.files = `git ls-files`.split($/)
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec)/})
21
- spec.require_paths = ["lib","lib/openpay",'openpay']
21
+ spec.require_paths = ['lib','lib/openpay','openpay','.']
22
+
23
+ spec.add_runtime_dependency 'rest-client' , '~>1.6.7'
24
+ spec.add_runtime_dependency 'json'
25
+
26
+
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.3'
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'json_spec'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'factory_girl' , '4.2.0'
33
+ spec.post_install_message = 'Thanks for installing openpay. Enjoy !'
34
+
35
+
36
+
22
37
 
23
- spec.add_development_dependency "bundler", "~> 1.3"
24
- spec.add_development_dependency "rake"
25
- spec.post_install_message = "Thanks for installing openpay. Enjoy !"
26
38
  end
@@ -1,4 +1,4 @@
1
- require './test/spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
 
4
4
  describe Bankaccounts do
@@ -19,7 +19,6 @@ describe Bankaccounts do
19
19
 
20
20
  after(:all) do
21
21
  @customers.delete_all
22
- @bank_accounts.delete_all
23
22
  end
24
23
 
25
24
  describe '.create' do
@@ -1,4 +1,4 @@
1
- require './test/spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
 
4
4
  describe Cards do
@@ -1,5 +1,4 @@
1
-
2
- require './test/spec_helper'
1
+ require_relative '../spec_helper'
3
2
 
4
3
  describe Charges do
5
4
 
@@ -1,4 +1,4 @@
1
- require './test/spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
 
4
4
  describe Customers do
@@ -200,7 +200,7 @@ describe Customers do
200
200
  end
201
201
 
202
202
  #performs check
203
- expect(@customers.all.size).to be 5
203
+ expect(@customers.all.size).to be > 4
204
204
  @customers.delete_all
205
205
  expect(@customers.all.size).to be 0
206
206
  end
@@ -1,4 +1,4 @@
1
- require './test/spec_helper'
1
+ require_relative '../spec_helper'
2
2
 
3
3
 
4
4
  describe 'Openpay Exceptions' do