mc2p-ruby 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mixins.rb ADDED
@@ -0,0 +1,377 @@
1
+ module MC2P
2
+
3
+ # Basic info of the object item
4
+ class ObjectItemMixin
5
+ attr_accessor :json_dict
6
+ attr_accessor :resource
7
+ attr_accessor :_deleted
8
+
9
+ def initialize(json_dict, resource, id_property = 'id')
10
+ @json_dict = json_dict
11
+ @resource = resource
12
+ @_deleted = nil
13
+ @id_property = id_property
14
+ end
15
+
16
+ def id_required_and_not_deleted
17
+ raise BadUseError('Object don\'t have ID') unless
18
+ @json_dict.fetch(@id_property, false)
19
+ raise BadUseError('Object deleted') if @_deleted
20
+ end
21
+
22
+ # Returns: Name of the object and content
23
+ def to_s
24
+ "#{self.class.name} #{@json_dict}"
25
+ end
26
+ end
27
+
28
+ # Allows delete an object item
29
+ class DeleteObjectItemMixin < ObjectItemMixin
30
+ # Deletes the object item
31
+ def delete
32
+ id_required_and_not_deleted
33
+ @resource.delete(
34
+ @json_dict[@id_property]
35
+ )
36
+ @_deleted = true
37
+ end
38
+ end
39
+
40
+ # Allows retrieve an object item
41
+ class RetrieveObjectItemMixin < ObjectItemMixin
42
+ # Retrieves the data of the object item
43
+ def retrieve
44
+ id_required_and_not_deleted
45
+ obj = @resource.detail(
46
+ @json_dict[@id_property]
47
+ )
48
+ @json_dict = obj.json_dict
49
+ end
50
+ end
51
+
52
+ # Allows create an object item
53
+ class CreateObjectItemMixin < ObjectItemMixin
54
+ # Creates the object item with the json_dict data
55
+ def _create
56
+ obj = @resource.create(
57
+ @json_dict
58
+ )
59
+ @json_dict = obj.json_dict
60
+ end
61
+
62
+ # Executes the internal function _create if the object item don't have id
63
+ def save
64
+ _create unless @json_dict.fetch(@id_property, false)
65
+ end
66
+ end
67
+
68
+ # Allows change an object item
69
+ class SaveObjectItemMixin < CreateObjectItemMixin
70
+ # Changes the object item with the json_dict data
71
+ def _change
72
+ id_required_and_not_deleted
73
+ obj = @resource.change(
74
+ @json_dict[@id_property],
75
+ @json_dict
76
+ )
77
+ @json_dict = obj.json_dict
78
+ end
79
+
80
+ # Executes the internal function _create if the object item don't have id,
81
+ # in other case, call to _change
82
+ def save
83
+ if @json_dict.fetch(@id_property, false)
84
+ _change
85
+ else
86
+ _create
87
+ end
88
+ end
89
+ end
90
+
91
+ # Allows make refund, capture and void an object item
92
+ class RefundCaptureVoidObjectItemMixin < ObjectItemMixin
93
+ # Refund the object item
94
+ # Params:
95
+ # +data+:: data to send
96
+ # Returns: response dictionary
97
+ def refund(data = nil)
98
+ id_required_and_not_deleted
99
+ @resource.refund(
100
+ @json_dict[@id_property],
101
+ data
102
+ )
103
+ end
104
+
105
+ # Capture the object item
106
+ # Params:
107
+ # +data+:: data to send
108
+ # Returns: response dictionary
109
+ def capture(data = nil)
110
+ id_required_and_not_deleted
111
+ @resource.capture(
112
+ @json_dict[@id_property],
113
+ data
114
+ )
115
+ end
116
+
117
+ # Void the object item
118
+ # Params:
119
+ # +data+:: data to send
120
+ # Returns: response dictionary
121
+ def void(data = nil)
122
+ id_required_and_not_deleted
123
+ @resource.void(
124
+ @json_dict[@id_property],
125
+ data
126
+ )
127
+ end
128
+ end
129
+
130
+ # Allows make card and share an object item
131
+ class CardShareObjectItemMixin < ObjectItemMixin
132
+ # Send card details
133
+ # Params:
134
+ # +gateway_code+:: gateway_code to send
135
+ # +data+:: data to send
136
+ # Returns: response dictionary
137
+ def card(gateway_code, data = nil)
138
+ id_required_and_not_deleted
139
+ @resource.card(
140
+ @json_dict[@id_property],
141
+ gateway_code,
142
+ data
143
+ )
144
+ end
145
+
146
+ # Send share details
147
+ # Params:
148
+ # +data+:: data to send
149
+ # Returns: response dictionary
150
+ def share(data = nil)
151
+ id_required_and_not_deleted
152
+ @resource.share(
153
+ @json_dict[@id_property],
154
+ data
155
+ )
156
+ end
157
+ end
158
+
159
+ # Add property to get pay_url based on token
160
+ class PayURLMixin < ObjectItemMixin
161
+ def initialize(json_dict, resource)
162
+ super(json_dict, resource)
163
+ @pay_url = 'https://pay.mychoice2pay.com/#/%s'
164
+ @iframe_url = 'https://pay.mychoice2pay.com/#/%s/iframe'
165
+ end
166
+
167
+ # Returns: pay url
168
+ def pay_url
169
+ id_required_and_not_deleted
170
+ @pay_url % @json_dict['token']
171
+ end
172
+
173
+ # Returns: iframe url
174
+ def iframe_url
175
+ id_required_and_not_deleted
176
+ @iframe_url % @json_dict['token']
177
+ end
178
+ end
179
+
180
+ # Basic info of the resource
181
+ class ResourceMixin
182
+ # Initializes a resource
183
+ # Params:
184
+ # +api_request+:: Api request used to make all the requests to the API
185
+ # +path+:: Path used to make all the requests to the API
186
+ # +object_item_class+:: Object item class used to return values
187
+ # +paginator_class+:: Paginator class used to return values
188
+ def initialize(api_request, path, object_item_class, paginator_class)
189
+ @api_request = api_request
190
+ @path = path
191
+ @object_item_class = object_item_class
192
+ @paginator_class = paginator_class
193
+ end
194
+
195
+ # Params:
196
+ # +resource_id+:: id used on the url returned
197
+ # Returns: url to request or change an item
198
+ def detail_url(resource_id)
199
+ "#{@path}#{resource_id}/"
200
+ end
201
+
202
+ # Help function to make a request that return one item
203
+ # Params:
204
+ # +func+:: function to make the request
205
+ # +data+:: data passed in the request
206
+ # +resource_id+:: id to use on the requested url
207
+ # Returns: an object item that represent the item returned
208
+ def _one_item(func, data = nil, resource_id = nil)
209
+ url = resource_id.nil? ? @path : detail_url(resource_id)
210
+
211
+ obj_data = @api_request.send(
212
+ func,
213
+ url,
214
+ data,
215
+ nil,
216
+ self,
217
+ resource_id
218
+ )
219
+
220
+ @object_item_class.new(obj_data, self)
221
+ end
222
+ end
223
+
224
+ # Allows send requests of detail
225
+ class DetailOnlyResourceMixin < ResourceMixin
226
+ # Params:
227
+ # +resource_id+:: id to request
228
+ # Returns: an object item class with the response of the server
229
+ def detail(resource_id)
230
+ _one_item('get',
231
+ nil,
232
+ resource_id)
233
+ end
234
+ end
235
+
236
+ # Allows send requests of list and detail
237
+ class ReadOnlyResourceMixin < DetailOnlyResourceMixin
238
+ # Params:
239
+ # +abs_url+:: if is passed the request is sent to this url
240
+ # Returns: a paginator class with the response of the server
241
+ def list(abs_url = nil)
242
+ json_dict = @api_request.get(
243
+ abs_url.nil? ? @path : nil,
244
+ nil,
245
+ abs_url,
246
+ self
247
+ )
248
+ @paginator_class.new(json_dict, @object_item_class, self)
249
+ end
250
+ end
251
+
252
+ # Allows send requests of create
253
+ class CreateResourceMixin < ResourceMixin
254
+ # Params:
255
+ # +data+:: data used on the request
256
+ # Returns: an object item class with the response of the server
257
+ def create(data)
258
+ _one_item('post',
259
+ data)
260
+ end
261
+ end
262
+
263
+ # Allows send requests of change
264
+ class ChangeResourceMixin < ResourceMixin
265
+ # Params:
266
+ # +resource_id+:: id to request
267
+ # +data+:: data used on the request
268
+ # Returns: an object item class with the response of the server
269
+ def change(resource_id, data)
270
+ _one_item('patch',
271
+ data,
272
+ resource_id)
273
+ end
274
+ end
275
+
276
+ # Allows send requests of delete
277
+ class DeleteResourceMixin < ResourceMixin
278
+ # Params:
279
+ # +resource_id+::id to request
280
+ def delete(resource_id)
281
+ _one_item('delete',
282
+ nil,
283
+ resource_id)
284
+ end
285
+ end
286
+
287
+ # Allows send requests of actions
288
+ class ActionsResourceMixin < ResourceMixin
289
+ # Params:
290
+ # +resource_id+:: id used on the url returned
291
+ # +action+:: action used on the url returned
292
+ # Returns: url to make an action in an item
293
+ def detail_action_url(resource_id, action)
294
+ "#{@path}#{resource_id}/#{action}/"
295
+ end
296
+
297
+ # Params:
298
+ # +func+:: function to make the request
299
+ # +resource_id+:: id to use on the requested url
300
+ # +action+:: action to use on the requested url
301
+ # +data+:: data passed in the request
302
+ # Returns: response dictionary
303
+ def _one_item_action(func, resource_id, action, data = nil)
304
+ url = detail_action_url(resource_id, action)
305
+ @api_request.send(
306
+ func,
307
+ url,
308
+ data,
309
+ nil,
310
+ self,
311
+ resource_id
312
+ )
313
+ end
314
+ end
315
+
316
+ # Allows send action requests of refund, capture and void
317
+ class RefundCaptureVoidResourceMixin < ActionsResourceMixin
318
+ # Params:
319
+ # +resource_id+:: id to request
320
+ # +data+:: data to send
321
+ # Returns: response dictionary
322
+ def refund(resource_id, data = nil)
323
+ _one_item_action('post200',
324
+ resource_id,
325
+ 'refund',
326
+ data)
327
+ end
328
+
329
+ # Params:
330
+ # +resource_id+:: id to request
331
+ # +data+:: data to send
332
+ # Returns: response dictionary
333
+ def capture(resource_id, data = nil)
334
+ _one_item_action('post200',
335
+ resource_id,
336
+ 'capture',
337
+ data)
338
+ end
339
+
340
+ # Params:
341
+ # +resource_id+:: id to request
342
+ # +data+:: data to send
343
+ # Returns: response dictionary
344
+ def void(resource_id, data = nil)
345
+ _one_item_action('post200',
346
+ resource_id,
347
+ 'void',
348
+ data)
349
+ end
350
+ end
351
+
352
+ # Allows send action requests of card and share
353
+ class CardShareResourceMixin < ActionsResourceMixin
354
+ # Params:
355
+ # +resource_id+:: id to request
356
+ # +gateway_code+:: gateway_code to send
357
+ # +data+:: data to send
358
+ # Returns: response dictionary
359
+ def card(resource_id, gateway_code, data = nil)
360
+ _one_item_action('post',
361
+ resource_id,
362
+ "card/#{gateway_code}",
363
+ data)
364
+ end
365
+
366
+ # Params:
367
+ # +resource_id+:: id to request
368
+ # +data+:: data to send
369
+ # Returns: response dictionary
370
+ def share(resource_id, data = nil)
371
+ _one_item_action('post',
372
+ resource_id,
373
+ 'share',
374
+ data)
375
+ end
376
+ end
377
+ end
@@ -0,0 +1,73 @@
1
+ module MC2P
2
+ # Notification data - class to manage notification from MyChoice2Pay
3
+ class NotificationData
4
+ # Initializes a notification data
5
+ # Params:
6
+ # +json_body+:: content of request from MyChoice2Pay
7
+ # +mc2p+:: MC2PClient
8
+ def initialize(json_body, mc2p)
9
+ @json_body = json_body
10
+ @mc2p = mc2p
11
+ end
12
+
13
+ # Returns: status of payment
14
+ def status
15
+ @json_body['status']
16
+ end
17
+
18
+ # Returns: status of subscription
19
+ def subscription_status
20
+ @json_body['subscription_status']
21
+ end
22
+
23
+ # Returns: type of payment
24
+ def type
25
+ @json_body['type']
26
+ end
27
+
28
+ # Returns: order_id sent when payment was created
29
+ def order_id
30
+ @json_body['order_id']
31
+ end
32
+
33
+ # Returns: action executed
34
+ def action
35
+ @json_body['action']
36
+ end
37
+
38
+ # Returns: transaction generated when payment was created
39
+ def transaction
40
+ ret = nil
41
+ if type == 'P'
42
+ ret = @mc2p.transaction('id' => @json_body['id'])
43
+ ret.retrieve
44
+ end
45
+ ret
46
+ end
47
+
48
+ # Returns: subscription generated when payment was created
49
+ def subscription
50
+ ret = nil
51
+ if type == 'S'
52
+ ret = @mc2p.subscription('id' => @json_body['id'])
53
+ ret.retrieve
54
+ end
55
+ ret
56
+ end
57
+
58
+ # Returns: sale generated when payment was paid
59
+ def sale
60
+ ret = nil
61
+ if @json_body.include?('sale_id')
62
+ ret = @mc2p.sale('id' => @json_body['sale_id'])
63
+ ret.retrieve
64
+ end
65
+ ret
66
+ end
67
+
68
+ # Returns: action of sale executed
69
+ def sale_action
70
+ @json_body['sale_action']
71
+ end
72
+ end
73
+ end
data/lib/objects.rb ADDED
@@ -0,0 +1,135 @@
1
+ module MC2P
2
+
3
+ # Product object
4
+ class Product < CRUDObjectItem
5
+
6
+ end
7
+
8
+ # Plan object
9
+ class Plan < CRUDObjectItem
10
+
11
+ end
12
+
13
+ # Tax object
14
+ class Tax < CRUDObjectItem
15
+
16
+ end
17
+
18
+ # Shipping object
19
+ class Shipping < CRUDObjectItem
20
+
21
+ end
22
+
23
+ # Coupon object
24
+ class Coupon < CRUDObjectItem
25
+
26
+ end
27
+
28
+ # Transaction object
29
+ class Transaction < PayURLCRObjectItem
30
+
31
+ end
32
+
33
+ # Subscription object
34
+
35
+ class Subscription < PayURLCRObjectItem
36
+
37
+ end
38
+
39
+ # Sale object
40
+ class Sale < ReadOnlyObjectItem
41
+ # Initializes an object item
42
+ # Params:
43
+ # +json_dict+:: Data of the object
44
+ # +resource+:: Resource used to delete, save, create or retrieve the object
45
+ def initialize(json_dict, resource)
46
+ super(json_dict, resource)
47
+ @rcv_mixin = RefundCaptureVoidObjectItemMixin.new(json_dict, resource)
48
+ end
49
+
50
+ # Refund the object item
51
+ # Params:
52
+ # +data+:: data to send
53
+ # Returns: response dictionary
54
+ def refund(data = nil)
55
+ @rcv_mixin.json_dict = @json_dict
56
+ @rcv_mixin._deleted = @_deleted
57
+ @rcv_mixin.refund(data)
58
+ @json_dict = @rcv_mixin.json_dict
59
+ @_deleted = @rcv_mixin._deleted
60
+ end
61
+
62
+ # Capture the object item
63
+ # Params:
64
+ # +data+:: data to send
65
+ # Returns: response dictionary
66
+ def capture(data = nil)
67
+ @rcv_mixin.json_dict = @json_dict
68
+ @rcv_mixin._deleted = @_deleted
69
+ @rcv_mixin.capture(data)
70
+ @json_dict = @rcv_mixin.json_dict
71
+ @_deleted = @rcv_mixin._deleted
72
+ end
73
+
74
+ # Void the object item
75
+ # Params:
76
+ # +data+:: data to send
77
+ # Returns: response dictionary
78
+ def void(data = nil)
79
+ @rcv_mixin.json_dict = @json_dict
80
+ @rcv_mixin._deleted = @_deleted
81
+ @rcv_mixin.void(data)
82
+ @json_dict = @rcv_mixin.json_dict
83
+ @_deleted = @rcv_mixin._deleted
84
+ end
85
+
86
+ end
87
+
88
+ # Currency object
89
+ class Currency < ReadOnlyObjectItem
90
+
91
+ end
92
+
93
+ # Gateway object
94
+ class Gateway < ReadOnlyObjectItem
95
+
96
+ end
97
+
98
+ # PayData object
99
+ class PayData < ReadOnlyObjectItem
100
+
101
+ # Initializes an object item
102
+ # Params:
103
+ # +json_dict+:: Data of the object
104
+ # +resource+:: Resource used to delete, save, create or retrieve the object
105
+ def initialize(json_dict, resource)
106
+ super(json_dict, resource, 'token')
107
+ @cs_mixin = CardShareObjectItemMixin.new(json_dict, resource, 'token')
108
+ end
109
+
110
+ # Send card details
111
+ # Params:
112
+ # +gateway_code+:: gateway_code to send
113
+ # +data+:: data to send
114
+ # Returns: response dictionary
115
+ def card(gateway_code, data = nil)
116
+ @cs_mixin.json_dict = @json_dict
117
+ @cs_mixin._deleted = @_deleted
118
+ @cs_mixin.card(gateway_code, data)
119
+ @json_dict = @cs_mixin.json_dict
120
+ @_deleted = @cs_mixin._deleted
121
+ end
122
+
123
+ # Send share details
124
+ # Params:
125
+ # +data+:: data to send
126
+ # Returns: response dictionary
127
+ def share(data = nil)
128
+ @cs_mixin.json_dict = @json_dict
129
+ @cs_mixin._deleted = @_deleted
130
+ @cs_mixin.share(data)
131
+ @json_dict = @cs_mixin.json_dict
132
+ @_deleted = @cs_mixin._deleted
133
+ end
134
+ end
135
+ end
data/lib/request.rb ADDED
@@ -0,0 +1,87 @@
1
+ module MC2P
2
+ # API request - class used to connect with the API
3
+ class APIRequest
4
+ # Initializes an api request
5
+ # Params:
6
+ # +key+:: key to connect with API
7
+ # +secret_key+:: secret key to connect with API
8
+ def initialize(key, secret_key)
9
+ @key = key
10
+ @secret_key = secret_key
11
+
12
+ @authorization_header = 'AppKeys'
13
+ @api_url = 'api.mychoice2pay.com/v1'
14
+ end
15
+
16
+ # Creates the headers to include in the request
17
+ # Returns: A dictionary with the headers needed for the API
18
+ def headers
19
+ {
20
+ 'authorization' => "#{@authorization_header} #{@key}:#{@secret_key}",
21
+ 'content-type' => 'application/json'
22
+ }
23
+ end
24
+
25
+ # Params:
26
+ # +path+:: relative url
27
+ # Returns: The absolute url to send the request
28
+ def get_abs_url(path)
29
+ "https://#{@api_url}#{path}"
30
+ end
31
+
32
+ # Decorator to make the request based on the method received
33
+ # Params:
34
+ # +method+:: method to make the request
35
+ # +status_code+:: value to check if the request receive a correct response
36
+ # Returns: a function to make the request
37
+ def _request(method, status_code = 200, path = nil, data = nil,
38
+ abs_url = nil, resource = nil, resource_id = nil)
39
+ url = abs_url.nil? ? get_abs_url(path) : abs_url
40
+
41
+ request = Unirest.send(method.downcase, url,
42
+ headers: headers,
43
+ parameters: data.to_json)
44
+
45
+ if request.code != status_code
46
+ raise InvalidRequestError.new(
47
+ "Error #{request.code}",
48
+ request.body,
49
+ resource,
50
+ resource_id
51
+ )
52
+ end
53
+
54
+ status_code != 204 ? request.body : {}
55
+ end
56
+
57
+ # POST 201 function
58
+ def post(path = nil, data = nil, abs_url = nil,
59
+ resource = nil, resource_id = nil)
60
+ _request('post', 201, path, data, abs_url, resource, resource_id)
61
+ end
62
+
63
+ # POST 200 function
64
+ def post200(path = nil, data = nil, abs_url = nil,
65
+ resource = nil, resource_id = nil)
66
+ _request('post', 200, path, data, abs_url, resource, resource_id)
67
+ end
68
+
69
+ # GET 200 function
70
+ def get(path = nil, data = nil, abs_url = nil,
71
+ resource = nil, resource_id = nil)
72
+ _request('get', 200, path, data, abs_url, resource, resource_id)
73
+ end
74
+
75
+ # PATCH 200 function
76
+ def patch(path = nil, data = nil, abs_url = nil,
77
+ resource = nil, resource_id = nil)
78
+ _request('patch', 200, path, data, abs_url, resource, resource_id)
79
+ end
80
+
81
+ # DELETE 204 function
82
+ def delete(path = nil, data = nil, abs_url = nil,
83
+ resource = nil, resource_id = nil)
84
+ _request('delete', 204, path, data, abs_url, resource, resource_id)
85
+ end
86
+ end
87
+ end