mercadopago 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,29 +1,33 @@
1
1
  MercadoPago Gem
2
2
  ===============
3
3
 
4
- This is a Ruby client for all the services offered by [MercadoPago](http://www.mercadopago.com).
4
+ This is a Ruby client for all the services offered by [MercadoPago](http://www.mercadopago.com).
5
5
 
6
6
  You should read the MercadoPago API documentation before you use this gem. This gem works with hashes and only deals with requests/responses. That's why you will need an understanding of their services.
7
7
 
8
- You can read the documentation of the MercadoPago API here:
8
+ You can read the documentation of the MercadoPago API here:
9
9
  * Portuguese: https://developers.mercadopago.com/integracao-checkout
10
- * Spanish: https://developers.mercadopago.com/integracion-checkout
10
+ * Spanish: https://developers.mercadopago.com/integracion-checkout
11
+
12
+
11
13
 
12
14
  Installation
13
15
  ------------
14
16
 
17
+ mercadopago 2.0.0 needs Ruby 1.9. Version 1.0.2 runs fine with Ruby 1.8.
18
+
15
19
  To install the last version of the gem:
16
-
20
+
17
21
  gem install mercadopago
18
-
22
+
19
23
  If you are using bundler, add this to your Gemfile:
20
-
24
+
21
25
  gem 'mercadopago'
22
-
26
+
23
27
  Access Credentials
24
28
  ------------------
25
29
 
26
- To use this gem, you will need the client_id and client_secret for a MercadoPago account.
30
+ To use this gem, you will need the client_id and client_secret for a MercadoPago account.
27
31
 
28
32
  In any case, this gem will not store this information. In order to find out your MercadoPago credentials, you can go here:
29
33
 
@@ -36,176 +40,276 @@ How to use
36
40
  ### Client creation
37
41
 
38
42
  The first thing to do is create a client. The client will authenticate with MercadoPago and will allow you to interact with the MercadoPago API.
39
-
43
+
40
44
  # Use your credentials
41
45
  client_id = '1234'
42
46
  client_secret = 'abcdefghijklmnopqrstuvwxyz'
43
-
47
+
44
48
  mp_client = MercadoPago::Client.new(client_id, client_secret)
45
-
49
+
46
50
  If any error ocurred while authenticating with MercadoPago, an AccessError will be raised. If nothing goes wrong, no errors are raised and you are ready to use the API.
47
51
 
48
52
  ### Payment Creation
49
53
 
50
54
  Your request will need a hash to explain what the payment is for. For example:
51
-
55
+
52
56
  data = {
53
- "external_reference" => "OPERATION-ID-1234",
54
- "items" => [
57
+ external_reference: "OPERATION-ID-1234",
58
+ items: [
55
59
  {
56
- "id" => "Código 123",
57
- "title" => "Example T-Shirt",
58
- "description" => "Red XL T-Shirt",
59
- "quantity" => 1,
60
- "unit_price" => 10.50,
61
- "currency_id" => "BRL",
62
- "picture_url" => "http://www.site.com/image/123.png"
60
+ id: "Código 123",
61
+ title: "Example T-Shirt",
62
+ description: "Red XL T-Shirt",
63
+ quantity: 1,
64
+ unit_price: 10.50,
65
+ currency_id: "BRL",
66
+ picture_url: "http://www.site.com/image/123.png"
63
67
  }
64
68
  ],
65
- "payer" => {
66
- "name"=> "John",
67
- "surname"=> "Mikel",
68
- "email"=> "buyer@email.com"
69
+ payer: {
70
+ name: "John",
71
+ surname: "Mikel",
72
+ email: "buyer@email.com"
69
73
  },
70
- "back_urls"=> {
71
- "pending"=> "https://www.site.com/pending",
72
- "success"=> "http://www.site.com/success",
73
- "failure"=> "http://www.site.com/failure"
74
+ back_urls: {
75
+ pending: "https://www.site.com/pending",
76
+ success: "http://www.site.com/success",
77
+ failure: "http://www.site.com/failure"
74
78
  }
75
79
  }
76
-
77
- payment = mp_client.create_preference(access_token, data)
78
-
80
+
81
+ payment = mp_client.create_preference(data)
82
+
79
83
  If everything worked out alright, you will get a response like this:
80
-
84
+
81
85
  {
82
86
  "payment_methods" => {},
83
- "init_point" => "https://www.mercadopago.com/mlb/checkout/pay?pref_id=abcdefgh-9999-9999-ab99-999999999999",
84
- "collector_id" => 123456789,
87
+ "init_point" => "https://www.mercadopago.com/mlb/checkout/pay?pref_id=abcdefgh-9999-9999-ab99-999999999999",
88
+ "collector_id" => 123456789,
85
89
  "back_urls" => {
86
90
  "pending"=> "https://www.site.com/pending",
87
91
  "success"=> "http://www.site.com/success",
88
92
  "failure"=> "http://www.site.com/failure"
89
93
  },
90
94
  "sponsor_id" => nil,
91
- "expiration_date_from" => nil,
92
- "additional_info" => "",
93
- "marketplace_fee" => 0,
94
- "date_created" => "2012-05-07T20:07:52.293-04:00",
95
- "subscription_plan_id" => nil,
96
- "id"=> "abcdefgh-9999-9999-ab99-999999999999",
97
- "expiration_date_to" => nil,
98
- "expires" => false,
99
- "external_reference" => "OPERATION-ID-1234",
95
+ "expiration_date_from" => nil,
96
+ "additional_info" => "",
97
+ "marketplace_fee" => 0,
98
+ "date_created" => "2012-05-07T20:07:52.293-04:00",
99
+ "subscription_plan_id" => nil,
100
+ "id" => "abcdefgh-9999-9999-ab99-999999999999",
101
+ "expiration_date_to" => nil,
102
+ "expires" => false,
103
+ "external_reference" => "OPERATION-ID-1234",
100
104
  "payer" => {
101
- "email" => "buyer@email.com",
102
- "name" => "John",
105
+ "email" => "buyer@email.com",
106
+ "name" => "John",
103
107
  "surname" => "Mikel"
104
108
  },
105
109
  "items" => [
106
110
  {
107
- "id" => "Código 123",
111
+ "id" => "Código 123",
108
112
  "currency_id" => "BRL",
109
- "title" => "Example T-Shirt",
113
+ "title" => "Example T-Shirt",
110
114
  "description" => "Red XL T-Shirt",
111
115
  "picture_url" => "http://www.site.com.br/image/123.png",
112
- "quantity" => 1,
113
- "unit_price" => 10.50
116
+ "quantity" => 1,
117
+ "unit_price" => 10.50
114
118
  }
115
119
  ],
116
- "client_id" => "963",
120
+ "client_id" => "963",
117
121
  "marketplace" => "NONE"
118
122
  }
119
-
123
+
120
124
  ### Payment Status Verification
121
125
 
122
126
  To check the payment status you will need the payment ID. Only then you can call the [MercadoPago IPN](https://developers.mercadopago.com/api-ipn).
123
-
127
+
124
128
  # Use the payment ID received on the IPN.
125
129
  payment_id = '987654321'
126
-
127
- notification = mp_client.notification(access_token, payment_id)
128
-
130
+
131
+ notification = mp_client.notification(payment_id)
132
+
129
133
  You will get a response like this one:
130
-
134
+
131
135
  {
132
136
  "collection" => {
133
- "id" => 987654321,
134
- "site_id" => "MLB",
135
- "operation_type" => "regular_payment",
136
- "order_id" => nil,
137
- "external_reference" => "OPERATION-ID-1234",
138
- "status" => "approved",
139
- "status_detail" => "approved",
140
- "payment_type" => "credit_card",
141
- "date_created" => "2012-05-05T14:22:43Z",
142
- "last_modified" => "2012-05-05T14:35:13Z",
143
- "date_approved" => "2012-05-05T14:22:43Z",
144
- "money_release_date" => "2012-05-19T14:22:43Z",
145
- "currency_id" => "BRL",
146
- "transaction_amount" => 10.50,
147
- "shipping_cost" => 0,
148
- "total_paid_amount" => 10.50,
149
- "finance_charge" => 0,
137
+ "id" => 987654321,
138
+ "site_id" => "MLB",
139
+ "operation_type" => "regular_payment",
140
+ "order_id" => nil,
141
+ "external_reference" => "OPERATION-ID-1234",
142
+ "status" => "approved",
143
+ "status_detail" => "approved",
144
+ "payment_type" => "credit_card",
145
+ "date_created" => "2012-05-05T14:22:43Z",
146
+ "last_modified" => "2012-05-05T14:35:13Z",
147
+ "date_approved" => "2012-05-05T14:22:43Z",
148
+ "money_release_date" => "2012-05-19T14:22:43Z",
149
+ "currency_id" => "BRL",
150
+ "transaction_amount" => 10.50,
151
+ "shipping_cost" => 0,
152
+ "total_paid_amount" => 10.50,
153
+ "finance_charge" => 0,
150
154
  "net_received_amount" => 0,
151
- "marketplace" => "NONE",
152
- "marketplace_fee" => nil,
153
- "reason" => "Example T-Shirt",
155
+ "marketplace" => "NONE",
156
+ "marketplace_fee" => nil,
157
+ "reason" => "Example T-Shirt",
154
158
  "payer" => {
155
- "id" => 543219876,
156
- "first_name" => "John",
157
- "last_name" => "Mikel",
158
- "nickname" => "JOHNMIKEL",
159
+ "id" => 543219876,
160
+ "first_name" => "John",
161
+ "last_name" => "Mikel",
162
+ "nickname" => "JOHNMIKEL",
159
163
  "phone" => {
160
164
  "area_code" => nil,
161
- "number" => "551122334455",
165
+ "number" => "551122334455",
162
166
  "extension" => nil
163
167
  },
164
168
  "email" => "buyer@email.com",
165
169
  "identification" => {
166
- "type" => nil,
167
- "number" => nil
170
+ "type" => nil,
171
+ "number" => nil
168
172
  }
169
173
  },
170
174
  "collector" => {
171
- "id" => 123456789,
172
- "first_name" => "Bill",
173
- "last_name" => "Receiver",
175
+ "id" => 123456789,
176
+ "first_name" => "Bill",
177
+ "last_name" => "Receiver",
174
178
  "phone" => {
175
179
  "area_code" => nil,
176
- "number" => "1122334455",
180
+ "number" => "1122334455",
177
181
  "extension" => nil
178
182
  },
179
- "email" => "receiver@email.com",
180
- "nickname" => "BILLRECEIVER"
183
+ "email" => "receiver@email.com",
184
+ "nickname" => "BILLRECEIVER"
181
185
  }
182
186
  }
183
- }
184
-
187
+ }
188
+
189
+ ### Search in the collections
190
+
191
+ To search over the collections (payments that you may be processing) you need to create a hash with the params that you want to search for an send it to the search method of the client instance.
192
+
193
+ # Create a hash to search for
194
+ search_hash = { external_reference: 'OPERATION-ID-1234' }
195
+
196
+ search = mp_client.search(search_hash)
197
+
198
+ You will get a response like this one:
199
+
200
+ {
201
+ "results"=>[
202
+ {
203
+ "collection" => {
204
+ "marketplace" =>"NONE",
205
+ "sponsor_id" =>nil,
206
+ "status" => "approved",
207
+ "status_detail" => "approved",
208
+ "payer" => {
209
+ "id" => 543219876,
210
+ "first_name" => "John",
211
+ "last_name" => "Mikel",
212
+ "nickname" => "JOHNMIKEL",
213
+ "phone" => {
214
+ "area_code" => nil,
215
+ "number" => "551122334455",
216
+ "extension" => nil
217
+ },
218
+ "email" => "buyer@email.com",
219
+ "identification" => {
220
+ "type" => nil,
221
+ "number" => nil
222
+ }
223
+ },
224
+ "currency_id" => "BRL",
225
+ "external_reference" => "OPERATION-ID-1234",
226
+ "transaction_amount" => 10.50,
227
+ "shipping_cost" => 0,
228
+ "total_paid_amount" => 10.50,
229
+ "id" => 987654321,
230
+ "status_code" => nil,
231
+ "reason" => "Example T-Shirt",
232
+ "collector_id" => 99678614,
233
+ "date_created" => "2012-05-05T14:22:43Z",
234
+ "last_modified" => "2012-05-05T14:35:13Z",
235
+ "date_approved" => "2012-05-05T14:22:43Z",
236
+ "money_release_date" => "2012-05-19T14:22:43Z",
237
+ "released" => "yes",
238
+ "operation_type" => "regular_payment",
239
+ "payment_type" => "credit_card",
240
+ "site_id" => "MLB"
241
+ }
242
+ }
243
+ ],
244
+ "paging" => {
245
+ "total" => 1,
246
+ "limit" => 30,
247
+ "offset" => 0
248
+ }
249
+ }
250
+
251
+ And the parameters thay could be used in the search hash are:
252
+
253
+ id: Payment identifier
254
+ site_id: Country identifier: Argentina: MLA; Brasil: MLB.
255
+ date_created: Creation date. Ej: range=date_created&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
256
+ date_approved: Approval date. Ej: range=date_approved&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
257
+ last_modified: Last modified date. Ej: range=last_modified&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
258
+ money_release_date: Date of the payment's release. Ej: range=money_release_date&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
259
+ payer_id: Buyer's identifier.
260
+ reason: Description of what's being payed.
261
+ transaction_amount: Amount of the transaction.
262
+ currency_id: Currency type. Argentina: ARS (peso argentino); USD (Dólar estadounidense); Brasil: BRL (Real).
263
+ external_reference: Field used by the seller as aditional reference.
264
+ mercadopago_fee: MercadoPago's comission fee.
265
+ net_received_amount: Amount payable to the seller without mercadopago_fee.
266
+ total_paid_amount: Obtained by adding: transaction_amount, shipping_cost and the amount payed by the buyer (including credit card's financing).
267
+ shipping_cost: Shipping cost.
268
+ status:
269
+ pending: The payment process is incomplete.
270
+ approved: The payment has been credited.
271
+ in_process: The payment is under review.
272
+ rejected: The payment has been rejected.
273
+ cancelled: The payment is canceled after timeout or by either party.
274
+ refunded: The payment has been refunded.
275
+ in_mediation: The payment is in dispute.
276
+ status_detail: Payment status detail.
277
+ released: When the amount is available or not. Possible values are yes or no.
278
+ operation_type:
279
+ regular_payment: A payment.
280
+ money_transfer: A money wire.
281
+ recurring_payment: Active subscription recurring payment.
282
+ subscription_payment: Subscription fee.
283
+
185
284
  ### Errors
186
285
 
187
286
  Errors will also be hashes with status code, message and error key.
188
287
 
189
288
  For example, if you request payment method status for an invalid operation, you will see something like this:
190
-
289
+
191
290
  {
192
- "message" => "Resource not found",
193
- "error" => "not_found",
194
- "status" => 404,
195
- "cause" => []
291
+ "message" => "Resource not found",
292
+ "error" => "not_found",
293
+ "status" => 404,
294
+ "cause" => []
196
295
  }
197
-
296
+
198
297
  ### Tests
199
298
 
200
299
  This gem has tests for a few methods. To check if it is working properly, just run:
201
-
300
+
202
301
  rake test
203
-
302
+
204
303
  Changelog
205
304
  ---------
206
305
 
306
+ 2.0.0 (thanks leanucci and cavi21)
307
+
308
+ Implemented basic access to the collection search method. Changed the test credentials. Using Ruby 1.9 hash format. Added documentation for collection search.
309
+
207
310
  1.0.2
208
- Changed documentation according to the new client intercace, added a notification method to the client and refactored the tests.
311
+
312
+ Changed documentation according to the new client intercace, added a notification method to the client and added a new test.
209
313
 
210
314
  1.0.1 (thanks etagwerker)
211
315
 
@@ -13,21 +13,17 @@ module MercadoPago
13
13
  # - client_secret
14
14
  #
15
15
  def self.access_token(client_id, client_secret)
16
-
17
- payload = { :grant_type => 'client_credentials', :client_id => client_id, :client_secret => client_secret }
18
- headers = { :content_type => 'application/x-www-form-urlencoded', :accept => 'application/json' }
16
+ payload = { grant_type: 'client_credentials', client_id: client_id, client_secret: client_secret }
17
+ headers = { content_type: 'application/x-www-form-urlencoded', accept: 'application/json' }
19
18
 
20
19
  MercadoPago::Request.wrap_post('/oauth/token', payload, headers)
21
-
22
20
  end
23
21
 
24
22
  #
25
23
  # TODO
26
24
  #
27
25
  def refresh_access_token
28
-
29
26
  # TODO
30
-
31
27
  end
32
28
 
33
29
  end
@@ -13,12 +13,10 @@ module MercadoPago
13
13
  # - data: a hash of preferences that will be trasmitted to checkout API.
14
14
  #
15
15
  def self.create_preference(access_token, data)
16
-
17
16
  payload = JSON.generate(data)
18
- headers = { :content_type => 'application/json', :accept => 'application/json' }
17
+ headers = { content_type: 'application/json', accept: 'application/json' }
19
18
 
20
19
  MercadoPago::Request.wrap_post("/checkout/preferences?access_token=#{access_token}", payload, headers)
21
-
22
20
  end
23
21
 
24
22
  # Returns the hash with the details of certain payment preference.
@@ -27,19 +25,15 @@ module MercadoPago
27
25
  # - preference_id: the payment preference ID
28
26
  #
29
27
  def self.get_preference(access_token, preference_id)
30
-
31
- headers = { :accept => 'application/json' }
28
+ headers = { accept: 'application/json' }
32
29
  MercadoPago::Request.wrap_get("/checkout/preferences/#{preference_id}?access_token=#{access_token}")
33
-
34
30
  end
35
31
 
36
32
  #
37
33
  # TODO
38
34
  #
39
35
  def self.update_preference
40
-
41
36
  # TODO
42
-
43
37
  end
44
38
 
45
39
  end
@@ -1,8 +1,8 @@
1
1
  module MercadoPago
2
-
2
+
3
3
  class AccessError < Exception
4
4
  end
5
-
5
+
6
6
  #
7
7
  # You can create a Client object to interact with a MercadoPago
8
8
  # account through the API.
@@ -14,42 +14,46 @@ module MercadoPago
14
14
  # Usage example:
15
15
  #
16
16
  # mp_client = MercadoPago::Client.new(client_id, client_secret)
17
+ #
17
18
  # mp_client.create_preference(data)
19
+ # mp_client.get_preference(preference_id)
20
+ # mp_client.notification(payment_id)
21
+ # mp_client.search(data)
18
22
  #
19
23
  class Client
24
+
20
25
  attr_reader :token
21
-
26
+
22
27
  #
23
- # Creates an instance and stores the
24
- # access_token to make calls to the
28
+ # Creates an instance and stores the access_token to make calls to the
25
29
  # MercadoPago API.
26
30
  #
27
31
  def initialize(client_id, client_secret)
28
32
  response = MercadoPago::Authentication.access_token(client_id, client_secret)
29
-
30
- unless @token = response["access_token"]
31
- raise AccessError, response["message"]
33
+
34
+ unless @token = response['access_token']
35
+ raise AccessError, response['message']
32
36
  end
33
37
  end
34
-
38
+
35
39
  #
36
40
  # Creates a payment preference.
37
41
  #
38
- # - data: contains the data according to the payment preference will be created.
42
+ # - data: contains the data according to the payment preference that will be created.
39
43
  #
40
44
  def create_preference(data)
41
45
  MercadoPago::Checkout.create_preference(@token, data)
42
46
  end
43
-
47
+
44
48
  #
45
49
  # Returns the payment preference.
46
50
  #
47
51
  # - preference_id: the id of the payment preference that will be retrieved.
48
52
  #
49
53
  def get_preference(preference_id)
50
- MercadoPago::Checkout.get_preference(@token, preference_id)
54
+ MercadoPago::Checkout.get_preference(@token, preference_id)
51
55
  end
52
-
56
+
53
57
  #
54
58
  # Retrieves the latest information about a payment.
55
59
  #
@@ -58,7 +62,16 @@ module MercadoPago
58
62
  def notification(payment_id)
59
63
  MercadoPago::Collection.notification(@token, payment_id)
60
64
  end
61
-
65
+
66
+ #
67
+ # Searches for collections that matches some of the search hash criteria.
68
+ #
69
+ # - search_hash: the search hash to find collections.
70
+ #
71
+ def search(search_hash)
72
+ MercadoPago::Collection.search(@token, search_hash)
73
+ end
74
+
62
75
  end
63
-
76
+
64
77
  end
@@ -10,9 +10,81 @@ module MercadoPago
10
10
  # - payment_id: the id of the payment to be checked.
11
11
  #
12
12
  def self.notification(access_token, payment_id)
13
+ MercadoPago::Request.wrap_get("/collections/notifications/#{payment_id}?access_token=#{access_token}", { accept: 'application/json' })
14
+ end
13
15
 
14
- MercadoPago::Request.wrap_get("/collections/notifications/#{payment_id}?access_token=#{access_token}", { :accept => 'application/json' })
16
+ #
17
+ # Receives an access_token and a search_hash and returns matching payments, according to the search params.
18
+ #
19
+ # - access_token: an access_token of the MercadoPago account associated with the payment to be checked.
20
+ # - search_hash: querystring in hash format.
21
+ #
22
+ # == Search parameter valid keys:
23
+ # id::
24
+ # Payment identifier.
25
+ # site_id::
26
+ # Country identifier: Argentina: MLA; Brasil: MLB.
27
+ # date_created::
28
+ # Creation date. Ej: range=date_created&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
29
+ # date_approved::
30
+ # Approval date. Ej: range=date_approved&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
31
+ # last_modified::
32
+ # Last modified date. Ej: range=last_modified&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
33
+ # money_release_date::
34
+ # Date of the payment's release. Ej: range=money_release_date&begin_date=NOW-1DAYS&end_date=NOW (Ver ISO-8601).
35
+ # payer_id::
36
+ # Buyer's identifier.
37
+ # reason::
38
+ # Description of what's being payed.
39
+ # transaction_amount::
40
+ # Amount of the transaction.
41
+ # currency_id::
42
+ # Currency type. Argentina: ARS (peso argentino);' USD (Dólar estadounidense); Brasil: BRL (Real).
43
+ # external_reference::
44
+ # Field used by the seller as aditional reference.
45
+ # mercadopago_fee::
46
+ # MercadoPago's comission fee.
47
+ # net_received_amount::
48
+ # Amount payable to the seller without mercadopago_fee.
49
+ # total_paid_amount::
50
+ # Obtained by adding: transaction_amount, shipping_cost and the amount payed by the buyer (including credit card's financing).
51
+ # shipping_cost::
52
+ # Shipping cost.
53
+ # status::
54
+ # === Status of the payment:
55
+ # pending::
56
+ # The payment process is incomplete.
57
+ # approved::
58
+ # The payment has been credited.
59
+ # in_process::
60
+ # The payment is under review.
61
+ # rejected::
62
+ # The payment has been rejected.
63
+ # cancelled::
64
+ # The payment is canceled after timeout or by either party.
65
+ # refunded::
66
+ # The payment has been refunded.
67
+ # in_mediation::
68
+ # The payment is in dispute.
69
+ # status_detail::
70
+ # Payment status detail.
71
+ # released::
72
+ # Wether the amount is available or not. Possible values are yes or no.
73
+ # operation_type::
74
+ # === Type of operation:
75
+ # regular_payment::
76
+ # A payment.
77
+ # money_transfer::
78
+ # A money wire.
79
+ # recurring_payment::
80
+ # Active subscription recurring payment.
81
+ # subscription_payment::
82
+ # Subscription fee.
83
+ #
84
+ def self.search(access_token, search_hash = {})
85
+ query = search_hash.map { |e| e.join('=') }.join('&')
15
86
 
87
+ MercadoPago::Request.wrap_get("/collections/search?access_token=#{access_token}&#{query}", { accept: 'application/json' })
16
88
  end
17
89
 
18
90
  end
@@ -5,6 +5,9 @@ module MercadoPago
5
5
 
6
6
  module Request
7
7
 
8
+ class ClientError < Exception
9
+ end
10
+
8
11
  #
9
12
  # This URL is the base for all API calls.
10
13
  #
@@ -18,10 +21,8 @@ module MercadoPago
18
21
  # - headers: the headers to be transmitted over the HTTP request.
19
22
  #
20
23
  def self.wrap_post(path, payload, headers = {})
21
-
22
24
  raise ClientError('No data given') if payload.nil? or payload.empty?
23
25
  make_request(:post, path, payload, headers)
24
-
25
26
  end
26
27
 
27
28
  #
@@ -31,9 +32,7 @@ module MercadoPago
31
32
  # - headers: the headers to be transmitted over the HTTP request.
32
33
  #
33
34
  def self.wrap_get(path, headers = {})
34
-
35
35
  make_request(:get, path, nil, headers)
36
-
37
36
  end
38
37
 
39
38
  #
@@ -45,19 +44,12 @@ module MercadoPago
45
44
  # - headers: the headers to be transmitted over the HTTP request.
46
45
  #
47
46
  def self.make_request(type, path, payload = nil, headers = {})
47
+ args = [type, "#{MERCADOPAGO_URL}#{path}", payload, headers].compact
48
+ response = RestClient.send *args
48
49
 
49
- begin
50
- args = [type, "#{MERCADOPAGO_URL}#{path}", payload, headers].compact
51
- response = RestClient.send *args
52
-
53
- JSON.load(response)
54
- rescue Exception => e
55
- JSON.load(e.response)
56
- end
57
-
58
- end
59
-
60
- class ClientError < Exception
50
+ JSON.load(response)
51
+ rescue Exception => e
52
+ JSON.load(e.response)
61
53
  end
62
54
 
63
55
  end
@@ -1,3 +1,3 @@
1
1
  module MercadoPago
2
- VERSION = "1.0.2"
2
+ VERSION = "2.0.0"
3
3
  end
data/mercadopago.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'json', '>= 1.4.6'
23
23
  s.add_dependency 'rest-client', '1.6.7'
24
24
  s.add_development_dependency 'pry'
25
+ s.add_development_dependency 'debugger'
25
26
  end
@@ -4,101 +4,107 @@ require 'minitest/autorun'
4
4
  require 'mercadopago'
5
5
 
6
6
  class TestMercadoPago < MiniTest::Unit::TestCase
7
-
7
+
8
8
  #
9
9
  # Valid credentials to be used in the tests.
10
10
  #
11
- CREDENTIALS = {
12
- :client_id => '8897',
13
- :client_secret => 'PC2MoR6baSu75xZnkhLRHoyelnpLkNbh'
14
- }
15
-
11
+ CREDENTIALS = { client_id: '3962917649351233', client_secret: 'rp7Ec38haoig7zWQXekXMiqA068eS376' }
12
+
16
13
  #
17
14
  # Example payment request.
18
15
  #
19
16
  PAYMENT_REQUEST = {
20
- "external_reference" => "OPERATION-ID-1234",
21
- "items" => [
17
+ external_reference: 'OPERATION-ID-1234',
18
+ items: [
22
19
  {
23
- "id" => "Código 123",
24
- "title" => "Example T-Shirt",
25
- "description" => "Red XL T-Shirt",
26
- "quantity" => 1,
27
- "unit_price" => 10.50,
28
- "currency_id" => "ARS",
29
- "picture_url" => "http://s3.amazonaws.com/ombu_store_production/images/products/1375/product/l-idiot-savant-rare-device-tee.jpeg"
20
+ id: 'Código 123',
21
+ title: 'Example T-Shirt',
22
+ description: 'Red XL T-Shirt',
23
+ quantity: 1,
24
+ unit_price: 0.50,
25
+ currency_id: 'BRL',
26
+ picture_url: 'http://s3.amazonaws.com/ombu_store_production/images/products/1375/product/l-idiot-savant-rare-device-tee.jpeg'
30
27
  }
31
28
  ],
32
- "payer" => {
33
- "name" => "John",
34
- "surname" => "Mikel",
35
- "email" => "buyer@email.com"
29
+ payer: {
30
+ name: 'John',
31
+ surname: 'Mikel',
32
+ email: 'buyer@email.com'
36
33
  },
37
- "back_urls" => {
38
- "pending" => "https://www.site.com/pending",
39
- "success" => "http://www.site.com/success",
40
- "failure" => "http://www.site.com/failure"
34
+ back_urls: {
35
+ pending: 'https://www.site.com/pending',
36
+ success: 'http://www.site.com/success',
37
+ failure: 'http://www.site.com/failure'
41
38
  }
42
39
  }
43
-
40
+
44
41
  # With a valid client id and secret (test account)
45
42
  def test_that_authentication_returns_access_token
46
43
  response = MercadoPago::Authentication.access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
47
- assert response["access_token"]
44
+ assert response['access_token']
48
45
  end
49
-
46
+
50
47
  # Using fake client id and client secret
51
48
  def test_that_authentication_fails_with_wrong_parameters
52
49
  response = MercadoPago::Authentication.access_token('fake_client_id', 'fake_client_secret')
53
- assert_nil response["access_token"]
54
- assert_equal "invalid_client", response["error"]
50
+ assert_nil response['access_token']
51
+ assert_equal "invalid_client", response['error']
55
52
  end
56
-
53
+
57
54
  # Using fake token
58
55
  def test_that_request_fails_with_wrong_token
59
56
  response = MercadoPago::Checkout.create_preference('fake_token', {})
60
- assert_equal "Malformed access_token: fake_token", response["message"]
61
- assert_equal "bad_request", response["error"]
57
+ assert_equal 'Malformed access_token: fake_token', response['message']
58
+ assert_equal 'bad_request', response['error']
62
59
  end
63
-
60
+
64
61
  def test_that_client_initializes_okay_with_valid_details
65
62
  mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
66
63
  assert mp_client.token
67
64
  end
68
-
65
+
69
66
  def test_that_client_fails_with_wrong_details
70
67
  assert_raises(MercadoPago::AccessError) do
71
68
  mp_client = MercadoPago::Client.new('fake_client_id', 'fake_client_secret')
72
69
  end
73
70
  end
74
-
71
+
75
72
  def test_that_client_can_create_payment_preference
76
73
  mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
77
74
  response = mp_client.create_preference(PAYMENT_REQUEST)
78
- assert response["init_point"]
75
+ assert response['init_point']
79
76
  end
80
-
77
+
81
78
  def test_that_client_can_get_preference
82
79
  mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
83
-
80
+
84
81
  response = mp_client.create_preference(PAYMENT_REQUEST)
85
- assert pref_id = response["id"]
86
-
82
+ assert pref_id = response['id']
83
+
87
84
  response = mp_client.get_preference(pref_id)
88
- assert_equal "https://www.mercadopago.com/mla/checkout/pay?pref_id=#{pref_id}", response["init_point"]
85
+ assert_equal "https://www.mercadopago.com/mlb/checkout/pay?pref_id=#{pref_id}", response['init_point']
89
86
  end
90
-
87
+
91
88
  def test_that_client_can_get_notification
92
- payment_id = '411994180'
93
-
94
- # The payment_id needs to belong to the account whose credentials were used to create the client.
95
- # When we have a payment_id that matches this requirement, uncomment the line bellow and remove the next one.
96
-
97
- # mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
98
- mp_client = MercadoPago::Client.new('7248', 'rYtWLHZhzd2KfHuycnS75ogbWHODPMil')
99
-
89
+ payment_id = 460494008
90
+ mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
91
+
100
92
  response = mp_client.notification(payment_id)
101
93
  assert_equal payment_id, response['collection']['id']
102
94
  end
103
-
104
- end
95
+
96
+ def test_that_client_can_search
97
+ mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
98
+ response = mp_client.search(status: :pending)
99
+
100
+ assert response.has_key?('results')
101
+
102
+ external_reference = 'OPERATION-ID-1234'
103
+ response = mp_client.search(external_reference: external_reference)
104
+ results = response['results']
105
+
106
+ assert_equal 1, results.length
107
+ assert_equal external_reference, results[0]['collection']['external_reference']
108
+ end
109
+
110
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mercadopago
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,12 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-04 00:00:00.000000000 -03:00
14
- default_executable:
13
+ date: 2012-12-21 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: json
18
- requirement: &2152004880 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
19
18
  none: false
20
19
  requirements:
21
20
  - - ! '>='
@@ -23,21 +22,31 @@ dependencies:
23
22
  version: 1.4.6
24
23
  type: :runtime
25
24
  prerelease: false
26
- version_requirements: *2152004880
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 1.4.6
27
31
  - !ruby/object:Gem::Dependency
28
32
  name: rest-client
29
- requirement: &2151872980 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
30
34
  none: false
31
35
  requirements:
32
- - - =
36
+ - - '='
33
37
  - !ruby/object:Gem::Version
34
38
  version: 1.6.7
35
39
  type: :runtime
36
40
  prerelease: false
37
- version_requirements: *2151872980
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.6.7
38
47
  - !ruby/object:Gem::Dependency
39
48
  name: pry
40
- requirement: &2151869700 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
41
50
  none: false
42
51
  requirements:
43
52
  - - ! '>='
@@ -45,7 +54,28 @@ dependencies:
45
54
  version: '0'
46
55
  type: :development
47
56
  prerelease: false
48
- version_requirements: *2151869700
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: debugger
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
49
79
  description: This gem allows developers to use the services available in the MercadoPago
50
80
  API (http://www.mercadopago.com)
51
81
  email:
@@ -67,7 +97,6 @@ files:
67
97
  - lib/mercadopago/version.rb
68
98
  - mercadopago.gemspec
69
99
  - test/test_mercado_pago.rb
70
- has_rdoc: true
71
100
  homepage: https://github.com/kauplus/mercadopago
72
101
  licenses: []
73
102
  post_install_message:
@@ -88,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
117
  version: '0'
89
118
  requirements: []
90
119
  rubyforge_project: mercadopago
91
- rubygems_version: 1.6.2
120
+ rubygems_version: 1.8.24
92
121
  signing_key:
93
122
  specification_version: 3
94
123
  summary: Client for the MercadoPago API