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,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
+
@@ -0,0 +1,66 @@
1
+ require 'open_pay_resource'
2
+
3
+
4
+
5
+ #TODO change name
6
+ class Bankaccounts < OpenPayResource
7
+
8
+
9
+ def create(bank_account,customer_id)
10
+ customers=@api_hook.create(:customers)
11
+ customers.create_bank_account(customer_id,bank_account)
12
+ end
13
+
14
+
15
+ def get(customer_id='',bank_account=nil)
16
+ customers=@api_hook.create(:customers)
17
+
18
+ if bank_account
19
+ customers.get_bank_account(customer_id,bank_account)
20
+ else
21
+ customers.get_bank_account(customer_id)
22
+ end
23
+
24
+ end
25
+
26
+
27
+ def delete(customer_id,bank_account)
28
+ customers=@api_hook.create(:customers)
29
+ customers.delete_bank_account(customer_id,bank_account)
30
+ end
31
+
32
+
33
+ def each(customer_id)
34
+ customers=@api_hook.create(:customers)
35
+ customers.each_bank_account(customer_id) do |acc|
36
+ yield acc
37
+ end
38
+
39
+ end
40
+
41
+
42
+ def all(customer_id)
43
+ customers=@api_hook.create(:customers)
44
+ customers.all_bank_accounts(customer_id)
45
+ end
46
+
47
+
48
+ def delete_all(customer_id)
49
+
50
+ if env == :production
51
+ raise OpenpayException.new('This method is not supported on PRODUCTION',false)
52
+ end
53
+
54
+ customers=@api_hook.create(:customers)
55
+ customers.delete_all_bank_accounts(customer_id)
56
+
57
+ end
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+ end
@@ -0,0 +1,51 @@
1
+ class OpenpayException < StandardError
2
+ attr_reader :description
3
+ attr_reader :http_code
4
+ attr_reader :http_body
5
+ attr_reader :json_body
6
+ attr_reader :error_code
7
+ attr_reader :category
8
+
9
+
10
+ def initialize(message=nil,json_message=true)
11
+ #openpay errors have a json error string
12
+ if json_message
13
+ json= JSON.parse message
14
+ @description = json['description']
15
+ @http_code = json['http_code']
16
+ @error_code = json['error_code']
17
+ @json_body = message
18
+ @category = json['category']
19
+ #other errors may or not have a json error string, so this case is for them
20
+ else
21
+ @description = message
22
+ @http_code = ''
23
+ @http_body = ''
24
+ @json_body = ''
25
+ @error_code = ''
26
+ end
27
+ end
28
+
29
+
30
+
31
+ =begin
32
+ rescue RestClient::BadRequest => e
33
+ oe=OpenpayTransactionException.new(e.message,e.http_code,e.http_body,e.http_body)
34
+ LOG.warn e.http_body
35
+ @errors=true
36
+ raise oe
37
+ rescue RestClient::BadGateway => e
38
+ oe=OpenpayConnectionException.new(e.message,e.http_code,e.http_body,e.http_body)
39
+ LOG.warn e.http_body
40
+ @errors=true
41
+ raise oe
42
+ rescue RestClient::Exception => e
43
+ oe=OpenpayApiError.new(e.message,e.http_code,e.http_body,e.http_body)
44
+ LOG.warn e.http_body
45
+ @errors=true
46
+ raise oe
47
+ =end
48
+
49
+
50
+
51
+ end
@@ -0,0 +1,58 @@
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
+
9
+ case exception
10
+
11
+ #resource not found
12
+ #malformed jason, invalid data, invalid request
13
+ when RestClient::BadRequest , RestClient::ResourceNotFound ,
14
+ RestClient::Conflict , RestClient::PaymentRequired ,
15
+ RestClient::UnprocessableEntity
16
+
17
+ oe=OpenpayTransactionException.new exception.http_body
18
+ LOG.warn "-OpenpayTransactionException: #{exception.http_body}"
19
+ @errors=true
20
+ raise oe
21
+
22
+
23
+
24
+ #connection, timeouts, network related errors
25
+ when Errno::EADDRINUSE , Errno::ETIMEDOUT ,OpenSSL::SSL::SSLError
26
+ #since this execeptions are not based on the rest api exeptions
27
+ #we do not have the json message so we just build the exception
28
+ #with the original exception message set in e.description and e.message
29
+ oe=OpenpayConnectionException.new(exception.message,false)
30
+ LOG.warn exception.message
31
+ @errors=true
32
+ raise oe
33
+ #badgateway-connection error, invalid credentials
34
+ when RestClient::BadGateway, RestClient::Unauthorized
35
+ oe=OpenpayConnectionException.new exception.http_body
36
+ LOG.warn exception.http_body
37
+ @errors=true
38
+ raise oe
39
+
40
+ when RestClient::Exception , RestClient::InternalServerError
41
+ oe=OpenpayException.new exception.http_body
42
+ LOG.warn exception.http_body
43
+ @errors=true
44
+ raise oe
45
+ else
46
+ #We won't hide unknown exceptions , those should be raised
47
+ LOG.warn exception.message
48
+ raise exception
49
+ end
50
+ end
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ require 'openpay/errors/open_pay_exception'
2
+ class OpenpayConnectionException < OpenpayException
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'openpay/errors/open_pay_exception'
2
+
3
+ class OpenpayTransactionException < OpenpayException
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
+ warn "deleting #{res}"
56
+ self.delete(res['id'])
57
+ end
58
+
59
+ end
60
+
61
+ def get(args='')
62
+
63
+ @errors=false
64
+
65
+ LOG.debug("#{self.class.name.downcase}:")
66
+ LOG.debug(" GET Resource URL:#{url(args)}")
67
+ res=RestClient::Request.new(
68
+ :method => :get,
69
+ :url => url(args),
70
+ :user => @private_key,
71
+ :timeout => @timeout,
72
+ :headers => {:accept => :json,
73
+ :content_type => :json,
74
+ :user_agent => 'Openpay/v1 Ruby-API',
75
+ }
76
+ )
77
+ json_out=nil
78
+ begin
79
+ json_out=res.execute
80
+ #exceptions
81
+ rescue Exception => e
82
+ @errors=true
83
+ #will raise the appropriate exception and return
84
+ OpenpayExceptionFactory::create(e)
85
+ end
86
+
87
+ JSON[json_out]
88
+
89
+ end
90
+
91
+ def delete(args)
92
+
93
+ @errors=false
94
+
95
+ LOG.debug("#{self.class.name.downcase}:")
96
+ LOG.debug(" DELETE URL:#{url(args)}")
97
+
98
+ res=''
99
+ req=RestClient::Request.new(
100
+ :method => :delete,
101
+ :url => url(args),
102
+ :user => @private_key,
103
+ :timeout => @timeout,
104
+ :headers => {:accept => :json,
105
+ :content_type => :json,
106
+ :user_agent => 'Openpay/v1 Ruby-API',
107
+ }
108
+ )
109
+
110
+ begin
111
+ res=req.execute
112
+ #exceptions
113
+ rescue Exception => e
114
+ @errors=true
115
+ #will raise the appropriate exception and return
116
+ OpenpayExceptionFactory::create(e)
117
+ end
118
+
119
+ #returns a hash
120
+ JSON[res] if not res.empty?
121
+
122
+ end
123
+
124
+ def post(message,args='')
125
+
126
+ return_hash=false
127
+ @errors=false
128
+
129
+ if message.is_a?(Hash)
130
+ return_hash=true
131
+ json= hash2json message
132
+ else
133
+ json=message
134
+ return_hash=false
135
+ end
136
+
137
+ LOG.debug("#{self.class.name.downcase}:")
138
+ LOG.debug " POST URL:#{url(args)}"
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