mercadopago 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ ## MAC OS
7
+ .DS_Store
8
+
9
+ ## TEXTMATE
10
+ *.tmproj
11
+ tmtags
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mercadopago.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,196 @@
1
+ MercadoPago
2
+ ===========
3
+
4
+ Esta gem para Ruby é um cliente que permite que desenvolvedores acessem os serviços do http://www.mercadopago.com (MercadoPago).
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.
7
+
8
+ Para conhecer a documentação das APIs do MercadoPago, veja este link: https://developers.mercadopago.com/integracao-checkout
9
+
10
+ Instalação
11
+ ----------
12
+
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
+ }
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ require 'mercadopago/version'
2
+ require 'mercadopago/request'
3
+ require 'mercadopago/authentication'
4
+ require 'mercadopago/checkout'
5
+ require 'mercadopago/collection'
6
+
7
+ module Mercadopago
8
+ end
@@ -0,0 +1,35 @@
1
+ module Mercadopago
2
+
3
+ module Authentication
4
+
5
+ #
6
+ # Receives the client credentials and makes a request to oAuth API.
7
+ # On success, returns a hash with the access data; on failure, returns nil.
8
+ #
9
+ # To get your client credentials, access:
10
+ # https://www.mercadopago.com/mlb/ferramentas/aplicacoes
11
+ #
12
+ # - client_id
13
+ # - client_secret
14
+ #
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' }
19
+
20
+ Mercadopago::Request.wrap_post('/oauth/token', payload, headers)
21
+
22
+ end
23
+
24
+ #
25
+ # TODO
26
+ #
27
+ def refresh_access_token
28
+
29
+ # TODO
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,44 @@
1
+ module Mercadopago
2
+
3
+ module Checkout
4
+
5
+ #
6
+ # Allows you to configure the checkout process.
7
+ # Receives an access_token and a prefereces hash and creates a new checkout preference.
8
+ # Once you have created the checkout preference, you can use the init_point URL
9
+ # to start the payment flow.
10
+ #
11
+ # - access_token: the MercadoPago account associated with this access_token will
12
+ # receive the money from the payment of this checkout preference.
13
+ # - data: a hash of preferences that will be trasmitted to checkout API.
14
+ #
15
+ def self.create_preference(access_token, data)
16
+
17
+ payload = JSON.generate(data)
18
+ headers = { :content_type => 'application/json', :accept => 'application/json' }
19
+
20
+ Mercadopago::Request.wrap_post("/checkout/preferences?access_token=#{access_token}", payload, headers)
21
+
22
+ end
23
+
24
+ #
25
+ # TODO
26
+ #
27
+ def self.get_preference
28
+
29
+ # TODO
30
+
31
+ end
32
+
33
+ #
34
+ # TODO
35
+ #
36
+ def self.update_preference
37
+
38
+ # TODO
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,20 @@
1
+ module Mercadopago
2
+
3
+ module Collection
4
+
5
+ #
6
+ # Receives an access_token and a payment id and retrieves information of the payment.
7
+ # This is useful, for example, to check the status of a payment.
8
+ #
9
+ # - access_token: an access_token of the MercadoPago account associated with the payment to be checked.
10
+ # - payment_id: the id of the payment to be checked.
11
+ #
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
+
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,66 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ module Mercadopago
5
+
6
+ module Request
7
+
8
+ #
9
+ # This URL is the base for all API calls.
10
+ #
11
+ MERCADOPAGO_URL = 'https://api.mercadolibre.com'
12
+
13
+ #
14
+ # Makes a POST request to a MercaPago API.
15
+ #
16
+ # - path: the path of the API to be called.
17
+ # - payload: the data to be trasmitted to the API.
18
+ # - headers: the headers to be transmitted over the HTTP request.
19
+ #
20
+ def self.wrap_post(path, payload, headers = {})
21
+
22
+ raise ClientError('No data given') if payload.nil? or payload.empty?
23
+ make_request(:post, path, payload, headers)
24
+
25
+ end
26
+
27
+ #
28
+ # Makes a GET request to a MercaPago API.
29
+ #
30
+ # - path: the path of the API to be called, including any query string parameters.
31
+ # - headers: the headers to be transmitted over the HTTP request.
32
+ #
33
+ def self.wrap_get(path, headers = {})
34
+
35
+ make_request(:get, path, nil, headers)
36
+
37
+ end
38
+
39
+ #
40
+ # Makes a HTTP request to a MercadoPago API.
41
+ #
42
+ # - type: the HTTP request type (:get, :post, :put, :delete).
43
+ # - path: the path of the API to be called.
44
+ # - payload: the data to be trasmitted to the API.
45
+ # - headers: the headers to be transmitted over the HTTP request.
46
+ #
47
+ def self.make_request(type, path, payload = nil, headers = {})
48
+
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
+
61
+ class ClientError < Exception
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ module Mercadopago
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mercadopago/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mercadopago"
7
+ s.version = Mercadopago::VERSION
8
+ s.authors = ["Kauplus Social Commerce"]
9
+ s.email = ["suporte@kauplus.com.br"]
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
+
14
+ s.rubyforge_project = "mercadopago"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here:
22
+ s.add_dependency 'json', '1.7.1'
23
+ s.add_dependency 'rest-client', '1.6.7'
24
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mercadopago
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kauplus Social Commerce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000 -03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ requirement: &2164866220 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - =
21
+ - !ruby/object:Gem::Version
22
+ version: 1.7.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2164866220
26
+ - !ruby/object:Gem::Dependency
27
+ name: rest-client
28
+ requirement: &2164857660 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - =
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.7
34
+ type: :runtime
35
+ 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)
39
+ email:
40
+ - suporte@kauplus.com.br
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.md
48
+ - Rakefile
49
+ - lib/mercadopago.rb
50
+ - lib/mercadopago/authentication.rb
51
+ - lib/mercadopago/checkout.rb
52
+ - lib/mercadopago/collection.rb
53
+ - lib/mercadopago/request.rb
54
+ - lib/mercadopago/version.rb
55
+ - mercadopago.gemspec
56
+ has_rdoc: true
57
+ homepage: https://github.com/kauplus/mercadopago
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project: mercadopago
77
+ rubygems_version: 1.6.2
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Cliente para a API do MercadoPago
81
+ test_files: []