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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2cf6d964ae739ae4de6ac141e488f89a29b28122
4
+ data.tar.gz: '0590e374daf4ad713f50b31d0880ab9f5b44d3d2'
5
+ SHA512:
6
+ metadata.gz: 2f265ef00b103392dcd85527a53b368fccfbf48722547892aa2c50d717883a2b89cd2a1c0392b598c4556a9c5a2b6e644ebe3740522c38b17890d737ab22246c
7
+ data.tar.gz: 4003ad34dca603880af5be7a7e7ea333483e904f2053604d242ffaa2830a4a82414ca4d38ae7cdc5e389f037b3dff6f178ad12f6c562667e8f96b174da0fe90c
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # MyChoice2Pay Ruby
2
+
3
+
4
+ # Overview
5
+
6
+ MyChoice2Pay Ruby provides integration access to the MyChoice2Pay API.
7
+
8
+ # Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'mc2p-ruby'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ gem install mc2p-ruby
21
+
22
+ # Quick Start Example
23
+
24
+ require 'mc2p'
25
+
26
+ mc2p = MC2P::MC2PClient.new('KEY', 'SECRET_KEY')
27
+
28
+ # Create transaction
29
+ transaction = mc2p.transaction({
30
+ 'currency' => 'EUR',
31
+ 'products' => [{
32
+ 'amount' => 1,
33
+ 'product_id' => 'PRODUCT-ID'
34
+ }]
35
+ })
36
+ # or with product details
37
+ transaction = mc2p.transaction({
38
+ 'currency' => 'EUR',
39
+ 'products' => [{
40
+ 'amount' => 1,
41
+ 'product' => {
42
+ 'name' => 'Product',
43
+ 'price' => 5
44
+ }
45
+ }]
46
+ })
47
+ transaction.save
48
+ transaction.pay_url # Send user to this url to pay
49
+ transaction.iframe_url # Use this url to show an iframe in your site
50
+
51
+ # Get plans
52
+ plans_paginator = mc2p.plan_resource.list
53
+ plans_paginator.count
54
+ plans_paginator.results # Application's plans
55
+ plans_paginator.next_list
56
+
57
+ # Get product, change and save
58
+ product = mc2p.product({
59
+ 'id' => 'PRODUCT-ID'
60
+ })
61
+ product.retrieve
62
+ product.set('price', 10)
63
+ product.save
64
+
65
+ # Create and delete tax
66
+ tax = mc2p.tax({
67
+ 'name' => 'Tax',
68
+ 'percent' => 5
69
+ })
70
+ tax.save
71
+ tax.delete
72
+
73
+ # Check if transaction was paid
74
+ transaction = mc2p.transaction({
75
+ 'id' => 'TRANSACTION-ID'
76
+ })
77
+ transaction.retrieve
78
+ transaction.status == 'D' # Paid
79
+
80
+ # Create subscription
81
+ subscription = mc2p.subscription({
82
+ 'currency' => 'EUR',
83
+ 'plan_id' => 'PLAN-ID',
84
+ 'note' => 'Note example'
85
+ })
86
+ # or with plan details
87
+ subscription = mc2p.subscription({
88
+ 'currency' => 'EUR',
89
+ 'plan' => {
90
+ 'name' => 'Plan',
91
+ 'price' => 5,
92
+ 'duration' => 1,
93
+ 'unit' => 'M',
94
+ 'recurring' => true
95
+ },
96
+ 'note' => 'Note example'
97
+ })
98
+ subscription.save
99
+ subscription.pay_url # Send user to this url to pay
100
+ subscription.iframe_url # Use this url to show an iframe in your site
101
+
102
+ # Receive a notification
103
+ notification_data = mc2p.notification_data(JSON_DICT_RECEIVED_FROM_MYCHOICE2PAY)
104
+ notification_data.status == 'D' # Paid
105
+ notification_data.transaction # Transaction Paid
106
+ notification_data.sale # Sale generated
107
+
108
+ # Exceptions
109
+
110
+ require 'mc2p'
111
+
112
+ # Incorrect data
113
+ shipping = mc2p.shipping({
114
+ 'name' => 'Normal shipping',
115
+ 'price' => 'text' # Price must be number
116
+ })
117
+ begin
118
+ shipping.save
119
+ rescue MC2P::InvalidRequestError => e
120
+ puts e.message # Status code of error
121
+ puts e.json_body # Info from server
122
+ puts e.resource # Resource used to make the server request
123
+ puts e.resource_id # Resource id requested
124
+ end
125
+
data/bin/mc2p ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib'
data/lib/base.rb ADDED
@@ -0,0 +1,331 @@
1
+ module MC2P
2
+ # Paginator - class used on list requests
3
+ class Paginator
4
+ attr_accessor :count
5
+ attr_accessor :results
6
+
7
+ # Initializes a paginator
8
+ # Params:
9
+ # +json_dict+:: Response from server
10
+ # +object_item_class+:: Class to wrapper all the items from results field
11
+ # +resource+:: Resource used to get next and previous items
12
+ def initialize(json_dict, object_item_class, resource)
13
+ @count = json_dict.fetch('count', 0)
14
+ @_previous = json_dict.fetch('previous', nil)
15
+ @_next = json_dict.fetch('next', nil)
16
+ @results = []
17
+ json_dict.fetch('results', []).each do |result|
18
+ @results.push(object_item_class.new(result, resource))
19
+ @resource = resource
20
+ end
21
+ end
22
+
23
+ # Params:
24
+ # Returns: Paginator object with the next items
25
+ def next_list
26
+ @_next ? @resource.list(abs_url: @_next) : nil
27
+ end
28
+
29
+ # Params:
30
+ # Returns: Paginator object with the previous items
31
+ def previous_list
32
+ @_previous ? @resource.list(abs_url: @_previous) : nil
33
+ end
34
+ end
35
+
36
+ # Object item - class used to wrap the data from API that represent an item
37
+ class ObjectItem < ObjectItemMixin
38
+ # Initializes an object item
39
+ # Params:
40
+ # +json_dict+:: Data of the object
41
+ # +resource+:: Resource used to delete, save, create or retrieve the object
42
+ def initialize(json_dict, resource)
43
+ @json_dict = json_dict.nil? ? {} : json_dict
44
+ @resource = resource
45
+ @_deleted = false
46
+ @id_property = 'id'
47
+ end
48
+
49
+ # Allows use the following syntax to get a field of the object:
50
+ # obj.name
51
+ # Params:
52
+ # +key+:: Field to return
53
+ # Returns: Value of the field from json_dict
54
+ def method_missing(key, *args)
55
+ @json_dict.include?(key.to_s) ? @json_dict[key.to_s] : super
56
+ end
57
+
58
+ def respond_to_missing?(key, include_private = false)
59
+ @json_dict.include?(key.to_s) || super
60
+ end
61
+
62
+ def respond_to?(key, include_private = false)
63
+ @json_dict.include?(key.to_s) || super
64
+ end
65
+
66
+ # Allows use the following syntax to set a field of the object:
67
+ # obj.name = 'example'
68
+ # Params:
69
+ # +key+:: Field to change
70
+ # +value+:: Content to replace the current value
71
+ def set(key, value)
72
+ @json_dict[key] = value
73
+ end
74
+ end
75
+
76
+ # Object item that allows retrieve an item
77
+ class ReadOnlyObjectItem < ObjectItem
78
+ # Initializes an object item
79
+ # Params:
80
+ # +json_dict+:: Data of the object
81
+ # +resource+:: Resource used to delete, save, create or retrieve the object
82
+ def initialize(json_dict, resource)
83
+ @retrieve_mixin = RetrieveObjectItemMixin.new(json_dict, resource)
84
+ super(json_dict, resource)
85
+ end
86
+
87
+ # Retrieve object with object_id and return
88
+ # Params:
89
+ # +object_id+:: Id to retrieve
90
+ # Returns: Object after retrieve
91
+ def self.get(object_id)
92
+ obj = new({
93
+ @id_property => object_id
94
+ }, resource)
95
+ obj.retrieve
96
+ obj
97
+ end
98
+
99
+ # Retrieves the data of the object item
100
+ def retrieve
101
+ @retrieve_mixin.json_dict = @json_dict
102
+ @retrieve_mixin._deleted = @_deleted
103
+ @retrieve_mixin.retrieve
104
+ @json_dict = @retrieve_mixin.json_dict
105
+ end
106
+ end
107
+
108
+ # Object item that allows retrieve and create an item
109
+ class CRObjectItem < ReadOnlyObjectItem
110
+ # Initializes an object item
111
+ # Params:
112
+ # +json_dict+:: Data of the object
113
+ # +resource+:: Resource used to delete, save, create or retrieve the object
114
+ def initialize(json_dict, resource)
115
+ @create_mixin = CreateObjectItemMixin.new(json_dict, resource)
116
+ super(json_dict, resource)
117
+ end
118
+
119
+ # Creates the object item with the json_dict data
120
+ def _create
121
+ @create_mixin.json_dict = @json_dict
122
+ @create_mixin._deleted = @_deleted
123
+ @create_mixin._create
124
+ @json_dict = @create_mixin.json_dict
125
+ end
126
+
127
+ # Executes the internal function _create if the object item don't have id
128
+ def save
129
+ @create_mixin.json_dict = @json_dict
130
+ @create_mixin._deleted = @_deleted
131
+ @create_mixin.save
132
+ @json_dict = @create_mixin.json_dict
133
+ end
134
+ end
135
+
136
+ # Object item that allows retrieve, create and change an item
137
+ class CRUObjectItem < ReadOnlyObjectItem
138
+ # Initializes an object item
139
+ # Params:
140
+ # +json_dict+:: Data of the object
141
+ # +resource+:: Resource used to delete, save, create or retrieve the object
142
+ def initialize(json_dict, resource)
143
+ @save_mixin = SaveObjectItemMixin.new(json_dict, resource)
144
+ super(json_dict, resource)
145
+ end
146
+
147
+ # Creates the object item with the json_dict data
148
+ def _create
149
+ @save_mixin.json_dict = @json_dict
150
+ @save_mixin._deleted = @_deleted
151
+ @save_mixin._create
152
+ @json_dict = @save_mixin.json_dict
153
+ end
154
+
155
+ # Creates the object item with the json_dict data
156
+ def _change
157
+ @save_mixin.json_dict = @json_dict
158
+ @save_mixin._deleted = @_deleted
159
+ @save_mixin._change
160
+ @json_dict = @save_mixin.json_dict
161
+ end
162
+
163
+ # Executes the internal function _create if the object item don't have id
164
+ def save
165
+ @save_mixin.json_dict = @json_dict
166
+ @save_mixin._deleted = @_deleted
167
+ @save_mixin.save
168
+ @json_dict = @save_mixin.json_dict
169
+ end
170
+ end
171
+
172
+ # Object item that allows retrieve, create, change and delete an item
173
+ class CRUDObjectItem < CRUObjectItem
174
+ # Initializes an object item
175
+ # Params:
176
+ # +json_dict+:: Data of the object
177
+ # +resource+:: Resource used to delete, save, create or retrieve the object
178
+ def initialize(json_dict, resource)
179
+ @delete_mixin = DeleteObjectItemMixin.new(json_dict, resource)
180
+ super(json_dict, resource)
181
+ end
182
+
183
+ # Deletes the object item
184
+ def delete
185
+ @delete_mixin.json_dict = @json_dict
186
+ @delete_mixin._deleted = @_deleted
187
+ @delete_mixin.delete
188
+ @json_dict = @delete_mixin.json_dict
189
+ @_deleted = @delete_mixin._deleted
190
+ end
191
+ end
192
+
193
+ # Object item that allows retrieve, create and to get pay_url based
194
+ # on token of an item
195
+ class PayURLCRObjectItem < CRObjectItem
196
+ # Initializes an object item
197
+ # Params:
198
+ # +json_dict+:: Data of the object
199
+ # +resource+:: Resource used to delete, save, create or retrieve the object
200
+ def initialize(json_dict, resource)
201
+ @pay_url_mixin = PayURLMixin.new(json_dict, resource)
202
+ super(json_dict, resource)
203
+ end
204
+
205
+ # Returns: pay_url
206
+ def pay_url
207
+ @pay_url_mixin.json_dict = @json_dict
208
+ @pay_url_mixin.pay_url
209
+ end
210
+
211
+ # Returns: iframe_url
212
+ def iframe_url
213
+ @pay_url_mixin.json_dict = @json_dict
214
+ @pay_url_mixin.iframe_url
215
+ end
216
+ end
217
+
218
+ # Resource - class used to manage the requests to the API related with
219
+ # a resource
220
+ # ex: product
221
+ class Resource < ResourceMixin
222
+ # Initializes a resource
223
+ # Params:
224
+ # +api_request+:: Api request used to make all the requests to the API
225
+ # +path+:: Path used to make all the requests to the API
226
+ # +object_item_class+:: Object item class used to return values
227
+ def initialize(api_request, path, object_item_class)
228
+ @paginator_class = Paginator
229
+ super(api_request, path, object_item_class, @paginator_class)
230
+ end
231
+ end
232
+
233
+ # Resource that allows send requests of detail
234
+ class DetailOnlyResource < Resource
235
+ # Initializes a resource
236
+ # Params:
237
+ # +api_request+:: Api request used to make all the requests to the API
238
+ # +path+:: Path used to make all the requests to the API
239
+ # +object_item_class+:: Object item class used to return values
240
+ def initialize(api_request, path, object_item_class)
241
+ super(api_request, path, object_item_class)
242
+ @do_resource_mixin = DetailOnlyResourceMixin.new(api_request, path,
243
+ object_item_class,
244
+ @paginator_class)
245
+ end
246
+
247
+ # Params:
248
+ # +resource_id+:: id to request
249
+ # Returns: an object item class with the response of the server
250
+ def detail(resource_id)
251
+ @do_resource_mixin.detail(resource_id)
252
+ end
253
+ end
254
+
255
+ # Resource that allows send requests of list and detail
256
+ class ReadOnlyResource < DetailOnlyResource
257
+ # Initializes a resource
258
+ # Params:
259
+ # +api_request+:: Api request used to make all the requests to the API
260
+ # +path+:: Path used to make all the requests to the API
261
+ # +object_item_class+:: Object item class used to return values
262
+ def initialize(api_request, path, object_item_class)
263
+ super(api_request, path, object_item_class)
264
+ @ro_resource_mixin = ReadOnlyResourceMixin.new(api_request, path,
265
+ object_item_class,
266
+ @paginator_class)
267
+ end
268
+
269
+ # Params:
270
+ # +abs_url+:: if is passed the request is sent to this url
271
+ # Returns: a paginator class with the response of the server
272
+ def list(abs_url = nil)
273
+ @ro_resource_mixin.list(abs_url)
274
+ end
275
+ end
276
+
277
+ # Resource that allows send requests of create, list and detail
278
+ class CRResource < ReadOnlyResource
279
+ # Initializes a resource
280
+ # Params:
281
+ # +api_request+:: Api request used to make all the requests to the API
282
+ # +path+:: Path used to make all the requests to the API
283
+ # +object_item_class+:: Object item class used to return values
284
+ def initialize(api_request, path, object_item_class)
285
+ super(api_request, path, object_item_class)
286
+ @create_resource_mixin = CreateResourceMixin.new(api_request, path,
287
+ object_item_class,
288
+ @paginator_class)
289
+ end
290
+
291
+ # Params:
292
+ # +data+:: data used on the request
293
+ # Returns: an object item class with the response of the server
294
+ def create(data)
295
+ @create_resource_mixin.create(data)
296
+ end
297
+ end
298
+
299
+ # Resource that allows send requests of delete, change, create,
300
+ # list and detail
301
+ class CRUDResource < CRResource
302
+ # Initializes a resource
303
+ # Params:
304
+ # +api_request+:: Api request used to make all the requests to the API
305
+ # +path+:: Path used to make all the requests to the API
306
+ # +object_item_class+:: Object item class used to return values
307
+ def initialize(api_request, path, object_item_class)
308
+ super(api_request, path, object_item_class)
309
+ @change_resource_mixin = ChangeResourceMixin.new(api_request, path,
310
+ object_item_class,
311
+ @paginator_class)
312
+ @delete_resource_mixin = DeleteResourceMixin.new(api_request, path,
313
+ object_item_class,
314
+ @paginator_class)
315
+ end
316
+
317
+ # Params:
318
+ # +resource_id+:: id to request
319
+ # +data+:: data used on the request
320
+ # Returns: an object item class with the response of the server
321
+ def change(resource_id, data)
322
+ @change_resource_mixin.change(resource_id, data)
323
+ end
324
+
325
+ # Params:
326
+ # +resource_id+::id to request
327
+ def delete(resource_id)
328
+ @delete_resource_mixin.delete(resource_id)
329
+ end
330
+ end
331
+ end
data/lib/errors.rb ADDED
@@ -0,0 +1,37 @@
1
+ module MC2P
2
+ # MC2P Error - class used to manage the exceptions related with mc2p library
3
+ class MC2PError < StandardError
4
+ attr_accessor :json_body
5
+ attr_accessor :resource
6
+ attr_accessor :resource_id
7
+
8
+ # Initializes an error
9
+ # Params:
10
+ # +message+:: Error type
11
+ # +json_body+:: Response from server
12
+ # +resource+:: Class resource used when the error raised
13
+ # +resource_id+:: Resource id requested when the error raised
14
+ def initialize(message = nil, json_body = nil,
15
+ resource = nil, resource_id = nil)
16
+ super(message)
17
+
18
+ @_message = message
19
+ @json_body = json_body
20
+ @resource = resource
21
+ @resource_id = resource_id
22
+ end
23
+
24
+ # Returns: Error type and response
25
+ def to_s
26
+ "#{@_message} #{@json_body}"
27
+ end
28
+ end
29
+
30
+ # Invalid request error
31
+ class InvalidRequestError < MC2PError
32
+ end
33
+
34
+ # Bad use error
35
+ class BadUseError < MC2PError
36
+ end
37
+ end
data/lib/mc2p.rb ADDED
@@ -0,0 +1,125 @@
1
+ require 'unirest'
2
+
3
+ require_relative 'errors'
4
+ require_relative 'request'
5
+ require_relative 'mixins'
6
+ require_relative 'base'
7
+ require_relative 'objects'
8
+ require_relative 'resources'
9
+ require_relative 'notification'
10
+
11
+ module MC2P
12
+ VERSION = '0.1.2'
13
+
14
+ # MC2P - class used to manage the communication with MyChoice2Pay API
15
+ class MC2PClient
16
+ attr_accessor :api_request
17
+
18
+ attr_accessor :product_resource
19
+ attr_accessor :plan_resource
20
+ attr_accessor :tax_resource
21
+ attr_accessor :shipping_resource
22
+ attr_accessor :coupon_resource
23
+ attr_accessor :transaction_resource
24
+ attr_accessor :subscription_resource
25
+ attr_accessor :sale_resource
26
+ attr_accessor :currency_resource
27
+ attr_accessor :gateway_resource
28
+ attr_accessor :pay_data_resource
29
+
30
+ # Initializes a resource
31
+ # Params:
32
+ # +api_request+:: Api request used to make all the requests to the API
33
+ # +path+:: Path used to make all the requests to the API
34
+ # +object_item_class+:: Object item class used to return values
35
+ def initialize(key, secret_key)
36
+ @api_request = APIRequest.new(key, secret_key)
37
+
38
+ @product_resource = ProductResource.new(@api_request,
39
+ '/product/',
40
+ Product)
41
+ @plan_resource = PlanResource.new(@api_request,
42
+ '/plan/',
43
+ Plan)
44
+ @tax_resource = TaxResource.new(@api_request,
45
+ '/tax/',
46
+ Tax)
47
+ @shipping_resource = ShippingResource.new(@api_request,
48
+ '/shipping/',
49
+ Shipping)
50
+ @coupon_resource = CouponResource.new(@api_request,
51
+ '/coupon/',
52
+ Coupon)
53
+ @transaction_resource = TransactionResource.new(@api_request,
54
+ '/transaction/',
55
+ Transaction)
56
+ @subscription_resource = SubscriptionResource.new(@api_request,
57
+ '/subscription/',
58
+ Subscription)
59
+ @sale_resource = SaleResource.new(@api_request,
60
+ '/sale/',
61
+ Sale)
62
+ @currency_resource = CurrencyResource.new(@api_request,
63
+ '/currency/',
64
+ Currency)
65
+ @gateway_resource = GatewayResource.new(@api_request,
66
+ '/gateway/',
67
+ Gateway)
68
+ @pay_data_resource = PayDataResource.new(@api_request,
69
+ '/pay/',
70
+ PayData)
71
+ end
72
+
73
+ def _wrapper(cls, resource, data)
74
+ cls.new(data, resource)
75
+ end
76
+
77
+ def product(data)
78
+ _wrapper(Product, @product_resource, data)
79
+ end
80
+
81
+ def plan(data)
82
+ _wrapper(Plan, @plan_resource, data)
83
+ end
84
+
85
+ def tax(data)
86
+ _wrapper(Tax, @tax_resource, data)
87
+ end
88
+
89
+ def shipping(data)
90
+ _wrapper(Shipping, @shipping_resource, data)
91
+ end
92
+
93
+ def coupon(data)
94
+ _wrapper(Coupon, @coupon_resource, data)
95
+ end
96
+
97
+ def transaction(data)
98
+ _wrapper(Transaction, @transaction_resource, data)
99
+ end
100
+
101
+ def subscription(data)
102
+ _wrapper(Subscription, @subscription_resource, data)
103
+ end
104
+
105
+ def sale(data)
106
+ _wrapper(Sale, @sale_resource, data)
107
+ end
108
+
109
+ def currency(data)
110
+ _wrapper(Currency, @currency_resource, data)
111
+ end
112
+
113
+ def gateway(data)
114
+ _wrapper(Gateway, @gateway_resource, data)
115
+ end
116
+
117
+ def pay_data(data)
118
+ _wrapper(PayData, @pay_data_resource, data)
119
+ end
120
+
121
+ def notification_data(data)
122
+ NotificationData.new(data, self)
123
+ end
124
+ end
125
+ end