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 CHANGED
@@ -1,196 +1,225 @@
1
- MercadoPago
2
- ===========
1
+ MercadoPago Gem
2
+ ===============
3
3
 
4
- Esta gem para Ruby é um cliente que permite que desenvolvedores acessem os serviços do http://www.mercadopago.com (MercadoPago).
4
+ This is a Ruby client for all the services offered by [MercadoPago](http://www.mercadopago.com).
5
5
 
6
- Antes de começar a usá-la, é recomendável conhecer mais sobre as APIs do MercadoPago e ler as suas documentações. Como esta gem utiliza hashes para representar requests e responses, é necessário conhecer os parâmetros das APIs do MercadoPago para fazer as chamadas corretas e processar as respostas corretamente.
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
- Para conhecer a documentação das APIs do MercadoPago, veja este link: https://developers.mercadopago.com/integracao-checkout
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
- Instalação
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
- Você pode instalar a última versão da gem MercadoPago com o seguinte comando:
14
-
15
- gem install mercadopago
16
-
17
- Você pode também adicionar a gem ao Gemfile do seu projeto:
18
-
19
- gem 'mercadopago'
20
-
21
- Credenciais de acesso
22
- ---------------------
23
-
24
- Para usar esta gem, é necessário fornecer o client_id e um client_secret da sua conta do MercaodPago. Esta gem não armazena este dados sob nenhuma hipótese. Para consultar as suas credenciais no MercadoPago, acesse o link a seguir: https://www.mercadopago.com/mlb/ferramentas/aplicacoes
25
-
26
- Exemplos
27
- --------
28
-
29
- ### Autenticação
30
-
31
- # Use as suas credenciais.
32
- client_id = '1234'
33
- client_secret = 'abcdefghijklmnopqrstuvwxyz'
34
-
35
- access = Mercadopago::Authentication.access_token(client_id, client_secret)
36
-
37
- A resposta desta requisição será um hash como o que segue:
38
-
39
- {
40
- "access_token" => "APP_USR-1234-999999-abcdefghijklmnopqrstuvwxyz-999999999",
41
- "token_type" => "bearer",
42
- "expires_in" => 10800,
43
- "scope" => "mclics_advertising offline_access read write",
44
- "refresh_token" => "TG-abcdefghijklmnopqrstuvwxyz"
45
- }
46
-
47
- Você deverá usar o "access_token" nas demais requisições para outros webservices.
48
-
49
- ### Criação de pagamento
50
-
51
- data = {
52
- "external_reference" => "OPERATION-ID-1234",
53
- "items" => [
54
- {
55
- "id" => "Código 123",
56
- "title" => "Nome produto",
57
- "description" => "Descrição produto",
58
- "quantity" => 1,
59
- "unit_price" => 10.50,
60
- "currency_id" => "BRL",
61
- "picture_url" => "http://www.site.com.br/image/123.png"
62
- }
63
- ],
64
- "payer" => {
65
- "name"=> "João",
66
- "surname"=> "Silva",
67
- "email"=> "comprador@email.com.br"
68
- },
69
- "back_urls"=> {
70
- "pending"=> "https://www.site.com.br/pending",
71
- "success"=> "http://www.site.com.br/success",
72
- "failure"=> "http://www.site.com.br/failure"
73
- }
74
- }
75
-
76
- payment = Mercadopago::Checkout.create_preference(access_token, data)
77
-
78
- Como resposta, será recebido um hash como o seguinte:
79
-
80
- {
81
- "payment_methods" => {},
82
- "init_point" => "https://www.mercadopago.com/mlb/checkout/pay?pref_id=abcdefgh-9999-9999-ab99-999999999999",
83
- "collector_id" => 123456789,
84
- "back_urls" => {
85
- "pending"=> "https://www.site.com.br/pending",
86
- "success"=> "http://www.site.com.br/success",
87
- "failure"=> "http://www.site.com.br/failure"
88
- },
89
- "sponsor_id" => nil,
90
- "expiration_date_from" => nil,
91
- "additional_info" => "",
92
- "marketplace_fee" => 0,
93
- "date_created" => "2012-05-07T20:07:52.293-04:00",
94
- "subscription_plan_id" => nil,
95
- "id"=> "abcdefgh-9999-9999-ab99-999999999999",
96
- "expiration_date_to" => nil,
97
- "expires" => false,
98
- "external_reference" => "OPERATION-ID-1234",
99
- "payer" => {
100
- "email" => "comprador@email.com.br",
101
- "name" => "João",
102
- "surname" => "Silva"
103
- },
104
- "items" => [
105
- {
106
- "id" => "Código 123",
107
- "currency_id" => "BRL",
108
- "title" => "Nome produto",
109
- "picture_url" => "http://www.site.com.br/image/123.png",
110
- "description" => "Descrição produto",
111
- "quantity" => 1,
112
- "unit_price" => 10.50
113
- }
114
- ],
115
- "client_id" => "963",
116
- "marketplace" => "NONE"
117
- }
118
-
119
- ### Verificação de status de pagamento
120
-
121
- Para consultar o status de um pagamento é necessário ter o id associado a ele, o qual é recebido na IPN do MercadoPago.
122
-
123
- # Use o id do pagamento recebido na IPN do MercadoPago.
124
- payment_id = '987654321'
125
-
126
- notification = Mercadopago::Collection.notification(access_token, payment_id)
127
-
128
- Será retornado um hash similar ao seguinte:
129
-
130
- {
131
- "collection" => {
132
- "id" => 987654321,
133
- "site_id" => "MLB",
134
- "operation_type" => "regular_payment",
135
- "order_id" => nil,
136
- "external_reference" => "OPERATION-ID-1234",
137
- "status" => "approved",
138
- "status_detail" => "approved",
139
- "payment_type" => "credit_card",
140
- "date_created" => "2012-05-05T14:22:43Z",
141
- "last_modified" => "2012-05-05T14:35:13Z",
142
- "date_approved" => "2012-05-05T14:22:43Z",
143
- "money_release_date" => "2012-05-19T14:22:43Z",
144
- "currency_id" => "BRL",
145
- "transaction_amount" => 10.50,
146
- "shipping_cost" => 0,
147
- "total_paid_amount" => 10.50,
148
- "finance_charge" => 0,
149
- "net_received_amount" => 0,
150
- "marketplace" => "NONE",
151
- "marketplace_fee" => nil,
152
- "reason" => "Nome produto",
153
- "payer" => {
154
- "id" => 543219876,
155
- "first_name" => "João",
156
- "last_name" => "Silva",
157
- "nickname" => "JOAOSILVA",
158
- "phone" => {
159
- "area_code" => nil,
160
- "number" => "551122334455",
161
- "extension" => nil
162
- },
163
- "email" => "comprador@email.com.br",
164
- "identification" => {
165
- "type" => nil,
166
- "number" => nil
167
- }
168
- },
169
- "collector" => {
170
- "id" => 123456789,
171
- "first_name" => "Manoel",
172
- "last_name" => "Recebedor",
173
- "phone" => {
174
- "area_code" => nil,
175
- "number" => "1122334455",
176
- "extension" => nil
177
- },
178
- "email" => "recebedor@email.com.br",
179
- "nickname" => "MANOELRECEBEDOR"
180
- }
181
- }
182
- }
183
-
184
-
185
- ### Erros
186
-
187
- Os erros retornados por esta gem são hashes quem contém os dados recebidos dos webservices do MercadoPago.
188
-
189
- Por exemplo, caso seja requisitado um access_token através da credenciais inválidas, um hash de erro como o seguinte será retornado:
190
-
191
- {
192
- "message" => "client_id[1,234] o client_secret[abcdefghijklmnopqrstuvwxyz] inválidos",
193
- "error" => "invalid_client",
194
- "status" => 400,
195
- "cause" => []
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 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ task :test do
6
+ require "./test/test_mercado_pago.rb"
7
+ end
@@ -1,35 +1,35 @@
1
- module Mercadopago
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
- Mercadopago::Request.wrap_post('/oauth/token', payload, headers)
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
@@ -1,7 +1,7 @@
1
- module Mercadopago
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
- Mercadopago::Request.wrap_post("/checkout/preferences?access_token=#{access_token}", payload, headers)
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
- # TODO
26
+ # - access_token: the MercadoPago account access token
27
+ # - preference_id: the payment preference ID
26
28
  #
27
- def self.get_preference
28
-
29
- # TODO
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 Mercadopago
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
- Mercadopago::Request.wrap_get("/collections/notifications/#{payment_id}?access_token=#{access_token}", { :accept => 'application/json' })
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
@@ -1,10 +1,10 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
3
 
4
- module Mercadopago
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
@@ -1,3 +1,3 @@
1
- module Mercadopago
2
- VERSION = "0.0.1"
1
+ module MercadoPago
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/mercadopago.rb CHANGED
@@ -3,6 +3,7 @@ require 'mercadopago/request'
3
3
  require 'mercadopago/authentication'
4
4
  require 'mercadopago/checkout'
5
5
  require 'mercadopago/collection'
6
+ require 'mercadopago/client'
6
7
 
7
- module Mercadopago
8
- end
8
+ module MercadoPago
9
+ end
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 = Mercadopago::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{Cliente para a API do MercadoPago}
12
- s.description = %q{Esta gem é um cliente que permite que desenvolvedores acessem os serviços do http://www.mercadopago.com (MercadoPago)}
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.7.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: 0.0.1
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-05-07 00:00:00.000000000 -03:00
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: &2164866220 !ruby/object:Gem::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.7.1
23
+ version: 1.4.6
23
24
  type: :runtime
24
25
  prerelease: false
25
- version_requirements: *2164866220
26
+ version_requirements: *2152004880
26
27
  - !ruby/object:Gem::Dependency
27
28
  name: rest-client
28
- requirement: &2164857660 !ruby/object:Gem::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: *2164857660
37
- description: Esta gem é um cliente que permite que desenvolvedores acessem os serviços
38
- do http://www.mercadopago.com (MercadoPago)
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: Cliente para a API do MercadoPago
81
- test_files: []
94
+ summary: Client for the MercadoPago API
95
+ test_files:
96
+ - test/test_mercado_pago.rb