processout 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +10 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/deploy.sh +25 -0
  12. data/lib/processout.rb +130 -0
  13. data/lib/processout/activity.rb +147 -0
  14. data/lib/processout/authorization_request.rb +248 -0
  15. data/lib/processout/card.rb +156 -0
  16. data/lib/processout/coupon.rb +284 -0
  17. data/lib/processout/customer.rb +418 -0
  18. data/lib/processout/customer_action.rb +50 -0
  19. data/lib/processout/discount.rb +217 -0
  20. data/lib/processout/errors/authentication_error.rb +7 -0
  21. data/lib/processout/errors/generic_error.rb +7 -0
  22. data/lib/processout/errors/internal_error.rb +7 -0
  23. data/lib/processout/errors/notfound_error.rb +7 -0
  24. data/lib/processout/errors/validation_error.rb +7 -0
  25. data/lib/processout/event.rb +176 -0
  26. data/lib/processout/gateway.rb +104 -0
  27. data/lib/processout/gateway_configuration.rb +91 -0
  28. data/lib/processout/invoice.rb +457 -0
  29. data/lib/processout/networking/request.rb +95 -0
  30. data/lib/processout/networking/response.rb +61 -0
  31. data/lib/processout/plan.rb +280 -0
  32. data/lib/processout/product.rb +313 -0
  33. data/lib/processout/project.rb +105 -0
  34. data/lib/processout/refund.rb +161 -0
  35. data/lib/processout/subscription.rb +633 -0
  36. data/lib/processout/token.rb +167 -0
  37. data/lib/processout/transaction.rb +302 -0
  38. data/lib/processout/version.rb +3 -0
  39. data/lib/processout/webhook.rb +154 -0
  40. data/processout.gemspec +26 -0
  41. metadata +125 -0
@@ -0,0 +1,95 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require "json"
4
+
5
+ module ProcessOut
6
+ class Request
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ # ApplyHeaders applies the pre-defined headers to the request
12
+ def apply_headers(req, options)
13
+ req.basic_auth @client.project_id, @client.project_secret
14
+ req.content_type = "application/json"
15
+ req["API-Version"] = "1.3.0.0"
16
+ req["User-Agent"] = "utf-8"
17
+
18
+ unless options.nil?
19
+ req["Idempotency-Key"] = options.fetch(:idempotency_key, "")
20
+ req["Disable-Logging"] = options.fetch(:disable_logging, "")
21
+ end
22
+ end
23
+ protected :apply_headers
24
+
25
+ # ComputeData computes the data to be sent in the request
26
+ def compute_data(data, options)
27
+ unless options.nil?
28
+ data["expand"] = options.fetch(:expand, "")
29
+ data["filter"] = options.fetch(:filter, "")
30
+ data["limit"] = options.fetch(:limit, "")
31
+ data["page"] = options.fetch(:page, "")
32
+ data["end_before"] = options.fetch(:end_before, "")
33
+ data["start_after"] = options.fetch(:start_after, "")
34
+ end
35
+ data
36
+ end
37
+ protected :compute_data
38
+
39
+ # GET sends a get request to the API
40
+ def get(path, data, options)
41
+ uri = URI(@client.host + path)
42
+ uri.query = URI.encode_www_form(self.compute_data(data, options))
43
+ req = Net::HTTP::Get.new(uri)
44
+ self.apply_headers(req, options)
45
+
46
+ Net::HTTP.start(uri.hostname, uri.port,
47
+ :use_ssl => true) do |http|
48
+
49
+ http.request(req)
50
+ end
51
+ end
52
+
53
+ # POST sends a post request to the API
54
+ def post(path, data, options)
55
+ uri = URI(@client.host + path)
56
+ req = Net::HTTP::Post.new(uri)
57
+ req.body = data.to_json
58
+ self.apply_headers(req, options)
59
+
60
+ Net::HTTP.start(uri.hostname, uri.port,
61
+ :use_ssl => true) do |http|
62
+
63
+ http.request(req)
64
+ end
65
+ end
66
+
67
+ # PUT sends a put request to the API
68
+ def put(path, data, options)
69
+ uri = URI(@client.host + path)
70
+ req = Net::HTTP::Put.new(uri)
71
+ req.body = data.to_json
72
+ self.apply_headers(req, options)
73
+
74
+ Net::HTTP.start(uri.hostname, uri.port,
75
+ :use_ssl => true) do |http|
76
+
77
+ http.request(req)
78
+ end
79
+ end
80
+
81
+ # DELETE sends a delete request to the API
82
+ def delete(path, data, options)
83
+ uri = URI(@client.host + path)
84
+ req = Net::HTTP::Delete.new(uri)
85
+ req.body = data.to_json
86
+ self.apply_headers(req, options)
87
+
88
+ Net::HTTP.start(uri.hostname, uri.port,
89
+ :use_ssl => true) do |http|
90
+
91
+ http.request(req)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,61 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ require "processout/errors/authentication_error"
5
+ require "processout/errors/generic_error"
6
+ require "processout/errors/internal_error"
7
+ require "processout/errors/notfound_error"
8
+ require "processout/errors/validation_error"
9
+
10
+ module ProcessOut
11
+ class Response
12
+ attr_reader :body
13
+
14
+ def initialize(resp)
15
+ @resp = resp
16
+ @status = resp.code.to_i
17
+ @body = JSON.parse(resp.body)
18
+ self.check
19
+ end
20
+
21
+ # Success returns whether or not the response returned a successful message
22
+ def success
23
+ if @body.include? "success"
24
+ return @body["success"]
25
+ end
26
+ false
27
+ end
28
+
29
+ # Message returns the error message contained in the response, if any
30
+ def message
31
+ if @body.include? "message"
32
+ return @body["message"]
33
+ end
34
+ ""
35
+ end
36
+
37
+ # Check checks the response didn't contain any error, or raises an
38
+ # error if one was found
39
+ def check
40
+ unless self.success
41
+ if @status == 404
42
+ raise NotFoundError, self.message
43
+ end
44
+ if @status == 401
45
+ raise AuthenticationError, self.message
46
+ end
47
+ if @status == 400
48
+ raise ValidationError, self.message
49
+ end
50
+ if @status >= 500
51
+ raise InternalError, "ProcessOut returned an internal error (" +
52
+ @status.to_s + "): " + self.message
53
+ end
54
+
55
+ raise GenericError, "ProcessOut returned an error (" +
56
+ @status.to_s + "): " + self.message
57
+ end
58
+ end
59
+ protected :check
60
+ end
61
+ end
@@ -0,0 +1,280 @@
1
+ # The content of this file was automatically generated
2
+
3
+ require "cgi"
4
+ require "processout/networking/request"
5
+ require "processout/networking/response"
6
+
7
+ module ProcessOut
8
+ class Plan
9
+
10
+ attr_reader :id
11
+ attr_reader :project
12
+ attr_reader :name
13
+ attr_reader :amount
14
+ attr_reader :currency
15
+ attr_reader :metadata
16
+ attr_reader :interval
17
+ attr_reader :trial_period
18
+ attr_reader :return_url
19
+ attr_reader :cancel_url
20
+ attr_reader :sandbox
21
+ attr_reader :created_at
22
+
23
+
24
+ def id=(val)
25
+ @id = val
26
+ end
27
+
28
+ def project=(val)
29
+ if val.instance_of? Project
30
+ @project = val
31
+ else
32
+ obj = Project.new(@client)
33
+ obj.fill_with_data(val)
34
+ @project = obj
35
+ end
36
+
37
+ end
38
+
39
+ def name=(val)
40
+ @name = val
41
+ end
42
+
43
+ def amount=(val)
44
+ @amount = val
45
+ end
46
+
47
+ def currency=(val)
48
+ @currency = val
49
+ end
50
+
51
+ def metadata=(val)
52
+ @metadata = val
53
+ end
54
+
55
+ def interval=(val)
56
+ @interval = val
57
+ end
58
+
59
+ def trial_period=(val)
60
+ @trial_period = val
61
+ end
62
+
63
+ def return_url=(val)
64
+ @return_url = val
65
+ end
66
+
67
+ def cancel_url=(val)
68
+ @cancel_url = val
69
+ end
70
+
71
+ def sandbox=(val)
72
+ @sandbox = val
73
+ end
74
+
75
+ def created_at=(val)
76
+ @created_at = val
77
+ end
78
+
79
+
80
+ # Initializes the Plan object
81
+ # Params:
82
+ # +client+:: +ProcessOut+ client instance
83
+ def initialize(client)
84
+ @client = client
85
+
86
+ @id = ""
87
+ @project = nil
88
+ @name = ""
89
+ @amount = ""
90
+ @currency = ""
91
+ @metadata = Hash.new
92
+ @interval = ""
93
+ @trial_period = "0d"
94
+ @return_url = ""
95
+ @cancel_url = ""
96
+ @sandbox = false
97
+ @created_at = ""
98
+
99
+ end
100
+
101
+ # Fills the object with data coming from the API
102
+ # Params:
103
+ # +data+:: +Hash+ of data coming from the API
104
+ def fill_with_data(data)
105
+ if data.include? "id"
106
+ @id = data["id"]
107
+ end
108
+ if data.include? "project"
109
+ @project = data["project"]
110
+ end
111
+ if data.include? "name"
112
+ @name = data["name"]
113
+ end
114
+ if data.include? "amount"
115
+ @amount = data["amount"]
116
+ end
117
+ if data.include? "currency"
118
+ @currency = data["currency"]
119
+ end
120
+ if data.include? "metadata"
121
+ @metadata = data["metadata"]
122
+ end
123
+ if data.include? "interval"
124
+ @interval = data["interval"]
125
+ end
126
+ if data.include? "trial_period"
127
+ @trial_period = data["trial_period"]
128
+ end
129
+ if data.include? "return_url"
130
+ @return_url = data["return_url"]
131
+ end
132
+ if data.include? "cancel_url"
133
+ @cancel_url = data["cancel_url"]
134
+ end
135
+ if data.include? "sandbox"
136
+ @sandbox = data["sandbox"]
137
+ end
138
+ if data.include? "created_at"
139
+ @created_at = data["created_at"]
140
+ end
141
+
142
+ self
143
+ end
144
+
145
+ # Get all the plans.
146
+ # Params:
147
+ # +options+:: +Hash+ of options
148
+ def all(options = nil)
149
+ request = Request.new(@client)
150
+ path = "/plans"
151
+ data = {
152
+
153
+ }
154
+
155
+ response = Response.new(request.get(path, data, options))
156
+ return_values = Array.new
157
+
158
+ a = Array.new
159
+ body = response.body
160
+ for v in body['plans']
161
+ tmp = Plan(@client)
162
+ tmp.fill_with_data(v)
163
+ a.push(tmp)
164
+ end
165
+
166
+ return_values.push(a)
167
+
168
+
169
+
170
+ return_values[0]
171
+ end
172
+
173
+ # Create a new plan.
174
+ # Params:
175
+ # +options+:: +Hash+ of options
176
+ def create(options = nil)
177
+ request = Request.new(@client)
178
+ path = "/plans"
179
+ data = {
180
+ "id": @id,
181
+ "name": @name,
182
+ "amount": @amount,
183
+ "currency": @currency,
184
+ "interval": @interval,
185
+ "trial_period": @trial_period,
186
+ "metadata": @metadata,
187
+ "return_url": @return_url,
188
+ "cancel_url": @cancel_url
189
+ }
190
+
191
+ response = Response.new(request.post(path, data, options))
192
+ return_values = Array.new
193
+
194
+ body = response.body
195
+ body = body["plan"]
196
+
197
+
198
+ return_values.push(self.fill_with_data(body))
199
+
200
+
201
+
202
+ return_values[0]
203
+ end
204
+
205
+ # Find a plan by its ID.
206
+ # Params:
207
+ # +plan_id+:: ID of the plan
208
+ # +options+:: +Hash+ of options
209
+ def find(plan_id, options = nil)
210
+ request = Request.new(@client)
211
+ path = "/plans/" + CGI.escape(plan_id) + ""
212
+ data = {
213
+
214
+ }
215
+
216
+ response = Response.new(request.get(path, data, options))
217
+ return_values = Array.new
218
+
219
+ body = response.body
220
+ body = body["plan"]
221
+
222
+
223
+ obj = Plan.new(@client)
224
+ return_values.push(obj.fill_with_data(body))
225
+
226
+
227
+
228
+ return_values[0]
229
+ end
230
+
231
+ # Update the plan. This action won't affect subscriptions already linked to this plan.
232
+ # Params:
233
+ # +options+:: +Hash+ of options
234
+ def update(options = nil)
235
+ request = Request.new(@client)
236
+ path = "/plans/" + CGI.escape(@id) + ""
237
+ data = {
238
+ "name": @name,
239
+ "trial_period": @trial_period,
240
+ "metadata": @metadata,
241
+ "return_url": @return_url,
242
+ "cancel_url": @cancel_url
243
+ }
244
+
245
+ response = Response.new(request.put(path, data, options))
246
+ return_values = Array.new
247
+
248
+ body = response.body
249
+ body = body["plan"]
250
+
251
+
252
+ return_values.push(self.fill_with_data(body))
253
+
254
+
255
+
256
+ return_values[0]
257
+ end
258
+
259
+ # Delete a plan. Subscriptions linked to this plan won't be affected.
260
+ # Params:
261
+ # +options+:: +Hash+ of options
262
+ def end(options = nil)
263
+ request = Request.new(@client)
264
+ path = "/plans/" + CGI.escape(@id) + ""
265
+ data = {
266
+
267
+ }
268
+
269
+ response = Response.new(request.delete(path, data, options))
270
+ return_values = Array.new
271
+
272
+ return_values.push(response.success)
273
+
274
+
275
+ return_values[0]
276
+ end
277
+
278
+
279
+ end
280
+ end
@@ -0,0 +1,313 @@
1
+ # The content of this file was automatically generated
2
+
3
+ require "cgi"
4
+ require "processout/networking/request"
5
+ require "processout/networking/response"
6
+
7
+ module ProcessOut
8
+ class Product
9
+
10
+ attr_reader :id
11
+ attr_reader :project
12
+ attr_reader :url
13
+ attr_reader :name
14
+ attr_reader :amount
15
+ attr_reader :currency
16
+ attr_reader :metadata
17
+ attr_reader :request_email
18
+ attr_reader :request_shipping
19
+ attr_reader :return_url
20
+ attr_reader :cancel_url
21
+ attr_reader :sandbox
22
+ attr_reader :created_at
23
+
24
+
25
+ def id=(val)
26
+ @id = val
27
+ end
28
+
29
+ def project=(val)
30
+ if val.instance_of? Project
31
+ @project = val
32
+ else
33
+ obj = Project.new(@client)
34
+ obj.fill_with_data(val)
35
+ @project = obj
36
+ end
37
+
38
+ end
39
+
40
+ def url=(val)
41
+ @url = val
42
+ end
43
+
44
+ def name=(val)
45
+ @name = val
46
+ end
47
+
48
+ def amount=(val)
49
+ @amount = val
50
+ end
51
+
52
+ def currency=(val)
53
+ @currency = val
54
+ end
55
+
56
+ def metadata=(val)
57
+ @metadata = val
58
+ end
59
+
60
+ def request_email=(val)
61
+ @request_email = val
62
+ end
63
+
64
+ def request_shipping=(val)
65
+ @request_shipping = val
66
+ end
67
+
68
+ def return_url=(val)
69
+ @return_url = val
70
+ end
71
+
72
+ def cancel_url=(val)
73
+ @cancel_url = val
74
+ end
75
+
76
+ def sandbox=(val)
77
+ @sandbox = val
78
+ end
79
+
80
+ def created_at=(val)
81
+ @created_at = val
82
+ end
83
+
84
+
85
+ # Initializes the Product object
86
+ # Params:
87
+ # +client+:: +ProcessOut+ client instance
88
+ def initialize(client)
89
+ @client = client
90
+
91
+ @id = ""
92
+ @project = nil
93
+ @url = ""
94
+ @name = ""
95
+ @amount = ""
96
+ @currency = ""
97
+ @metadata = Hash.new
98
+ @request_email = false
99
+ @request_shipping = false
100
+ @return_url = ""
101
+ @cancel_url = ""
102
+ @sandbox = false
103
+ @created_at = ""
104
+
105
+ end
106
+
107
+ # Fills the object with data coming from the API
108
+ # Params:
109
+ # +data+:: +Hash+ of data coming from the API
110
+ def fill_with_data(data)
111
+ if data.include? "id"
112
+ @id = data["id"]
113
+ end
114
+ if data.include? "project"
115
+ @project = data["project"]
116
+ end
117
+ if data.include? "url"
118
+ @url = data["url"]
119
+ end
120
+ if data.include? "name"
121
+ @name = data["name"]
122
+ end
123
+ if data.include? "amount"
124
+ @amount = data["amount"]
125
+ end
126
+ if data.include? "currency"
127
+ @currency = data["currency"]
128
+ end
129
+ if data.include? "metadata"
130
+ @metadata = data["metadata"]
131
+ end
132
+ if data.include? "request_email"
133
+ @request_email = data["request_email"]
134
+ end
135
+ if data.include? "request_shipping"
136
+ @request_shipping = data["request_shipping"]
137
+ end
138
+ if data.include? "return_url"
139
+ @return_url = data["return_url"]
140
+ end
141
+ if data.include? "cancel_url"
142
+ @cancel_url = data["cancel_url"]
143
+ end
144
+ if data.include? "sandbox"
145
+ @sandbox = data["sandbox"]
146
+ end
147
+ if data.include? "created_at"
148
+ @created_at = data["created_at"]
149
+ end
150
+
151
+ self
152
+ end
153
+
154
+ # Create a new invoice from the product.
155
+ # Params:
156
+ # +options+:: +Hash+ of options
157
+ def invoice(options = nil)
158
+ request = Request.new(@client)
159
+ path = "/products/" + CGI.escape(@id) + "/invoices"
160
+ data = {
161
+
162
+ }
163
+
164
+ response = Response.new(request.post(path, data, options))
165
+ return_values = Array.new
166
+
167
+ body = response.body
168
+ body = body["invoice"]
169
+ invoice = Invoice(self._client)
170
+ return_values.push(invoice.fill_with_data(body))
171
+
172
+
173
+ return_values[0]
174
+ end
175
+
176
+ # Get all the products.
177
+ # Params:
178
+ # +options+:: +Hash+ of options
179
+ def all(options = nil)
180
+ request = Request.new(@client)
181
+ path = "/products"
182
+ data = {
183
+
184
+ }
185
+
186
+ response = Response.new(request.get(path, data, options))
187
+ return_values = Array.new
188
+
189
+ a = Array.new
190
+ body = response.body
191
+ for v in body['products']
192
+ tmp = Product(@client)
193
+ tmp.fill_with_data(v)
194
+ a.push(tmp)
195
+ end
196
+
197
+ return_values.push(a)
198
+
199
+
200
+
201
+ return_values[0]
202
+ end
203
+
204
+ # Create a new product.
205
+ # Params:
206
+ # +options+:: +Hash+ of options
207
+ def create(options = nil)
208
+ request = Request.new(@client)
209
+ path = "/products"
210
+ data = {
211
+ "name": @name,
212
+ "amount": @amount,
213
+ "currency": @currency,
214
+ "metadata": @metadata,
215
+ "request_email": @request_email,
216
+ "request_shipping": @request_shipping,
217
+ "return_url": @return_url,
218
+ "cancel_url": @cancel_url
219
+ }
220
+
221
+ response = Response.new(request.post(path, data, options))
222
+ return_values = Array.new
223
+
224
+ body = response.body
225
+ body = body["product"]
226
+
227
+
228
+ return_values.push(self.fill_with_data(body))
229
+
230
+
231
+
232
+ return_values[0]
233
+ end
234
+
235
+ # Find a product by its ID.
236
+ # Params:
237
+ # +product_id+:: ID of the product
238
+ # +options+:: +Hash+ of options
239
+ def find(product_id, options = nil)
240
+ request = Request.new(@client)
241
+ path = "/products/" + CGI.escape(product_id) + ""
242
+ data = {
243
+
244
+ }
245
+
246
+ response = Response.new(request.get(path, data, options))
247
+ return_values = Array.new
248
+
249
+ body = response.body
250
+ body = body["product"]
251
+
252
+
253
+ obj = Product.new(@client)
254
+ return_values.push(obj.fill_with_data(body))
255
+
256
+
257
+
258
+ return_values[0]
259
+ end
260
+
261
+ # Save the updated product attributes.
262
+ # Params:
263
+ # +options+:: +Hash+ of options
264
+ def save(options = nil)
265
+ request = Request.new(@client)
266
+ path = "/products/" + CGI.escape(@id) + ""
267
+ data = {
268
+ "name": @name,
269
+ "amount": @amount,
270
+ "currency": @currency,
271
+ "metadata": @metadata,
272
+ "request_email": @request_email,
273
+ "request_shipping": @request_shipping,
274
+ "return_url": @return_url,
275
+ "cancel_url": @cancel_url
276
+ }
277
+
278
+ response = Response.new(request.put(path, data, options))
279
+ return_values = Array.new
280
+
281
+ body = response.body
282
+ body = body["product"]
283
+
284
+
285
+ return_values.push(self.fill_with_data(body))
286
+
287
+
288
+
289
+ return_values[0]
290
+ end
291
+
292
+ # Delete the product.
293
+ # Params:
294
+ # +options+:: +Hash+ of options
295
+ def delete(options = nil)
296
+ request = Request.new(@client)
297
+ path = "/products/" + CGI.escape(@id) + ""
298
+ data = {
299
+
300
+ }
301
+
302
+ response = Response.new(request.delete(path, data, options))
303
+ return_values = Array.new
304
+
305
+ return_values.push(response.success)
306
+
307
+
308
+ return_values[0]
309
+ end
310
+
311
+
312
+ end
313
+ end