marvin-trejo11-mercadopago-sdk 0.0.1
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 +176 -0
- data/Rakefile +9 -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 +23 -0
- data/examples/payment-search/search-payments.rb +23 -0
- data/examples/preapproval-payments/button.rb +42 -0
- data/lib/mercadopago.rb +349 -0
- data/lib/ssl_options_patch.rb +15 -0
- data/lib/version.rb +1 -0
- data/mercadopago-sdk-0.3.2.gem +0 -0
- data/mercadopago-sdk-0.3.3.gem +0 -0
- data/mercadopago.gemspec +21 -0
- data/tests/tests.rb +56 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d3565c6ea3788bba45d52a7b017572fed32a881503117b4e41a5ff106e843023
|
4
|
+
data.tar.gz: 1387fb4021f5fb413f27d2d8a4757e19b6223fbe61bb109b57be1d84bf082a23
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: deab6f0cf37a84b7c5aef982508dcbd1f094d18f14896a45533a06374c653dda8538f3ba7c5377520e10d33e747253fb1119a064c56e41741a0eeb0c8b71f38e
|
7
|
+
data.tar.gz: 5057a9f85651c0c3f3e5833d13ae8c30c93c3e3ab095159cecb263928286c655aad3da3e58f6011ae1708f9ddd593da49cec3b96ebe747b06fe8f4a047b64bbb
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mercadopago-sdk (0.3.5)
|
5
|
+
json
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.0.9)
|
11
|
+
json (1.8.3)
|
12
|
+
method_source (0.8.1)
|
13
|
+
pry (0.9.12)
|
14
|
+
coderay (~> 1.0.5)
|
15
|
+
method_source (~> 0.8)
|
16
|
+
slop (~> 3.4)
|
17
|
+
rake (10.0.3)
|
18
|
+
slop (3.4.4)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
mercadopago-sdk!
|
25
|
+
pry
|
26
|
+
rake
|
data/README.md
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# MercadoPago SDK module for Payments integration
|
2
|
+
|
3
|
+
* [Install](#install)
|
4
|
+
* [Basic checkout](#basic-checkout)
|
5
|
+
* [Customized checkout](#custom-checkout)
|
6
|
+
* [Generic methods](#generic-methods)
|
7
|
+
|
8
|
+
<a name="install"></a>
|
9
|
+
## Install
|
10
|
+
|
11
|
+
```gem install mercadopago-sdk```
|
12
|
+
|
13
|
+
<a name="basic-checkout"></a>
|
14
|
+
## Basic checkout
|
15
|
+
|
16
|
+
### Configure your credentials
|
17
|
+
|
18
|
+
* Get your **CLIENT_ID** and **CLIENT_SECRET** in the following address:
|
19
|
+
* Argentina: [https://www.mercadopago.com/mla/herramientas/aplicaciones](https://www.mercadopago.com/mla/herramientas/aplicaciones)
|
20
|
+
* Brazil: [https://www.mercadopago.com/mlb/ferramentas/aplicacoes](https://www.mercadopago.com/mlb/ferramentas/aplicacoes)
|
21
|
+
* México: [https://www.mercadopago.com/mlm/herramientas/aplicaciones](https://www.mercadopago.com/mlm/herramientas/aplicaciones)
|
22
|
+
* Venezuela: [https://www.mercadopago.com/mlv/herramientas/aplicaciones](https://www.mercadopago.com/mlv/herramientas/aplicaciones)
|
23
|
+
* Colombia: [https://www.mercadopago.com/mco/herramientas/aplicaciones](https://www.mercadopago.com/mco/herramientas/aplicaciones)
|
24
|
+
* Chile: [https://www.mercadopago.com/mlc/herramientas/aplicaciones](https://www.mercadopago.com/mlc/herramientas/aplicaciones)
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'mercadopago.rb'
|
28
|
+
|
29
|
+
$mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
30
|
+
```
|
31
|
+
|
32
|
+
### Preferences
|
33
|
+
|
34
|
+
#### Get an existent Checkout preference
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
preference = $mp.get_preference('PREFERENCE_ID')
|
38
|
+
|
39
|
+
puts $preferenceResult
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Create a Checkout preference
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
|
46
|
+
preference_data = {
|
47
|
+
"items": [
|
48
|
+
{
|
49
|
+
"title": "testCreate",
|
50
|
+
"quantity": 1,
|
51
|
+
"unit_price": 10.2,
|
52
|
+
"currency_id": "ARS"
|
53
|
+
}
|
54
|
+
]
|
55
|
+
}
|
56
|
+
preference = $mp.create_preference(preference_data)
|
57
|
+
|
58
|
+
puts preference
|
59
|
+
```
|
60
|
+
|
61
|
+
#### Update an existent Checkout preference
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
preferenceDataToUpdate = Hash["items" => Array(Array["title"=>"testUpdated", "quantity"=>1, "unit_price"=>2])]
|
65
|
+
|
66
|
+
preferenceUpdate = $mp.update_preference("PREFERENCE_ID", preferenceDataToUpdate)
|
67
|
+
|
68
|
+
puts preferenceUpdate
|
69
|
+
```
|
70
|
+
|
71
|
+
### Payments/Collections
|
72
|
+
|
73
|
+
#### Search for payments
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
filters = Array["id"=>null, "external_reference"=>null]
|
77
|
+
|
78
|
+
searchResult = $mp.search_payment(filters)
|
79
|
+
|
80
|
+
puts searchResult
|
81
|
+
```
|
82
|
+
|
83
|
+
#### Get payment data
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
paymentInfo = $mp.get_payment("ID")
|
87
|
+
|
88
|
+
puts paymentInfo
|
89
|
+
```
|
90
|
+
|
91
|
+
### Cancel (only for pending payments)
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
result = $mp.cancel_payment("ID");
|
95
|
+
|
96
|
+
// Show result
|
97
|
+
puts result
|
98
|
+
```
|
99
|
+
|
100
|
+
### Refund (only for accredited payments)
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
result = $mp.refund_payment("ID");
|
104
|
+
|
105
|
+
// Show result
|
106
|
+
puts result
|
107
|
+
```
|
108
|
+
|
109
|
+
<a name="custom-checkout"></a>
|
110
|
+
## Customized checkout
|
111
|
+
|
112
|
+
### Configure your credentials
|
113
|
+
|
114
|
+
* Get your **ACCESS_TOKEN** in the following address:
|
115
|
+
* Argentina: [https://www.mercadopago.com/mla/account/credentials](https://www.mercadopago.com/mla/account/credentials)
|
116
|
+
* Brazil: [https://www.mercadopago.com/mlb/account/credentials](https://www.mercadopago.com/mlb/account/credentials)
|
117
|
+
* Mexico: [https://www.mercadopago.com/mlm/account/credentials](https://www.mercadopago.com/mlm/account/credentials)
|
118
|
+
* Venezuela: [https://www.mercadopago.com/mlv/account/credentials](https://www.mercadopago.com/mlv/account/credentials)
|
119
|
+
* Colombia: [https://www.mercadopago.com/mco/account/credentials](https://www.mercadopago.com/mco/account/credentials)
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
require 'mercadopago.rb'
|
123
|
+
|
124
|
+
$mp = MercadoPago.new('ACCESS_TOKEN')
|
125
|
+
```
|
126
|
+
|
127
|
+
### Create payment
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
$mp.post ("/v1/payments", payment_data);
|
131
|
+
```
|
132
|
+
|
133
|
+
### Create customer
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
$mp.post ("/v1/customers", Hash["email" => "email@test.com"]);
|
137
|
+
```
|
138
|
+
|
139
|
+
### Get customer
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
$mp.get ("/v1/customers/CUSTOMER_ID");
|
143
|
+
```
|
144
|
+
|
145
|
+
* View more Custom checkout related APIs in Developers Site
|
146
|
+
* Argentina: [https://www.mercadopago.com.ar/developers](https://www.mercadopago.com.ar/developers)
|
147
|
+
* Brazil: [https://www.mercadopago.com.br/developers](https://www.mercadopago.com.br/developers)
|
148
|
+
* Mexico: [https://www.mercadopago.com.mx/developers](https://www.mercadopago.com.mx/developers)
|
149
|
+
* Venezuela: [https://www.mercadopago.com.ve/developers](https://www.mercadopago.com.ve/developers)
|
150
|
+
* Colombia: [https://www.mercadopago.com.co/developers](https://www.mercadopago.com.co/developers)
|
151
|
+
|
152
|
+
<a name="generic-methods"></a>
|
153
|
+
## Generic methods
|
154
|
+
You can access any other resource from the MercadoPago API using the generic methods:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
// Get a resource, with optional URL params. Also you can disable authentication for public APIs
|
158
|
+
$mp.get ("/resource/uri", [params], [authenticate=true])
|
159
|
+
|
160
|
+
// Create a resource with "data" and optional URL params.
|
161
|
+
$mp.post ("/resource/uri", data, [params])
|
162
|
+
|
163
|
+
// Update a resource with "data" and optional URL params.
|
164
|
+
$mp.put ("/resource/uri", data, [params])
|
165
|
+
|
166
|
+
// Delete a resource with optional URL params.
|
167
|
+
$mp.delete ("/resource/uri", [params])
|
168
|
+
```
|
169
|
+
|
170
|
+
For example, if you want to get the Sites list (no params and no authentication):
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
$sites = $mp.get ("/sites", null, false)
|
174
|
+
|
175
|
+
puts $sites
|
176
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class Button
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
10
|
+
preferenceData = {"items" => ["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
11
|
+
preference = mp.create_preference(preferenceData)
|
12
|
+
|
13
|
+
html = '<!doctype html>
|
14
|
+
<html>
|
15
|
+
<head>
|
16
|
+
<title>MercadoPago SDK - Create Preference and Show Checkout Example</title>
|
17
|
+
</head>
|
18
|
+
<body>
|
19
|
+
<a href="' + preference['response']['init_point'] + '" name="MP-Checkout" class="orange-ar-m-sq-arall">Pay</a>
|
20
|
+
<script type="text/javascript" src="//resources.mlstatic.com/mptools/render.js"></script>
|
21
|
+
</body>
|
22
|
+
</html>'
|
23
|
+
|
24
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Rack::Handler::WEBrick.run(Button.new, :Port => 9000)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Get the payment reported by the IPN. Glossary of attributes response in https://developers.mercadopago.com
|
11
|
+
payment_info = mp.get_payment_info('ID');
|
12
|
+
|
13
|
+
html = ""
|
14
|
+
# Show payment information
|
15
|
+
if (payment_info['status'] == 200)
|
16
|
+
html = payment_info['response'].inspect
|
17
|
+
else
|
18
|
+
html = payment_info['response'].inspect
|
19
|
+
end
|
20
|
+
|
21
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Sets the filters you want
|
11
|
+
filters = Hash["range"=>"date_created", "begin_date"=>"NOW-1MONTH", "end_date"=>"NOW", "status"=>"approved", "operation_type"=>"regular_payment"]
|
12
|
+
|
13
|
+
# Search payment data according to filters
|
14
|
+
searchResult = mp.search_payment(filters)
|
15
|
+
|
16
|
+
# Show payment information
|
17
|
+
html = searchResult.inspect
|
18
|
+
|
19
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Sets the filters you want
|
11
|
+
filters = Hash["range"=>"date_created", "begin_date"=>"2011-10-21T00:00:00Z", "end_date"=>"2011-10-25T24:00:00Z", "payment_type_id"=>"credit_card", "operation_type"=>"regular_payment"]
|
12
|
+
|
13
|
+
# Search payment data according to filters
|
14
|
+
searchResult = mp.search_payment(filters)
|
15
|
+
|
16
|
+
# Show payment information
|
17
|
+
html = searchResult.inspect
|
18
|
+
|
19
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Sets the filters you want
|
11
|
+
filters = Hash["installments" => 12, "description"=>"product_name", "operation_type"=>"regular_payment"]
|
12
|
+
|
13
|
+
# Search payment data according to filters
|
14
|
+
searchResult = mp.search_payment(filters)
|
15
|
+
|
16
|
+
# Show payment information
|
17
|
+
html = searchResult.inspect
|
18
|
+
|
19
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Sets the filters you want
|
11
|
+
filters = Hash["payer.email" => "mail02@mail02.com", "begin_date"=>"2011-01-01T00:00:00Z", "end_date"=>"2011-02-01T00:00:00Z"]
|
12
|
+
|
13
|
+
# Search payment data according to filters
|
14
|
+
searchResult = mp.search_payment(filters)
|
15
|
+
|
16
|
+
# Show payment information
|
17
|
+
html = searchResult.inspect
|
18
|
+
|
19
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class IPN
|
7
|
+
def call(env)
|
8
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
9
|
+
|
10
|
+
# Sets the filters you want
|
11
|
+
filters = Hash["external_reference"=>"Bill001"]
|
12
|
+
|
13
|
+
# Search payment data according to filters
|
14
|
+
searchResult = mp.search_payment(filters)
|
15
|
+
|
16
|
+
# Show payment information
|
17
|
+
html = searchResult.inspect
|
18
|
+
|
19
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Rack::Handler::WEBrick.run(IPN.new, :Port => 9000)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rack'
|
3
|
+
$LOAD_PATH << '../../lib'
|
4
|
+
require 'mercadopago.rb'
|
5
|
+
|
6
|
+
class Button
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
mp = MercadoPago.new('CLIENT_ID', 'CLIENT_SECRET')
|
10
|
+
preapprovalPayment_data = Hash[
|
11
|
+
"payer_email" => "my_customer@my_site.com",
|
12
|
+
"back_url" => "http://www.my_site.com",
|
13
|
+
"reason" => "Monthly subscription to premium package",
|
14
|
+
"external_reference" => "OP-1234",
|
15
|
+
"auto_recurring" => Hash[
|
16
|
+
"frequency" => 1,
|
17
|
+
"frequency_type" => "months",
|
18
|
+
"transaction_amount" => 60,
|
19
|
+
"currency_id" => "BRL",
|
20
|
+
"start_date" => "2012-12-10T14:58:11.778-03:00",
|
21
|
+
"end_date" => "2013-06-10T14:58:11.778-03:00"
|
22
|
+
]
|
23
|
+
]
|
24
|
+
|
25
|
+
preapprovalPayment = mp.create_preapproval_payment(preapprovalPayment_data)
|
26
|
+
|
27
|
+
html = '<!doctype html>
|
28
|
+
<html>
|
29
|
+
<head>
|
30
|
+
<title>MercadoPago SDK - Create Preapproval Payment and Show Subscription Example</title>
|
31
|
+
</head>
|
32
|
+
<body>
|
33
|
+
<a href="' + preapprovalPayment['response']['init_point'] + '" name="MP-Checkout" class="orange-ar-m-sq-arall">Pay</a>
|
34
|
+
<script type="text/javascript" src="//resources.mlstatic.com/mptools/render.js"></script>
|
35
|
+
</body>
|
36
|
+
</html>'
|
37
|
+
|
38
|
+
return [200, {'Content-Type' => 'text/html'}, [html]]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Rack::Handler::WEBrick.run(Button.new, :Port => 9000)
|
data/lib/mercadopago.rb
ADDED
@@ -0,0 +1,349 @@
|
|
1
|
+
#MercadoPago Integration Library
|
2
|
+
#Access MercadoPago for payments integration
|
3
|
+
#
|
4
|
+
#@author @maticompiano
|
5
|
+
#@contributors @chrismo
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'json'
|
9
|
+
require 'uri'
|
10
|
+
require 'net/https'
|
11
|
+
require 'yaml'
|
12
|
+
require File.dirname(__FILE__) + '/version'
|
13
|
+
require 'ssl_options_patch'
|
14
|
+
|
15
|
+
class MercadoPago
|
16
|
+
def initialize(*args)
|
17
|
+
if args.size < 1 or args.size > 2
|
18
|
+
raise "Invalid arguments. Use CLIENT_ID and CLIENT SECRET, or ACCESS_TOKEN"
|
19
|
+
end
|
20
|
+
|
21
|
+
@client_id = args.at(0) if args.size == 2
|
22
|
+
@client_secret = args.at(1) if args.size == 2
|
23
|
+
@ll_access_token = args.at(0) if args.size == 1
|
24
|
+
|
25
|
+
@rest_client = RestClient.new()
|
26
|
+
@sandbox = false
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_debug_logger(debug_logger)
|
30
|
+
@rest_client.set_debug_logger(debug_logger)
|
31
|
+
end
|
32
|
+
|
33
|
+
def sandbox_mode(enable=nil)
|
34
|
+
if not enable.nil?
|
35
|
+
@sandbox = enable
|
36
|
+
end
|
37
|
+
|
38
|
+
return @sandbox
|
39
|
+
end
|
40
|
+
|
41
|
+
# Get Access Token for API use
|
42
|
+
def get_access_token
|
43
|
+
if @ll_access_token
|
44
|
+
@ll_access_token
|
45
|
+
else
|
46
|
+
app_client_values = {
|
47
|
+
'grant_type' => 'client_credentials',
|
48
|
+
'client_id' => @client_id,
|
49
|
+
'client_secret' => @client_secret
|
50
|
+
}
|
51
|
+
|
52
|
+
@access_data = @rest_client.post("/oauth/token", build_query(app_client_values), RestClient::MIME_FORM)
|
53
|
+
|
54
|
+
if @access_data['status'] == "200"
|
55
|
+
@access_data = @access_data["response"]
|
56
|
+
@access_data['access_token']
|
57
|
+
else
|
58
|
+
raise @access_data.inspect
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Get information for specific payment
|
64
|
+
def get_payment(id)
|
65
|
+
begin
|
66
|
+
access_token = get_access_token
|
67
|
+
rescue => e
|
68
|
+
return e.message
|
69
|
+
end
|
70
|
+
|
71
|
+
uri_prefix = @sandbox ? "/sandbox" : ""
|
72
|
+
@rest_client.get("/v1/payments/" + id + "?access_token=" + access_token)
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_payment_info(id)
|
76
|
+
get_payment(id)
|
77
|
+
end
|
78
|
+
|
79
|
+
# Get information for specific authorized payment
|
80
|
+
def get_authorized_payment(id)
|
81
|
+
begin
|
82
|
+
access_token = get_access_token
|
83
|
+
rescue => e
|
84
|
+
return e.message
|
85
|
+
end
|
86
|
+
|
87
|
+
@rest_client.get("/authorized_payments/" + id + "?access_token=" + access_token)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Refund accredited payment
|
91
|
+
def refund_payment(id)
|
92
|
+
begin
|
93
|
+
access_token = get_access_token
|
94
|
+
rescue => e
|
95
|
+
return e.message
|
96
|
+
end
|
97
|
+
|
98
|
+
refund_status = {}
|
99
|
+
@rest_client.post("/v1/payments/" + id + "/refunds?access_token=" + access_token, refund_status)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Cancel pending payment
|
103
|
+
def cancel_payment(id)
|
104
|
+
begin
|
105
|
+
access_token = get_access_token
|
106
|
+
rescue => e
|
107
|
+
return e.message
|
108
|
+
end
|
109
|
+
|
110
|
+
cancel_status = {"status" => "cancelled"}
|
111
|
+
@rest_client.put("/v1/payments/" + id + "?access_token=" + access_token, cancel_status)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Cancel preapproval payment
|
115
|
+
def cancel_preapproval_payment(id)
|
116
|
+
begin
|
117
|
+
access_token = get_access_token
|
118
|
+
rescue => e
|
119
|
+
return e.message
|
120
|
+
end
|
121
|
+
|
122
|
+
cancel_status = {"status" => "cancelled"}
|
123
|
+
@rest_client.put("/preapproval/" + id + "?access_token=" + access_token, cancel_status)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Search payments according to filters, with pagination
|
127
|
+
def search_payment(filters, offset=0, limit=0)
|
128
|
+
begin
|
129
|
+
access_token = get_access_token
|
130
|
+
rescue => e
|
131
|
+
return e.message
|
132
|
+
end
|
133
|
+
|
134
|
+
filters["offset"] = offset
|
135
|
+
filters["limit"] = limit
|
136
|
+
|
137
|
+
filters = build_query(filters)
|
138
|
+
|
139
|
+
uri_prefix = @sandbox ? "/sandbox" : ""
|
140
|
+
@rest_client.get("/v1/payments/search?" + filters + "&access_token=" + access_token)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Create a checkout preference
|
144
|
+
def create_preference(preference)
|
145
|
+
begin
|
146
|
+
access_token = get_access_token
|
147
|
+
rescue => e
|
148
|
+
return e.message
|
149
|
+
end
|
150
|
+
|
151
|
+
@rest_client.post("/checkout/preferences?access_token=" + access_token, preference)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Update a checkout preference
|
155
|
+
def update_preference(id, preference)
|
156
|
+
begin
|
157
|
+
access_token = get_access_token
|
158
|
+
rescue => e
|
159
|
+
return e.message
|
160
|
+
end
|
161
|
+
|
162
|
+
@rest_client.put("/checkout/preferences/" + id + "?access_token=" + access_token, preference)
|
163
|
+
end
|
164
|
+
|
165
|
+
# Get a checkout preference
|
166
|
+
def get_preference(id)
|
167
|
+
begin
|
168
|
+
access_token = get_access_token
|
169
|
+
rescue => e
|
170
|
+
return e.message
|
171
|
+
end
|
172
|
+
|
173
|
+
@rest_client.get("/checkout/preferences/" + id + "?access_token=" + access_token)
|
174
|
+
end
|
175
|
+
|
176
|
+
# Create a preapproval payment
|
177
|
+
def create_preapproval_payment(preapproval_payment)
|
178
|
+
begin
|
179
|
+
access_token = get_access_token
|
180
|
+
rescue => e
|
181
|
+
return e.message
|
182
|
+
end
|
183
|
+
|
184
|
+
@rest_client.post("/preapproval?access_token=" + access_token, preapproval_payment)
|
185
|
+
end
|
186
|
+
|
187
|
+
# Get a preapproval payment
|
188
|
+
def get_preapproval_payment(id)
|
189
|
+
begin
|
190
|
+
access_token = get_access_token
|
191
|
+
rescue => e
|
192
|
+
return e.message
|
193
|
+
end
|
194
|
+
|
195
|
+
@rest_client.get("/preapproval/" + id + "?access_token=" + access_token)
|
196
|
+
end
|
197
|
+
|
198
|
+
# Generic resource get
|
199
|
+
def get(uri, params = nil, authenticate = true)
|
200
|
+
if not params.class == Hash
|
201
|
+
params = Hash.new
|
202
|
+
end
|
203
|
+
|
204
|
+
if authenticate
|
205
|
+
begin
|
206
|
+
access_token = get_access_token
|
207
|
+
rescue => e
|
208
|
+
return e.message
|
209
|
+
end
|
210
|
+
|
211
|
+
params["access_token"] = access_token
|
212
|
+
end
|
213
|
+
|
214
|
+
if not params.empty?
|
215
|
+
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
|
216
|
+
end
|
217
|
+
|
218
|
+
@rest_client.get(uri)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Generic resource post
|
222
|
+
def post(uri, data, params = nil)
|
223
|
+
if not params.class == Hash
|
224
|
+
params = Hash.new
|
225
|
+
end
|
226
|
+
|
227
|
+
begin
|
228
|
+
access_token = get_access_token
|
229
|
+
rescue => e
|
230
|
+
return e.message
|
231
|
+
end
|
232
|
+
|
233
|
+
params["access_token"] = access_token
|
234
|
+
|
235
|
+
if not params.empty?
|
236
|
+
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
|
237
|
+
end
|
238
|
+
|
239
|
+
@rest_client.post(uri, data)
|
240
|
+
end
|
241
|
+
|
242
|
+
# Generic resource put
|
243
|
+
def put(uri, data, params = nil)
|
244
|
+
if not params.class == Hash
|
245
|
+
params = Hash.new
|
246
|
+
end
|
247
|
+
|
248
|
+
begin
|
249
|
+
access_token = get_access_token
|
250
|
+
rescue => e
|
251
|
+
return e.message
|
252
|
+
end
|
253
|
+
|
254
|
+
params["access_token"] = access_token
|
255
|
+
|
256
|
+
if not params.empty?
|
257
|
+
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
|
258
|
+
end
|
259
|
+
|
260
|
+
@rest_client.put(uri, data)
|
261
|
+
end
|
262
|
+
|
263
|
+
# Generic resource delete
|
264
|
+
def delete(uri, params = nil)
|
265
|
+
if not params.class == Hash
|
266
|
+
params = Hash.new
|
267
|
+
end
|
268
|
+
|
269
|
+
begin
|
270
|
+
access_token = get_access_token
|
271
|
+
rescue => e
|
272
|
+
return e.message
|
273
|
+
end
|
274
|
+
|
275
|
+
params["access_token"] = access_token
|
276
|
+
|
277
|
+
if not params.empty?
|
278
|
+
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
|
279
|
+
end
|
280
|
+
|
281
|
+
@rest_client.delete(uri)
|
282
|
+
end
|
283
|
+
|
284
|
+
def build_query(params)
|
285
|
+
URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
|
286
|
+
end
|
287
|
+
|
288
|
+
private
|
289
|
+
|
290
|
+
class RestClient
|
291
|
+
|
292
|
+
MIME_JSON = 'application/json'
|
293
|
+
MIME_FORM = 'application/x-www-form-urlencoded'
|
294
|
+
API_BASE_URL = URI.parse('https://api.mercadopago.com')
|
295
|
+
|
296
|
+
def initialize(debug_logger=nil)
|
297
|
+
@http = Net::HTTP.new(API_BASE_URL.host, API_BASE_URL.port)
|
298
|
+
|
299
|
+
if API_BASE_URL.scheme == "https" # enable SSL/TLS
|
300
|
+
@http.use_ssl = true
|
301
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
302
|
+
|
303
|
+
# explicitly tell OpenSSL not to use SSL3 nor TLS 1.0
|
304
|
+
@http.ssl_options = OpenSSL::SSL::OP_NO_SSLv3 + OpenSSL::SSL::OP_NO_TLSv1
|
305
|
+
end
|
306
|
+
|
307
|
+
@http.set_debug_output debug_logger if debug_logger
|
308
|
+
end
|
309
|
+
|
310
|
+
def set_debug_logger(debug_logger)
|
311
|
+
@http.set_debug_output debug_logger
|
312
|
+
end
|
313
|
+
|
314
|
+
def exec(method, uri, data, content_type)
|
315
|
+
if not data.nil? and content_type == MIME_JSON
|
316
|
+
data = data.to_json
|
317
|
+
end
|
318
|
+
|
319
|
+
headers = {
|
320
|
+
'User-Agent' => "MercadoPago Ruby SDK v" + MERCADO_PAGO_VERSION,
|
321
|
+
'Content-type' => content_type,
|
322
|
+
'Accept' => MIME_JSON
|
323
|
+
}
|
324
|
+
|
325
|
+
api_result = @http.send_request(method, uri, data, headers)
|
326
|
+
|
327
|
+
{
|
328
|
+
"status" => api_result.code,
|
329
|
+
"response" => JSON.parse(api_result.body)
|
330
|
+
}
|
331
|
+
end
|
332
|
+
|
333
|
+
def get(uri, content_type=MIME_JSON)
|
334
|
+
exec("GET", uri, nil, content_type)
|
335
|
+
end
|
336
|
+
|
337
|
+
def post(uri, data = nil, content_type=MIME_JSON)
|
338
|
+
exec("POST", uri, data, content_type)
|
339
|
+
end
|
340
|
+
|
341
|
+
def put(uri, data = nil, content_type=MIME_JSON)
|
342
|
+
exec("PUT", uri, data, content_type)
|
343
|
+
end
|
344
|
+
|
345
|
+
def delete(uri, content_type=MIME_JSON)
|
346
|
+
exec("DELETE", uri, nil, content_type)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# This is a workaround for issue #9450 in Ruby core
|
2
|
+
# https://bugs.ruby-lang.org/issues/9450
|
3
|
+
#
|
4
|
+
# Add an :ssl_options accessor to Net::HTTP, which controls the options
|
5
|
+
# attribute on the resulting SSLContext. This allows the user to customize
|
6
|
+
# behavior of SSL connections, like disabling specific protocol versions.
|
7
|
+
|
8
|
+
require 'net/http'
|
9
|
+
|
10
|
+
# (Net::HTTP::SSL_IVNAMES << :@ssl_options).uniq!
|
11
|
+
(Net::HTTP::SSL_ATTRIBUTES << :options).uniq!
|
12
|
+
|
13
|
+
Net::HTTP.class_eval do
|
14
|
+
attr_accessor :ssl_options
|
15
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
MERCADO_PAGO_VERSION = '0.3.5' unless defined?(MERCADO_PAGO_VERSION)
|
Binary file
|
Binary file
|
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 = 'marvin-trejo11-mercadopago-sdk'
|
6
|
+
gem.version = "0.0.1"
|
7
|
+
gem.authors = %w(Marvin Trejo)
|
8
|
+
gem.email = %w(marvintrejo11@gmail.com)
|
9
|
+
gem.description = %q{MercadoPago Ruby SDK}
|
10
|
+
gem.summary = %q{MercadoPago Ruby SDK}
|
11
|
+
gem.homepage = 'http://github.com/marvin-trejo11/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,56 @@
|
|
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_long_live_access_token
|
14
|
+
@mp = MercadoPago.new("LONG_LIVE_ACCESS_TOKEN")
|
15
|
+
|
16
|
+
assert_equal(@mp.get_access_token(), "LONG_LIVE_ACCESS_TOKEN")
|
17
|
+
end
|
18
|
+
|
19
|
+
# Call preference added through button flow
|
20
|
+
def test_get_preference
|
21
|
+
preferenceData = {"items" => ["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
22
|
+
|
23
|
+
preference = @mp.create_preference(preferenceData)
|
24
|
+
|
25
|
+
preference = @mp.get_preference(preference['response']['id']);
|
26
|
+
|
27
|
+
assert_equal("#{preference['status']}","200")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Create a new preference and verify that data result are ok
|
31
|
+
def test_create_preference
|
32
|
+
preference_data = {"items" => ["title"=>"testCreate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
33
|
+
|
34
|
+
preference = @mp.create_preference(preference_data)
|
35
|
+
assert_equal "201", "#{preference['status']}"
|
36
|
+
assert_equal "testCreate", "#{preference['response']["items"][0]["title"]}"
|
37
|
+
end
|
38
|
+
|
39
|
+
# We create a new preference, we modify this one and then we verify that data are ok.
|
40
|
+
def test_update_preference
|
41
|
+
preference_data = {"items" => ["title"=>"testUpdate", "quantity"=>1, "unit_price"=>10.2, "currency_id"=>"ARS"]}
|
42
|
+
preference_created = @mp.create_preference(preference_data)
|
43
|
+
preference_to_update = @mp.get_preference("#{preference_created['response']['id']}")
|
44
|
+
|
45
|
+
preference_data_to_update = {"items" => ["title"=>"testUpdated", "quantity"=>1, "unit_price"=>2]}
|
46
|
+
preference_update = @mp.update_preference("#{preference_created['response']['id']}", preference_data_to_update)
|
47
|
+
assert_equal "200", "#{preference_update['status']}"
|
48
|
+
|
49
|
+
preference_to_update = @mp.get_preference("#{preference_created['response']['id']}")
|
50
|
+
|
51
|
+
assert_equal "testUpdated", "#{preference_to_update['response']["items"][0]["title"]}"
|
52
|
+
assert_equal "2", "#{preference_to_update['response']["items"][0]["unit_price"]}"
|
53
|
+
assert_equal "1", "#{preference_to_update['response']["items"][0]["quantity"]}"
|
54
|
+
assert_equal "ARS", "#{preference_to_update['response']["items"][0]["currency_id"]}"
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marvin-trejo11-mercadopago-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marvin
|
8
|
+
- Trejo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-08-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: pry
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: MercadoPago Ruby SDK
|
57
|
+
email:
|
58
|
+
- marvintrejo11@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- examples/checkout-buttons/basic_preference/button.rb
|
69
|
+
- examples/instant-payment-notifications/receive-ipn.rb
|
70
|
+
- examples/payment-search/search-approved-payments.rb
|
71
|
+
- examples/payment-search/search-creditcard-payments.rb
|
72
|
+
- examples/payment-search/search-funded-payments-by-name.rb
|
73
|
+
- examples/payment-search/search-payments-from-email-and-date.rb
|
74
|
+
- examples/payment-search/search-payments.rb
|
75
|
+
- examples/preapproval-payments/button.rb
|
76
|
+
- lib/mercadopago.rb
|
77
|
+
- lib/ssl_options_patch.rb
|
78
|
+
- lib/version.rb
|
79
|
+
- mercadopago-sdk-0.3.2.gem
|
80
|
+
- mercadopago-sdk-0.3.3.gem
|
81
|
+
- mercadopago.gemspec
|
82
|
+
- tests/tests.rb
|
83
|
+
homepage: http://github.com/marvin-trejo11/sdk-ruby
|
84
|
+
licenses: []
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.7.7
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: MercadoPago Ruby SDK
|
106
|
+
test_files:
|
107
|
+
- tests/tests.rb
|