simplex-mercadopago-sdk 0.2.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f93c33e7cd78a08d80d5ae16970df8b976319f2
4
- data.tar.gz: 0c620b90e7e37e08ef246f4192938b25fec59464
3
+ metadata.gz: 6d4572d150e5adacbe8cd1fc2dd379b123a7a889
4
+ data.tar.gz: 3fe6ab4a85cd4aaee027f2af94e07aed60ddf81e
5
5
  SHA512:
6
- metadata.gz: ea36182edf6ed287e110cf51d9bb65b5ef76c4a9ce66b02be4084552f121968d8340fd1e7a6a4525cd157bd0ba3d8d7ce346d3c6eb6db8cb811e853373b260d3
7
- data.tar.gz: 348a5a30aab886c4a405a937f6b82f82b63ccbd8dc387ed6d4b7506242f6a1e90bfdcf39ce6025352ba1b44a735e3d7a8aec97c252f79db975ca3b38c4e8cd93
6
+ metadata.gz: e72f803e7326f6c361fec51cb86aa16eab3b17e9971bcf154473c9afca5077bf5a4df9d18ca3a56c9f422c8ae3997c81cdd967152c488e766b02d7e9e9e1e9c4
7
+ data.tar.gz: 06a82441ff2876a45144e9b09bde5f40cc2bb897827b9c3268fd86a45c7497235e9ab1bc540b8e4b925b1348d08f67aa9cb7a1c8a4c9ac2d941ac3d1317bd90a
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simplex-mercadopago-sdk (0.2.1)
4
+ simplex-mercadopago-sdk (0.3.4)
5
5
  json
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  coderay (1.0.9)
11
- json (1.8.1)
11
+ json (1.8.3)
12
12
  method_source (0.8.1)
13
13
  pry (0.9.12)
14
14
  coderay (~> 1.0.5)
data/README.md CHANGED
@@ -1,14 +1,19 @@
1
1
  # MercadoPago SDK module for Payments integration
2
2
 
3
- * [Usage](#usage)
4
- * [Using MercadoPago Checkout](#checkout)
5
- * [Using MercadoPago Payment collection](#payments)
3
+ * [Install](#install)
4
+ * [Basic checkout](#basic-checkout)
5
+ * [Customized checkout](#custom-checkout)
6
+ * [Generic methods](#generic-methods)
6
7
 
7
- <a name="usage"></a>
8
- ## Usage:
8
+ <a name="install"></a>
9
+ ## Install
9
10
 
10
- 1. Copy lib/mercadopago.rb to your project desired folder.
11
- 2. Copy lib/cacert.pem to the same folder (for SSL access to MercadoPago APIs).
11
+ ```gem install mercadopago-sdk```
12
+
13
+ <a name="basic-checkout"></a>
14
+ ## Basic checkout
15
+
16
+ ### Configure your credentials
12
17
 
13
18
  * Get your **CLIENT_ID** and **CLIENT_SECRET** in the following address:
14
19
  * Argentina: [https://www.mercadopago.com/mla/herramientas/aplicaciones](https://www.mercadopago.com/mla/herramientas/aplicaciones)
@@ -16,6 +21,7 @@
16
21
  * México: [https://www.mercadopago.com/mlm/herramientas/aplicaciones](https://www.mercadopago.com/mlm/herramientas/aplicaciones)
17
22
  * Venezuela: [https://www.mercadopago.com/mlv/herramientas/aplicaciones](https://www.mercadopago.com/mlv/herramientas/aplicaciones)
18
23
  * Colombia: [https://www.mercadopago.com/mco/herramientas/aplicaciones](https://www.mercadopago.com/mco/herramientas/aplicaciones)
24
+ * Chile: [https://www.mercadopago.com/mlc/herramientas/aplicaciones](https://www.mercadopago.com/mlc/herramientas/aplicaciones)
19
25
 
20
26
  ```ruby
21
27
  require 'mercadopago.rb'
@@ -23,18 +29,9 @@ require 'mercadopago.rb'
23
29
  $mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
24
30
  ```
25
31
 
26
- ### Get your Access Token:
27
-
28
- ```ruby
29
- $accessToken = $mp.getAccessToken()
30
-
31
- puts (accessToken)
32
- ```
32
+ ### Preferences
33
33
 
34
- <a name="checkout"></a>
35
- ## Using MercadoPago Checkout
36
-
37
- ### Get an existent Checkout preference:
34
+ #### Get an existent Checkout preference
38
35
 
39
36
  ```ruby
40
37
  preference = $mp.get_preference('PREFERENCE_ID')
@@ -42,18 +39,26 @@ preference = $mp.get_preference('PREFERENCE_ID')
42
39
  puts $preferenceResult
43
40
  ```
44
41
 
45
- ### Create a Checkout preference:
42
+ #### Create a Checkout preference
46
43
 
47
44
  ```ruby
48
- preferenceData = Hash["items" => Array(Array["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"])]
49
-
50
- preference = $mp.create_preference(preferenceData)
45
+
46
+ preference_data = {
47
+ "items": [
48
+ {
49
+ "title": "testCreate",
50
+ "quantity": 1,
51
+ "unit_price": 10.2,
52
+ "currency_id": "ARS"
53
+ }
54
+ ]
55
+ }
56
+ preference = $mp.create_preference(preference_data)
51
57
 
52
58
  puts preference
53
59
  ```
54
- <a href="http://developers.mercadopago.com/documentacion/recibir-pagos#glossary">Others items to use</a>
55
60
 
56
- ### Update an existent Checkout preference:
61
+ #### Update an existent Checkout preference
57
62
 
58
63
  ```ruby
59
64
  preferenceDataToUpdate = Hash["items" => Array(Array["title"=>"testUpdated", "quantity"=>1, "unit_price"=>2])]
@@ -63,10 +68,9 @@ preferenceUpdate = $mp.update_preference("PREFERENCE_ID", preferenceDataToUpdate
63
68
  puts preferenceUpdate
64
69
  ```
65
70
 
66
- <a name="payments"></a>
67
- ## Using MercadoPago Payment
71
+ ### Payments/Collections
68
72
 
69
- ### Searching:
73
+ #### Search for payments
70
74
 
71
75
  ```ruby
72
76
  filters = Array["id"=>null, "site_id"=>null, "external_reference"=>null]
@@ -75,25 +79,16 @@ searchResult = $mp.search_payment(filters)
75
79
 
76
80
  puts searchResult
77
81
  ```
78
- <a href="http://developers.mercadopago.com/documentacion/busqueda-de-pagos-recibidos">More search examples</a>
79
-
80
- ### Receiving IPN notification:
81
82
 
82
- * Go to **Mercadopago IPN configuration**:
83
- * Argentina: [https://www.mercadopago.com/mla/herramientas/notificaciones](https://www.mercadopago.com/mla/herramientas/notificaciones)
84
- * Brasil: [https://www.mercadopago.com/mlb/ferramentas/notificacoes](https://www.mercadopago.com/mlb/ferramentas/notificacoes)
85
- * México: [https://www.mercadopago.com/mlm/herramientas/notificaciones](https://www.mercadopago.com/mlm/herramientas/notificaciones)
86
- * Venezuela: [https://www.mercadopago.com/mlv/herramientas/notificaciones](https://www.mercadopago.com/mlv/herramientas/notificaciones)
87
- * Colombia: [https://www.mercadopago.com/mco/herramientas/notificaciones](https://www.mercadopago.com/mco/herramientas/notificaciones)<br />
83
+ #### Get payment data
88
84
 
89
85
  ```ruby
90
-
91
- paymentInfo = $mp.get_payment_info("ID")
86
+ paymentInfo = $mp.get_payment("ID")
92
87
 
93
88
  puts paymentInfo
94
89
  ```
95
90
 
96
- ### Cancel (only for pending payments):
91
+ ### Cancel (only for pending payments)
97
92
 
98
93
  ```ruby
99
94
  result = $mp.cancel_payment("ID");
@@ -102,7 +97,7 @@ result = $mp.cancel_payment("ID");
102
97
  puts result
103
98
  ```
104
99
 
105
- ### Refund (only for accredited payments):
100
+ ### Refund (only for accredited payments)
106
101
 
107
102
  ```ruby
108
103
  result = $mp.refund_payment("ID");
@@ -110,4 +105,72 @@ result = $mp.refund_payment("ID");
110
105
  // Show result
111
106
  puts result
112
107
  ```
113
- <a href=http://developers.mercadopago.com/documentacion/devolucion-y-cancelacion>About Cancel & Refund</a>
108
+
109
+ <a name="custom-checkout"></a>
110
+ ## Customized checkout
111
+
112
+ ### Configure your credentials
113
+
114
+ * Get your **ACCESS_TOKEN** in the following address:
115
+ * Argentina: [https://www.mercadopago.com/mla/account/credentials](https://www.mercadopago.com/mla/account/credentials)
116
+ * Brazil: [https://www.mercadopago.com/mlb/account/credentials](https://www.mercadopago.com/mlb/account/credentials)
117
+ * Mexico: [https://www.mercadopago.com/mlm/account/credentials](https://www.mercadopago.com/mlm/account/credentials)
118
+ * Venezuela: [https://www.mercadopago.com/mlv/account/credentials](https://www.mercadopago.com/mlv/account/credentials)
119
+ * Colombia: [https://www.mercadopago.com/mco/account/credentials](https://www.mercadopago.com/mco/account/credentials)
120
+
121
+ ```ruby
122
+ require 'mercadopago.rb'
123
+
124
+ $mp = MercadoPago.new('ACCESS_TOKEN')
125
+ ```
126
+
127
+ ### Create payment
128
+
129
+ ```ruby
130
+ $mp.post ("/v1/payments", payment_data);
131
+ ```
132
+
133
+ ### Create customer
134
+
135
+ ```ruby
136
+ $mp.post ("/v1/customers", Hash["email" => "email@test.com"]);
137
+ ```
138
+
139
+ ### Get customer
140
+
141
+ ```ruby
142
+ $mp.get ("/v1/customers/CUSTOMER_ID");
143
+ ```
144
+
145
+ * View more Custom checkout related APIs in Developers Site
146
+ * Argentina: [https://labs.mercadopago.com.ar/developers](https://labs.mercadopago.com.ar/developers)
147
+ * Brazil: [https://labs.mercadopago.com.br/developers](https://labs.mercadopago.com.br/developers)
148
+ * Mexico: [https://labs.mercadopago.com.mx/developers](https://labs.mercadopago.com.mx/developers)
149
+ * Venezuela: [https://labs.mercadopago.com.ve/developers](https://labs.mercadopago.com.ve/developers)
150
+ * Colombia: [https://labs.mercadopago.com.co/developers](https://labs.mercadopago.com.co/developers)
151
+
152
+ <a name="generic-methods"></a>
153
+ ## Generic methods
154
+ You can access any other resource from the MercadoPago API using the generic methods:
155
+
156
+ ```ruby
157
+ // Get a resource, with optional URL params. Also you can disable authentication for public APIs
158
+ $mp.get ("/resource/uri", [params], [authenticate=true])
159
+
160
+ // Create a resource with "data" and optional URL params.
161
+ $mp.post ("/resource/uri", data, [params])
162
+
163
+ // Update a resource with "data" and optional URL params.
164
+ $mp.put ("/resource/uri", data, [params])
165
+
166
+ // Delete a resource with optional URL params.
167
+ $mp.delete ("/resource/uri", [params])
168
+ ```
169
+
170
+ For example, if you want to get the Sites list (no params and no authentication):
171
+
172
+ ```ruby
173
+ $sites = $mp.get ("/sites", null, false)
174
+
175
+ puts $sites
176
+ ```
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- require 'bundler/gem_tasks'
2
1
  require 'rake/testtask'
3
2
 
4
3
  Rake::TestTask.new do |t|
@@ -7,7 +7,7 @@ class Button
7
7
 
8
8
  def call(env)
9
9
  mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
10
- preferenceData = Hash["items" => Array(Hash["title"=>"sdk-ruby", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"])]
10
+ preferenceData = {"items" => ["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
11
11
  preference = mp.create_preference(preferenceData)
12
12
 
13
13
  html = '<!doctype html>
@@ -7,7 +7,7 @@ class Button
7
7
 
8
8
  def call(env)
9
9
  mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
10
- preapprovalPayment = Hash[
10
+ preapprovalPayment_data = Hash[
11
11
  "payer_email" => "my_customer@my_site.com",
12
12
  "back_url" => "http://www.my_site.com",
13
13
  "reason" => "Monthly subscription to premium package",
@@ -22,7 +22,7 @@ class Button
22
22
  ]
23
23
  ]
24
24
 
25
- preapprovalPaymentResult = mp.create_preference(preapprovalPayment)
25
+ preapprovalPayment = mp.create_preapproval_payment(preapprovalPayment_data)
26
26
 
27
27
  html = '<!doctype html>
28
28
  <html>
@@ -30,7 +30,7 @@ class Button
30
30
  <title>MercadoPago SDK - Create Preapproval Payment and Show Subscription Example</title>
31
31
  </head>
32
32
  <body>
33
- <a href="' + preapprovalPaymentResult['response']['init_point'] + '" name="MP-Checkout" class="orange-ar-m-sq-arall">Pay</a>
33
+ <a href="' + preapprovalPayment['response']['init_point'] + '" name="MP-Checkout" class="orange-ar-m-sq-arall">Pay</a>
34
34
  <script type="text/javascript" src="http://mp-tools.mlstatic.com/buttons/render.js"></script>
35
35
  </body>
36
36
  </html>'
@@ -10,12 +10,19 @@ require 'uri'
10
10
  require 'net/https'
11
11
  require 'yaml'
12
12
  require File.dirname(__FILE__) + '/version'
13
+ require 'ssl_options_patch'
13
14
 
14
15
  class MercadoPago
15
- def initialize(client_id, client_secret, debug_logger=nil)
16
- @client_id = client_id
17
- @client_secret = client_secret
18
- @rest_client = RestClient.new(debug_logger)
16
+ def initialize(*args)
17
+ if args.size < 1 or args.size > 2
18
+ raise "Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN"
19
+ end
20
+
21
+ @client_id = args.at(0) if args.size == 2
22
+ @client_secret = args.at(1) if args.size == 2
23
+ @ll_access_token = args.at(0) if args.size == 1
24
+
25
+ @rest_client = RestClient.new()
19
26
  @sandbox = false
20
27
  end
21
28
 
@@ -33,19 +40,23 @@ class MercadoPago
33
40
 
34
41
  # Get Access Token for API use
35
42
  def get_access_token
36
- app_client_values = {
37
- 'grant_type' => 'client_credentials',
38
- 'client_id' => @client_id,
39
- 'client_secret' => @client_secret
40
- }
43
+ if @ll_access_token
44
+ @ll_access_token
45
+ else
46
+ app_client_values = {
47
+ 'grant_type' => 'client_credentials',
48
+ 'client_id' => @client_id,
49
+ 'client_secret' => @client_secret
50
+ }
41
51
 
42
- @access_data = @rest_client.post("/oauth/token", build_query(app_client_values), RestClient::MIME_FORM)
52
+ @access_data = @rest_client.post("/oauth/token", build_query(app_client_values), RestClient::MIME_FORM)
43
53
 
44
- if @access_data['status'] == "200"
45
- @access_data = @access_data["response"]
46
- @access_data['access_token']
47
- else
48
- raise @access_data.inspect
54
+ if @access_data['status'] == "200"
55
+ @access_data = @access_data["response"]
56
+ @access_data['access_token']
57
+ else
58
+ raise @access_data.inspect
59
+ end
49
60
  end
50
61
  end
51
62
 
@@ -184,6 +195,92 @@ class MercadoPago
184
195
  @rest_client.get("/preapproval/" + id + "?access_token=" + access_token)
185
196
  end
186
197
 
198
+ # Generic resource get
199
+ def get(uri, params = nil, authenticate = true)
200
+ if not params.class == Hash
201
+ params = Hash.new
202
+ end
203
+
204
+ if authenticate
205
+ begin
206
+ access_token = get_access_token
207
+ rescue => e
208
+ return e.message
209
+ end
210
+
211
+ params["access_token"] = access_token
212
+ end
213
+
214
+ if not params.empty?
215
+ uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
216
+ end
217
+
218
+ @rest_client.get(uri)
219
+ end
220
+
221
+ # Generic resource post
222
+ def post(uri, data, params = nil)
223
+ if not params.class == Hash
224
+ params = Hash.new
225
+ end
226
+
227
+ begin
228
+ access_token = get_access_token
229
+ rescue => e
230
+ return e.message
231
+ end
232
+
233
+ params["access_token"] = access_token
234
+
235
+ if not params.empty?
236
+ uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
237
+ end
238
+
239
+ @rest_client.post(uri, data)
240
+ end
241
+
242
+ # Generic resource put
243
+ def put(uri, data, params = nil)
244
+ if not params.class == Hash
245
+ params = Hash.new
246
+ end
247
+
248
+ begin
249
+ access_token = get_access_token
250
+ rescue => e
251
+ return e.message
252
+ end
253
+
254
+ params["access_token"] = access_token
255
+
256
+ if not params.empty?
257
+ uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
258
+ end
259
+
260
+ @rest_client.put(uri, data)
261
+ end
262
+
263
+ # Generic resource delete
264
+ def delete(uri, params = nil)
265
+ if not params.class == Hash
266
+ params = Hash.new
267
+ end
268
+
269
+ begin
270
+ access_token = get_access_token
271
+ rescue => e
272
+ return e.message
273
+ end
274
+
275
+ params["access_token"] = access_token
276
+
277
+ if not params.empty?
278
+ uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
279
+ end
280
+
281
+ @rest_client.delete(uri)
282
+ end
283
+
187
284
  def build_query(params)
188
285
  URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
189
286
  end
@@ -194,16 +291,14 @@ class MercadoPago
194
291
 
195
292
  MIME_JSON = 'application/json'
196
293
  MIME_FORM = 'application/x-www-form-urlencoded'
197
- API_BASE_URL = URI.parse('https://api.mercadolibre.com')
294
+ API_BASE_URL = URI.parse('https://api.mercadopago.com')
198
295
 
199
296
  def initialize(debug_logger=nil)
200
297
  @http = Net::HTTP.new(API_BASE_URL.host, API_BASE_URL.port)
201
298
 
202
299
  if API_BASE_URL.scheme == "https" # enable SSL/TLS
203
300
  @http.use_ssl = true
204
- @http.ssl_version = :SSLv3 if @http.respond_to? :ssl_version
205
- @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
206
- @http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
301
+ @http.ssl_options = OpenSSL::SSL::OP_NO_SSLv3 # explicitly tell OpenSSL not to use SSL3
207
302
  end
208
303
 
209
304
  @http.set_debug_output debug_logger if debug_logger
@@ -243,5 +338,9 @@ class MercadoPago
243
338
  def put(uri, data = nil, content_type=MIME_JSON)
244
339
  exec("PUT", uri, data, content_type)
245
340
  end
341
+
342
+ def delete(uri, content_type=MIME_JSON)
343
+ exec("DELETE", uri, nil, content_type)
344
+ end
246
345
  end
247
346
  end