mercadopago 0.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +219 -190
- data/Rakefile +6 -0
- data/lib/mercadopago/authentication.rb +14 -14
- data/lib/mercadopago/checkout.rb +22 -19
- data/lib/mercadopago/client.rb +64 -0
- data/lib/mercadopago/collection.rb +9 -9
- data/lib/mercadopago/request.rb +9 -10
- data/lib/mercadopago/version.rb +2 -2
- data/lib/mercadopago.rb +3 -2
- data/mercadopago.gemspec +7 -6
- data/test/test_mercado_pago.rb +104 -0
- metadata +27 -12
data/README.md
CHANGED
@@ -1,196 +1,225 @@
|
|
1
|
-
MercadoPago
|
2
|
-
|
1
|
+
MercadoPago Gem
|
2
|
+
===============
|
3
3
|
|
4
|
-
|
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
|
-
|
8
|
+
You can read the documentation of the MercadoPago API here:
|
9
|
+
* Portuguese: https://developers.mercadopago.com/integracao-checkout
|
10
|
+
* Spanish: https://developers.mercadopago.com/integracion-checkout
|
9
11
|
|
10
|
-
|
12
|
+
Installation
|
13
|
+
------------
|
14
|
+
|
15
|
+
To install the last version of the gem:
|
16
|
+
|
17
|
+
gem install mercadopago
|
18
|
+
|
19
|
+
If you are using bundler, add this to your Gemfile:
|
20
|
+
|
21
|
+
gem 'mercadopago'
|
22
|
+
|
23
|
+
Access Credentials
|
24
|
+
------------------
|
25
|
+
|
26
|
+
To use this gem, you will need the client_id and client_secret for a MercadoPago account.
|
27
|
+
|
28
|
+
In any case, this gem will not store this information. In order to find out your MercadoPago credentials, you can go here:
|
29
|
+
|
30
|
+
* Brasil: https://www.mercadopago.com/mlb/ferramentas/aplicacoes
|
31
|
+
* Argentina: https://www.mercadopago.com/mla/herramientas/aplicaciones
|
32
|
+
|
33
|
+
How to use
|
11
34
|
----------
|
12
35
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
36
|
+
### Client creation
|
37
|
+
|
38
|
+
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
|
+
|
40
|
+
# Use your credentials
|
41
|
+
client_id = '1234'
|
42
|
+
client_secret = 'abcdefghijklmnopqrstuvwxyz'
|
43
|
+
|
44
|
+
mp_client = MercadoPago::Client.new(client_id, client_secret)
|
45
|
+
|
46
|
+
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
|
+
|
48
|
+
### Payment Creation
|
49
|
+
|
50
|
+
Your request will need a hash to explain what the payment is for. For example:
|
51
|
+
|
52
|
+
data = {
|
53
|
+
"external_reference" => "OPERATION-ID-1234",
|
54
|
+
"items" => [
|
55
|
+
{
|
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"
|
63
|
+
}
|
64
|
+
],
|
65
|
+
"payer" => {
|
66
|
+
"name"=> "John",
|
67
|
+
"surname"=> "Mikel",
|
68
|
+
"email"=> "buyer@email.com"
|
69
|
+
},
|
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
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
payment = mp_client.create_preference(access_token, data)
|
78
|
+
|
79
|
+
If everything worked out alright, you will get a response like this:
|
80
|
+
|
81
|
+
{
|
82
|
+
"payment_methods" => {},
|
83
|
+
"init_point" => "https://www.mercadopago.com/mlb/checkout/pay?pref_id=abcdefgh-9999-9999-ab99-999999999999",
|
84
|
+
"collector_id" => 123456789,
|
85
|
+
"back_urls" => {
|
86
|
+
"pending"=> "https://www.site.com/pending",
|
87
|
+
"success"=> "http://www.site.com/success",
|
88
|
+
"failure"=> "http://www.site.com/failure"
|
89
|
+
},
|
90
|
+
"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",
|
100
|
+
"payer" => {
|
101
|
+
"email" => "buyer@email.com",
|
102
|
+
"name" => "John",
|
103
|
+
"surname" => "Mikel"
|
104
|
+
},
|
105
|
+
"items" => [
|
106
|
+
{
|
107
|
+
"id" => "Código 123",
|
108
|
+
"currency_id" => "BRL",
|
109
|
+
"title" => "Example T-Shirt",
|
110
|
+
"description" => "Red XL T-Shirt",
|
111
|
+
"picture_url" => "http://www.site.com.br/image/123.png",
|
112
|
+
"quantity" => 1,
|
113
|
+
"unit_price" => 10.50
|
114
|
+
}
|
115
|
+
],
|
116
|
+
"client_id" => "963",
|
117
|
+
"marketplace" => "NONE"
|
118
|
+
}
|
119
|
+
|
120
|
+
### Payment Status Verification
|
121
|
+
|
122
|
+
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
|
+
|
124
|
+
# Use the payment ID received on the IPN.
|
125
|
+
payment_id = '987654321'
|
126
|
+
|
127
|
+
notification = mp_client.notification(access_token, payment_id)
|
128
|
+
|
129
|
+
You will get a response like this one:
|
130
|
+
|
131
|
+
{
|
132
|
+
"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,
|
150
|
+
"net_received_amount" => 0,
|
151
|
+
"marketplace" => "NONE",
|
152
|
+
"marketplace_fee" => nil,
|
153
|
+
"reason" => "Example T-Shirt",
|
154
|
+
"payer" => {
|
155
|
+
"id" => 543219876,
|
156
|
+
"first_name" => "John",
|
157
|
+
"last_name" => "Mikel",
|
158
|
+
"nickname" => "JOHNMIKEL",
|
159
|
+
"phone" => {
|
160
|
+
"area_code" => nil,
|
161
|
+
"number" => "551122334455",
|
162
|
+
"extension" => nil
|
163
|
+
},
|
164
|
+
"email" => "buyer@email.com",
|
165
|
+
"identification" => {
|
166
|
+
"type" => nil,
|
167
|
+
"number" => nil
|
168
|
+
}
|
169
|
+
},
|
170
|
+
"collector" => {
|
171
|
+
"id" => 123456789,
|
172
|
+
"first_name" => "Bill",
|
173
|
+
"last_name" => "Receiver",
|
174
|
+
"phone" => {
|
175
|
+
"area_code" => nil,
|
176
|
+
"number" => "1122334455",
|
177
|
+
"extension" => nil
|
178
|
+
},
|
179
|
+
"email" => "receiver@email.com",
|
180
|
+
"nickname" => "BILLRECEIVER"
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
### Errors
|
186
|
+
|
187
|
+
Errors will also be hashes with status code, message and error key.
|
188
|
+
|
189
|
+
For example, if you request payment method status for an invalid operation, you will see something like this:
|
190
|
+
|
191
|
+
{
|
192
|
+
"message" => "Resource not found",
|
193
|
+
"error" => "not_found",
|
194
|
+
"status" => 404,
|
195
|
+
"cause" => []
|
196
|
+
}
|
197
|
+
|
198
|
+
### Tests
|
199
|
+
|
200
|
+
This gem has tests for a few methods. To check if it is working properly, just run:
|
201
|
+
|
202
|
+
rake test
|
203
|
+
|
204
|
+
Changelog
|
205
|
+
---------
|
206
|
+
|
207
|
+
1.0.2
|
208
|
+
Changed documentation according to the new client intercace, added a notification method to the client and refactored the tests.
|
209
|
+
|
210
|
+
1.0.1 (thanks etagwerker)
|
211
|
+
|
212
|
+
Added client interface, renamed "Mercadopago" to "MercadoPago", translated project summary to English and added tests for a few methods.
|
213
|
+
|
214
|
+
0.0.1
|
215
|
+
|
216
|
+
First release. It's possible to authenticate with the MercadoPago APIs, create payments and check payment status.
|
217
|
+
|
218
|
+
Copyright
|
219
|
+
---------
|
220
|
+
|
221
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
222
|
+
|
223
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
224
|
+
|
225
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module MercadoPago
|
2
|
+
|
3
3
|
module Authentication
|
4
|
-
|
4
|
+
|
5
5
|
#
|
6
6
|
# Receives the client credentials and makes a request to oAuth API.
|
7
7
|
# On success, returns a hash with the access data; on failure, returns nil.
|
8
8
|
#
|
9
9
|
# To get your client credentials, access:
|
10
10
|
# https://www.mercadopago.com/mlb/ferramentas/aplicacoes
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# - client_id
|
13
13
|
# - client_secret
|
14
14
|
#
|
15
15
|
def self.access_token(client_id, client_secret)
|
16
|
-
|
16
|
+
|
17
17
|
payload = { :grant_type => 'client_credentials', :client_id => client_id, :client_secret => client_secret }
|
18
18
|
headers = { :content_type => 'application/x-www-form-urlencoded', :accept => 'application/json' }
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
|
20
|
+
MercadoPago::Request.wrap_post('/oauth/token', payload, headers)
|
21
|
+
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
#
|
25
25
|
# TODO
|
26
26
|
#
|
27
27
|
def refresh_access_token
|
28
|
-
|
28
|
+
|
29
29
|
# TODO
|
30
|
-
|
30
|
+
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
end
|
34
|
-
|
35
|
-
end
|
34
|
+
|
35
|
+
end
|
data/lib/mercadopago/checkout.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module MercadoPago
|
2
|
+
|
3
3
|
module Checkout
|
4
|
-
|
4
|
+
|
5
5
|
#
|
6
6
|
# Allows you to configure the checkout process.
|
7
7
|
# Receives an access_token and a prefereces hash and creates a new checkout preference.
|
@@ -13,32 +13,35 @@ 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
|
-
|
16
|
+
|
17
17
|
payload = JSON.generate(data)
|
18
18
|
headers = { :content_type => 'application/json', :accept => 'application/json' }
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
|
20
|
+
MercadoPago::Request.wrap_post("/checkout/preferences?access_token=#{access_token}", payload, headers)
|
21
|
+
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
|
+
# Returns the hash with the details of certain payment preference.
|
24
25
|
#
|
25
|
-
#
|
26
|
+
# - access_token: the MercadoPago account access token
|
27
|
+
# - preference_id: the payment preference ID
|
26
28
|
#
|
27
|
-
def self.get_preference
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def self.get_preference(access_token, preference_id)
|
30
|
+
|
31
|
+
headers = { :accept => 'application/json' }
|
32
|
+
MercadoPago::Request.wrap_get("/checkout/preferences/#{preference_id}?access_token=#{access_token}")
|
33
|
+
|
31
34
|
end
|
32
|
-
|
35
|
+
|
33
36
|
#
|
34
37
|
# TODO
|
35
38
|
#
|
36
39
|
def self.update_preference
|
37
|
-
|
40
|
+
|
38
41
|
# TODO
|
39
|
-
|
42
|
+
|
40
43
|
end
|
41
|
-
|
44
|
+
|
42
45
|
end
|
43
|
-
|
44
|
-
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module MercadoPago
|
2
|
+
|
3
|
+
class AccessError < Exception
|
4
|
+
end
|
5
|
+
|
6
|
+
#
|
7
|
+
# You can create a Client object to interact with a MercadoPago
|
8
|
+
# account through the API.
|
9
|
+
#
|
10
|
+
# You will need client_id and client_secret. Client will save
|
11
|
+
# the token as part of its inner state. It will use it to call
|
12
|
+
# API methods.
|
13
|
+
#
|
14
|
+
# Usage example:
|
15
|
+
#
|
16
|
+
# mp_client = MercadoPago::Client.new(client_id, client_secret)
|
17
|
+
# mp_client.create_preference(data)
|
18
|
+
#
|
19
|
+
class Client
|
20
|
+
attr_reader :token
|
21
|
+
|
22
|
+
#
|
23
|
+
# Creates an instance and stores the
|
24
|
+
# access_token to make calls to the
|
25
|
+
# MercadoPago API.
|
26
|
+
#
|
27
|
+
def initialize(client_id, client_secret)
|
28
|
+
response = MercadoPago::Authentication.access_token(client_id, client_secret)
|
29
|
+
|
30
|
+
unless @token = response["access_token"]
|
31
|
+
raise AccessError, response["message"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Creates a payment preference.
|
37
|
+
#
|
38
|
+
# - data: contains the data according to the payment preference will be created.
|
39
|
+
#
|
40
|
+
def create_preference(data)
|
41
|
+
MercadoPago::Checkout.create_preference(@token, data)
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Returns the payment preference.
|
46
|
+
#
|
47
|
+
# - preference_id: the id of the payment preference that will be retrieved.
|
48
|
+
#
|
49
|
+
def get_preference(preference_id)
|
50
|
+
MercadoPago::Checkout.get_preference(@token, preference_id)
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Retrieves the latest information about a payment.
|
55
|
+
#
|
56
|
+
# - payment_id: the id of the payment to be checked.
|
57
|
+
#
|
58
|
+
def notification(payment_id)
|
59
|
+
MercadoPago::Collection.notification(@token, payment_id)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
module
|
2
|
-
|
1
|
+
module MercadoPago
|
2
|
+
|
3
3
|
module Collection
|
4
|
-
|
4
|
+
|
5
5
|
#
|
6
6
|
# Receives an access_token and a payment id and retrieves information of the payment.
|
7
7
|
# This is useful, for example, to check the status of a payment.
|
@@ -10,11 +10,11 @@ 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
|
-
|
14
|
-
|
15
|
-
|
13
|
+
|
14
|
+
MercadoPago::Request.wrap_get("/collections/notifications/#{payment_id}?access_token=#{access_token}", { :accept => 'application/json' })
|
15
|
+
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
end
|
19
|
-
|
20
|
-
end
|
19
|
+
|
20
|
+
end
|
data/lib/mercadopago/request.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'rest-client'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
4
|
+
module MercadoPago
|
5
|
+
|
6
6
|
module Request
|
7
|
-
|
7
|
+
|
8
8
|
#
|
9
9
|
# This URL is the base for all API calls.
|
10
10
|
#
|
@@ -31,7 +31,7 @@ module Mercadopago
|
|
31
31
|
# - headers: the headers to be transmitted over the HTTP request.
|
32
32
|
#
|
33
33
|
def self.wrap_get(path, headers = {})
|
34
|
-
|
34
|
+
|
35
35
|
make_request(:get, path, nil, headers)
|
36
36
|
|
37
37
|
end
|
@@ -45,11 +45,11 @@ module Mercadopago
|
|
45
45
|
# - headers: the headers to be transmitted over the HTTP request.
|
46
46
|
#
|
47
47
|
def self.make_request(type, path, payload = nil, headers = {})
|
48
|
-
|
48
|
+
|
49
49
|
begin
|
50
50
|
args = [type, "#{MERCADOPAGO_URL}#{path}", payload, headers].compact
|
51
51
|
response = RestClient.send *args
|
52
|
-
|
52
|
+
|
53
53
|
JSON.load(response)
|
54
54
|
rescue Exception => e
|
55
55
|
JSON.load(e.response)
|
@@ -57,10 +57,9 @@ module Mercadopago
|
|
57
57
|
|
58
58
|
end
|
59
59
|
|
60
|
-
|
61
60
|
class ClientError < Exception
|
62
61
|
end
|
63
|
-
|
62
|
+
|
64
63
|
end
|
65
|
-
|
66
|
-
end
|
64
|
+
|
65
|
+
end
|
data/lib/mercadopago/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "
|
1
|
+
module MercadoPago
|
2
|
+
VERSION = "1.0.2"
|
3
3
|
end
|
data/lib/mercadopago.rb
CHANGED
data/mercadopago.gemspec
CHANGED
@@ -4,13 +4,13 @@ require "mercadopago/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "mercadopago"
|
7
|
-
s.version =
|
8
|
-
s.authors = ["Kauplus Social Commerce"]
|
7
|
+
s.version = MercadoPago::VERSION
|
8
|
+
s.authors = ["Kauplus Social Commerce", "Ombu Shop, Tu Tienda Online"]
|
9
9
|
s.email = ["suporte@kauplus.com.br"]
|
10
10
|
s.homepage = "https://github.com/kauplus/mercadopago"
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{
|
13
|
-
|
11
|
+
s.summary = %q{Client for the MercadoPago API}
|
12
|
+
s.description = %q{This gem allows developers to use the services available in the MercadoPago API (http://www.mercadopago.com)}
|
13
|
+
|
14
14
|
s.rubyforge_project = "mercadopago"
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
# specify any dependencies here:
|
22
|
-
s.add_dependency 'json', '1.
|
22
|
+
s.add_dependency 'json', '>= 1.4.6'
|
23
23
|
s.add_dependency 'rest-client', '1.6.7'
|
24
|
+
s.add_development_dependency 'pry'
|
24
25
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'mercadopago'
|
5
|
+
|
6
|
+
class TestMercadoPago < MiniTest::Unit::TestCase
|
7
|
+
|
8
|
+
#
|
9
|
+
# Valid credentials to be used in the tests.
|
10
|
+
#
|
11
|
+
CREDENTIALS = {
|
12
|
+
:client_id => '8897',
|
13
|
+
:client_secret => 'PC2MoR6baSu75xZnkhLRHoyelnpLkNbh'
|
14
|
+
}
|
15
|
+
|
16
|
+
#
|
17
|
+
# Example payment request.
|
18
|
+
#
|
19
|
+
PAYMENT_REQUEST = {
|
20
|
+
"external_reference" => "OPERATION-ID-1234",
|
21
|
+
"items" => [
|
22
|
+
{
|
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"
|
30
|
+
}
|
31
|
+
],
|
32
|
+
"payer" => {
|
33
|
+
"name" => "John",
|
34
|
+
"surname" => "Mikel",
|
35
|
+
"email" => "buyer@email.com"
|
36
|
+
},
|
37
|
+
"back_urls" => {
|
38
|
+
"pending" => "https://www.site.com/pending",
|
39
|
+
"success" => "http://www.site.com/success",
|
40
|
+
"failure" => "http://www.site.com/failure"
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
# With a valid client id and secret (test account)
|
45
|
+
def test_that_authentication_returns_access_token
|
46
|
+
response = MercadoPago::Authentication.access_token(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
|
47
|
+
assert response["access_token"]
|
48
|
+
end
|
49
|
+
|
50
|
+
# Using fake client id and client secret
|
51
|
+
def test_that_authentication_fails_with_wrong_parameters
|
52
|
+
response = MercadoPago::Authentication.access_token('fake_client_id', 'fake_client_secret')
|
53
|
+
assert_nil response["access_token"]
|
54
|
+
assert_equal "invalid_client", response["error"]
|
55
|
+
end
|
56
|
+
|
57
|
+
# Using fake token
|
58
|
+
def test_that_request_fails_with_wrong_token
|
59
|
+
response = MercadoPago::Checkout.create_preference('fake_token', {})
|
60
|
+
assert_equal "Malformed access_token: fake_token", response["message"]
|
61
|
+
assert_equal "bad_request", response["error"]
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_that_client_initializes_okay_with_valid_details
|
65
|
+
mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
|
66
|
+
assert mp_client.token
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_that_client_fails_with_wrong_details
|
70
|
+
assert_raises(MercadoPago::AccessError) do
|
71
|
+
mp_client = MercadoPago::Client.new('fake_client_id', 'fake_client_secret')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_that_client_can_create_payment_preference
|
76
|
+
mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
|
77
|
+
response = mp_client.create_preference(PAYMENT_REQUEST)
|
78
|
+
assert response["init_point"]
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_that_client_can_get_preference
|
82
|
+
mp_client = MercadoPago::Client.new(CREDENTIALS[:client_id], CREDENTIALS[:client_secret])
|
83
|
+
|
84
|
+
response = mp_client.create_preference(PAYMENT_REQUEST)
|
85
|
+
assert pref_id = response["id"]
|
86
|
+
|
87
|
+
response = mp_client.get_preference(pref_id)
|
88
|
+
assert_equal "https://www.mercadopago.com/mla/checkout/pay?pref_id=#{pref_id}", response["init_point"]
|
89
|
+
end
|
90
|
+
|
91
|
+
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
|
+
|
100
|
+
response = mp_client.notification(payment_id)
|
101
|
+
assert_equal payment_id, response['collection']['id']
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
metadata
CHANGED
@@ -1,31 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mercadopago
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kauplus Social Commerce
|
9
|
+
- Ombu Shop, Tu Tienda Online
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
+
date: 2012-07-04 00:00:00.000000000 -03:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: json
|
17
|
-
requirement: &
|
18
|
+
requirement: &2152004880 !ruby/object:Gem::Requirement
|
18
19
|
none: false
|
19
20
|
requirements:
|
20
|
-
- -
|
21
|
+
- - ! '>='
|
21
22
|
- !ruby/object:Gem::Version
|
22
|
-
version: 1.
|
23
|
+
version: 1.4.6
|
23
24
|
type: :runtime
|
24
25
|
prerelease: false
|
25
|
-
version_requirements: *
|
26
|
+
version_requirements: *2152004880
|
26
27
|
- !ruby/object:Gem::Dependency
|
27
28
|
name: rest-client
|
28
|
-
requirement: &
|
29
|
+
requirement: &2151872980 !ruby/object:Gem::Requirement
|
29
30
|
none: false
|
30
31
|
requirements:
|
31
32
|
- - =
|
@@ -33,9 +34,20 @@ dependencies:
|
|
33
34
|
version: 1.6.7
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
|
-
version_requirements: *
|
37
|
-
|
38
|
-
|
37
|
+
version_requirements: *2151872980
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: pry
|
40
|
+
requirement: &2151869700 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *2151869700
|
49
|
+
description: This gem allows developers to use the services available in the MercadoPago
|
50
|
+
API (http://www.mercadopago.com)
|
39
51
|
email:
|
40
52
|
- suporte@kauplus.com.br
|
41
53
|
executables: []
|
@@ -49,10 +61,12 @@ files:
|
|
49
61
|
- lib/mercadopago.rb
|
50
62
|
- lib/mercadopago/authentication.rb
|
51
63
|
- lib/mercadopago/checkout.rb
|
64
|
+
- lib/mercadopago/client.rb
|
52
65
|
- lib/mercadopago/collection.rb
|
53
66
|
- lib/mercadopago/request.rb
|
54
67
|
- lib/mercadopago/version.rb
|
55
68
|
- mercadopago.gemspec
|
69
|
+
- test/test_mercado_pago.rb
|
56
70
|
has_rdoc: true
|
57
71
|
homepage: https://github.com/kauplus/mercadopago
|
58
72
|
licenses: []
|
@@ -77,5 +91,6 @@ rubyforge_project: mercadopago
|
|
77
91
|
rubygems_version: 1.6.2
|
78
92
|
signing_key:
|
79
93
|
specification_version: 3
|
80
|
-
summary:
|
81
|
-
test_files:
|
94
|
+
summary: Client for the MercadoPago API
|
95
|
+
test_files:
|
96
|
+
- test/test_mercado_pago.rb
|