mercadopago-sdk 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/README.md +111 -0
- data/Rakefile +10 -0
- data/examples/checkout-buttons/basic_preference/button.rb +28 -0
- data/examples/instant-payment-notifications/receive-ipn.rb +25 -0
- data/examples/payment-search/search-approved-payments.rb +23 -0
- data/examples/payment-search/search-creditcard-payments.rb +23 -0
- data/examples/payment-search/search-funded-payments-by-name.rb +23 -0
- data/examples/payment-search/search-payments-from-email-and-date.rb +30 -0
- data/examples/payment-search/search-payments.rb +23 -0
- data/lib/cacert.pem +3390 -0
- data/lib/mercadopago.rb +197 -0
- data/lib/version.rb +1 -0
- data/mercadopago.gemspec +21 -0
- data/tests/tests.rb +49 -0
- metadata +92 -0
data/lib/mercadopago.rb
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
#MercadoPago Integration Library
|
2
|
+
#Access MercadoPago for payments integration
|
3
|
+
#
|
4
|
+
#@author @maticompiano
|
5
|
+
#@contributors @chrismo
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'json/add/core'
|
9
|
+
require 'uri'
|
10
|
+
require 'net/https'
|
11
|
+
require 'yaml'
|
12
|
+
require 'version'
|
13
|
+
|
14
|
+
class MercadoPago
|
15
|
+
def initialize(client_id, client_secret, debug_logger=nil)
|
16
|
+
@client_id = client_id
|
17
|
+
@client_secret = client_secret
|
18
|
+
@rest_client = RestClient.new(debug_logger)
|
19
|
+
@sandbox = false
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_debug_logger(debug_logger)
|
23
|
+
@rest_client.set_debug_logger(debug_logger)
|
24
|
+
end
|
25
|
+
|
26
|
+
def sandbox_mode(enable=nil)
|
27
|
+
if not enable.nil?
|
28
|
+
@sandbox = enable
|
29
|
+
end
|
30
|
+
|
31
|
+
return @sandbox
|
32
|
+
end
|
33
|
+
|
34
|
+
# Get Access Token for API use
|
35
|
+
def get_access_token
|
36
|
+
app_client_values = {
|
37
|
+
'grant_type' => 'client_credentials',
|
38
|
+
'client_id' => @client_id,
|
39
|
+
'client_secret' => @client_secret
|
40
|
+
}
|
41
|
+
|
42
|
+
@access_data = @rest_client.post("/oauth/token", build_query(app_client_values), RestClient::MIME_FORM)
|
43
|
+
|
44
|
+
if @access_data['status'] == "200"
|
45
|
+
@access_data = @access_data["response"]
|
46
|
+
@access_data['access_token']
|
47
|
+
else
|
48
|
+
raise @access_data.inspect
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get information for specific payment
|
53
|
+
def get_payment_info(id)
|
54
|
+
begin
|
55
|
+
access_token = get_access_token
|
56
|
+
rescue => e
|
57
|
+
return e.message
|
58
|
+
end
|
59
|
+
|
60
|
+
uri_prefix = @sandbox ? "/sandbox" : ""
|
61
|
+
@rest_client.get(uri_prefix + "/collections/notifications/" + id + "?access_token=" + access_token)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Refund accredited payment
|
65
|
+
def refund_payment(id)
|
66
|
+
begin
|
67
|
+
access_token = get_access_token
|
68
|
+
rescue => e
|
69
|
+
return e.message
|
70
|
+
end
|
71
|
+
|
72
|
+
refund_status = {"status" => "refunded"}
|
73
|
+
@rest_client.put("/collections/" + id + "?access_token=" + access_token, refund_status)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Cancel pending payment
|
77
|
+
def cancel_payment(id)
|
78
|
+
begin
|
79
|
+
access_token = get_access_token
|
80
|
+
rescue => e
|
81
|
+
return e.message
|
82
|
+
end
|
83
|
+
|
84
|
+
cancel_status = {"status" => "cancelled"}
|
85
|
+
@rest_client.put("/collections/" + id + "?access_token=" + access_token, cancel_status)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Search payments according to filters, with pagination
|
89
|
+
def search_payment(filters, offset=0, limit=0)
|
90
|
+
begin
|
91
|
+
access_token = get_access_token
|
92
|
+
rescue => e
|
93
|
+
return e.message
|
94
|
+
end
|
95
|
+
|
96
|
+
filters["offset"] = offset
|
97
|
+
filters["limit"] = limit
|
98
|
+
|
99
|
+
filters = build_query(filters)
|
100
|
+
|
101
|
+
uri_prefix = @sandbox ? "/sandbox" : ""
|
102
|
+
@rest_client.get(uri_prefix + "/collections/search?" + filters + "&access_token=" + access_token)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Create a checkout preference
|
106
|
+
def create_preference(preference)
|
107
|
+
begin
|
108
|
+
access_token = get_access_token
|
109
|
+
rescue => e
|
110
|
+
return e.message
|
111
|
+
end
|
112
|
+
|
113
|
+
@rest_client.post("/checkout/preferences?access_token=" + access_token, preference)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Update a checkout preference
|
117
|
+
def update_preference(id, preference)
|
118
|
+
begin
|
119
|
+
access_token = get_access_token
|
120
|
+
rescue => e
|
121
|
+
return e.message
|
122
|
+
end
|
123
|
+
|
124
|
+
@rest_client.put("/checkout/preferences/" + id + "?access_token=" + access_token, preference)
|
125
|
+
end
|
126
|
+
|
127
|
+
# Get a checkout preference
|
128
|
+
def get_preference(id)
|
129
|
+
begin
|
130
|
+
access_token = get_access_token
|
131
|
+
rescue => e
|
132
|
+
return e.message
|
133
|
+
end
|
134
|
+
|
135
|
+
@rest_client.get("/checkout/preferences/" + id + "?access_token=" + access_token)
|
136
|
+
end
|
137
|
+
|
138
|
+
def build_query(params)
|
139
|
+
URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
class RestClient
|
145
|
+
|
146
|
+
MIME_JSON = 'application/json'
|
147
|
+
MIME_FORM = 'application/x-www-form-urlencoded'
|
148
|
+
API_BASE_URL = URI.parse('https://api.mercadolibre.com')
|
149
|
+
|
150
|
+
def initialize(debug_logger=nil)
|
151
|
+
@http = Net::HTTP.new(API_BASE_URL.host, API_BASE_URL.port)
|
152
|
+
|
153
|
+
if API_BASE_URL.scheme == "https" # enable SSL/TLS
|
154
|
+
@http.use_ssl = true
|
155
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
156
|
+
@http.ca_file = File.join(File.dirname(__FILE__), "cacert.pem")
|
157
|
+
end
|
158
|
+
|
159
|
+
@http.set_debug_output debug_logger if debug_logger
|
160
|
+
end
|
161
|
+
|
162
|
+
def set_debug_logger(debug_logger)
|
163
|
+
@http.set_debug_output debug_logger
|
164
|
+
end
|
165
|
+
|
166
|
+
def exec(method, uri, data, content_type)
|
167
|
+
if not data.nil? and content_type == MIME_JSON
|
168
|
+
data = data.to_json
|
169
|
+
end
|
170
|
+
|
171
|
+
headers = {
|
172
|
+
'User-Agent' => "MercadoPago Ruby SDK v" + MERCADO_PAGO_VERSION,
|
173
|
+
'Content-type' => content_type,
|
174
|
+
'Accept' => MIME_JSON
|
175
|
+
}
|
176
|
+
|
177
|
+
api_result = @http.send_request(method, uri, data, headers)
|
178
|
+
|
179
|
+
{
|
180
|
+
"status" => api_result.code,
|
181
|
+
"response" => JSON.parse(api_result.body)
|
182
|
+
}
|
183
|
+
end
|
184
|
+
|
185
|
+
def get(uri, content_type=MIME_JSON)
|
186
|
+
exec("GET", uri, nil, content_type)
|
187
|
+
end
|
188
|
+
|
189
|
+
def post(uri, data = nil, content_type=MIME_JSON)
|
190
|
+
exec("POST", uri, data, content_type)
|
191
|
+
end
|
192
|
+
|
193
|
+
def put(uri, data = nil, content_type=MIME_JSON)
|
194
|
+
exec("PUT", uri, data, content_type)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
MERCADO_PAGO_VERSION = '0.1.9' unless defined?(MERCADO_PAGO_VERSION)
|
data/mercadopago.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.dirname(__FILE__) + '/lib/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'mercadopago-sdk'
|
6
|
+
gem.version = MERCADO_PAGO_VERSION
|
7
|
+
gem.authors = %w(maticompiano)
|
8
|
+
gem.email = %w(matias.compiano@mercadolibre.com)
|
9
|
+
gem.description = %q{MercadoPago Ruby SDK}
|
10
|
+
gem.summary = %q{MercadoPago Ruby SDK}
|
11
|
+
gem.homepage = 'http://github.com/mercadopago/sdk-ruby'
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($/)
|
14
|
+
gem.test_files = gem.files.grep(%r{^(tests)/})
|
15
|
+
gem.require_paths = %w(lib)
|
16
|
+
|
17
|
+
gem.add_dependency 'json'
|
18
|
+
|
19
|
+
gem.add_development_dependency 'pry'
|
20
|
+
gem.add_development_dependency 'rake'
|
21
|
+
end
|
data/tests/tests.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
$LOAD_PATH << '../lib'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'mercadopago'
|
5
|
+
require 'logger'
|
6
|
+
|
7
|
+
class MercadoPagoTest < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
@mp = MercadoPago.new("CLIENT_ID", "CLIENT_SECRET")
|
10
|
+
end
|
11
|
+
|
12
|
+
# Call preference added through button flow
|
13
|
+
def test_get_preference
|
14
|
+
preferenceData = Hash["items" => Array(Array["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"])]
|
15
|
+
|
16
|
+
preference = @mp.create_preference(preferenceData)
|
17
|
+
|
18
|
+
preference = @mp.get_preference(preference['response']['id']);
|
19
|
+
|
20
|
+
assert_equal("#{preference['status']}","200")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Create a new preference and verify that data result are ok
|
24
|
+
def test_create_preference
|
25
|
+
preference_data = {"items" => ["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
26
|
+
|
27
|
+
preference = @mp.create_preference(preference_data)
|
28
|
+
assert_equal "201", "#{preference['status']}"
|
29
|
+
assert_equal "testCreate", "#{preference['response']["items"][0]["title"]}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# We create a new preference, we modify this one and then we verify that data are ok.
|
33
|
+
def test_update_preference
|
34
|
+
preference_data = {"items" => ["title"=>"testUpdate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
35
|
+
preference_created = @mp.create_preference(preference_data)
|
36
|
+
preference_to_update = @mp.get_preference("#{preference_created['response']['id']}")
|
37
|
+
|
38
|
+
preference_data_to_update = {"items" => ["title"=>"testUpdated", "quantity"=>1, "unit_price"=>2]}
|
39
|
+
preference_update = @mp.update_preference("#{preference_created['response']['id']}", preference_data_to_update)
|
40
|
+
assert_equal "200", "#{preference_update['status']}"
|
41
|
+
|
42
|
+
preference_to_update = @mp.get_preference("#{preference_created['response']['id']}")
|
43
|
+
|
44
|
+
assert_equal "testUpdated", "#{preference_to_update['response']["items"][0]["title"]}"
|
45
|
+
assert_equal "2", "#{preference_to_update['response']["items"][0]["unit_price"]}"
|
46
|
+
assert_equal "1", "#{preference_to_update['response']["items"][0]["quantity"]}"
|
47
|
+
assert_equal "ARS", "#{preference_to_update['response']["items"][0]["currency_id"]}"
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mercadopago-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.9
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- maticompiano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-04-05 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- &id002
|
20
|
+
- ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: *id001
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pry
|
27
|
+
prerelease: false
|
28
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- *id002
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id003
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- *id002
|
39
|
+
type: :development
|
40
|
+
version_requirements: *id004
|
41
|
+
description: MercadoPago Ruby SDK
|
42
|
+
email:
|
43
|
+
- matias.compiano@mercadolibre.com
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
extra_rdoc_files: []
|
49
|
+
|
50
|
+
files:
|
51
|
+
- .gitignore
|
52
|
+
- Gemfile
|
53
|
+
- Gemfile.lock
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- examples/checkout-buttons/basic_preference/button.rb
|
57
|
+
- examples/instant-payment-notifications/receive-ipn.rb
|
58
|
+
- examples/payment-search/search-approved-payments.rb
|
59
|
+
- examples/payment-search/search-creditcard-payments.rb
|
60
|
+
- examples/payment-search/search-funded-payments-by-name.rb
|
61
|
+
- examples/payment-search/search-payments-from-email-and-date.rb
|
62
|
+
- examples/payment-search/search-payments.rb
|
63
|
+
- lib/cacert.pem
|
64
|
+
- lib/mercadopago.rb
|
65
|
+
- lib/version.rb
|
66
|
+
- mercadopago.gemspec
|
67
|
+
- tests/tests.rb
|
68
|
+
homepage: http://github.com/mercadopago/sdk-ruby
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
metadata: {}
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- *id002
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- *id002
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 2.0.3
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: MercadoPago Ruby SDK
|
91
|
+
test_files:
|
92
|
+
- tests/tests.rb
|