mercadopago-api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +27 -0
- data/lib/mercadopago-api.rb +4 -0
- data/lib/mercadopago/api.rb +63 -0
- data/lib/mercadopago/sdk.rb +128 -0
- data/lib/mercadopago/version.rb +3 -0
- data/lib/mercadopagoapi.rb +13 -0
- data/mercadopago-api.gemspec +27 -0
- data/spec/mercadopagoapi_spec.rb +34 -0
- data/spec/spec_helper.rb +16 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a264f88b690087c2ea53b0bcb7b42ce594c59f0b
|
4
|
+
data.tar.gz: a8dd0abab34f50e8535c786c233f4c6c9dc8b9ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cd48c663eab6117ce1b8bd80b8017699f64039ddbb27ab3b5dad1158d65367d90d2f85b27e749d0a75c2032fa0703a2c18b195c1b51c6212ca8e6bde88d9bb9
|
7
|
+
data.tar.gz: a253b69eafb095cdd5117982bf2cc85e87aa7aacd94d3a4b06728b3d36c03bba77c7a56802f0884de68954dfdb609859b99e2f4db43aa4b18ee9bf8ff9ee5e27
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Mercadopago::Api
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mercadopago-api'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mercadopago-api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# require 'bundler/gem_tasks'
|
2
|
+
require 'rake'
|
3
|
+
require File.expand_path('../lib/mercadopago/version', __FILE__)
|
4
|
+
|
5
|
+
desc 'Run Tests'
|
6
|
+
task :test do
|
7
|
+
sh "rspec"
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Builds the gem'
|
11
|
+
task :build do
|
12
|
+
sh "gem build mercadopago-api.gemspec"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Builds and installs the gem'
|
16
|
+
task :install => :build do
|
17
|
+
sh "gem install mercadopago-api-#{Mercadopago::VERSION}"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Tags version, pushes to remote, and pushes gem'
|
21
|
+
task :release => :build do
|
22
|
+
sh "git tag v#{Mercadopago::VERSION}"
|
23
|
+
sh "git push origin develop"
|
24
|
+
sh "git push origin v#{Mercadopago::VERSION}"
|
25
|
+
sh "gem push mercadopago-api-#{Mercadopago::VERSION}.gem"
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative "./sdk"
|
2
|
+
|
3
|
+
module Mercadopago
|
4
|
+
module Api
|
5
|
+
attr_writer :credentials, :preference
|
6
|
+
attr_reader :sdk
|
7
|
+
|
8
|
+
def checkout_link(data)
|
9
|
+
preference = sdk.create_checkout_preference(data, config[:excluded_payment_methods])
|
10
|
+
if config[:sandbox].nil?
|
11
|
+
preference['init_point']
|
12
|
+
else
|
13
|
+
preference['sandbox_init_point']
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_payment(payment_id)
|
18
|
+
if config[:sandbox]
|
19
|
+
result = sdk.search_payments_where({:id => payment_id})['results'].first
|
20
|
+
result["collection"] unless result.nil?
|
21
|
+
else
|
22
|
+
result = sdk.search_payment(payment_id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def sdk
|
27
|
+
@sdk ||= Sdk.new(credentials[:client_id], credentials[:client_secret], config[:sandbox])
|
28
|
+
end
|
29
|
+
|
30
|
+
def credentials
|
31
|
+
if @credentials.empty?
|
32
|
+
raise "Please set yours Credentials, using set_credentials"
|
33
|
+
else
|
34
|
+
@credentials
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def preference
|
39
|
+
if @preference.nil?
|
40
|
+
raise "You have to create or get a preference first"
|
41
|
+
else
|
42
|
+
@preference
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def config
|
47
|
+
@config || {}
|
48
|
+
end
|
49
|
+
|
50
|
+
def config=(config)
|
51
|
+
@config = config
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_credentials(client_id, client_secret)
|
55
|
+
@credentials = {
|
56
|
+
:client_id => client_id,
|
57
|
+
:client_secret => client_secret
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rest_client'
|
3
|
+
require 'json/ext'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module Mercadopago
|
7
|
+
class Sdk
|
8
|
+
|
9
|
+
attr_accessor :client_id, :client_secret, :sandbox, :checkout
|
10
|
+
attr_writer :access_token
|
11
|
+
|
12
|
+
def initialize(client_id, client_secret, sandbox=false)
|
13
|
+
@client_id = client_id
|
14
|
+
@client_secret = client_secret
|
15
|
+
@sandbox = sandbox
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_access_token
|
19
|
+
url = "/oauth/token"
|
20
|
+
data = {
|
21
|
+
:grant_type => "client_credentials",
|
22
|
+
:client_id => @client_id,
|
23
|
+
:client_secret => @client_secret
|
24
|
+
}
|
25
|
+
result = Rest::exec(:post, url, data)
|
26
|
+
if result[:code] == 200
|
27
|
+
@access_token = result["access_token"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def access_token
|
32
|
+
@access_token || get_access_token
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_checkout_preference(data, exclude_methods=nil)
|
36
|
+
unless exclude_methods.nil?
|
37
|
+
data[:payment_methods] = {
|
38
|
+
:excluded_payment_methods => exclude_methods
|
39
|
+
}
|
40
|
+
end
|
41
|
+
url = "/checkout/preferences?access_token="+access_token
|
42
|
+
response = Rest::exec(:post, url, data, true)
|
43
|
+
if response[:code] = 201
|
44
|
+
@checkout = response
|
45
|
+
else
|
46
|
+
response
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def update_checkout_preference(preference_id, data)
|
51
|
+
url = sandbox_prefix + "/checkout/preferences/#{preference_id}?access_token=#{access_token}"
|
52
|
+
Rest::exec(:put, url, data, true)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_checkout_preference(id)
|
56
|
+
url = sandbox_prefix + "/checkout/preferences/#{preference_id}?access_token=#{access_token}"
|
57
|
+
Rest::exec(:get, url, nil, true)
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_payment_info(notification_id)
|
61
|
+
url = sandbox_prefix + "/collections/notifications/#{notification_id}?access_token=#{access_token}"
|
62
|
+
Rest::exec(:get, url, nil, true)
|
63
|
+
end
|
64
|
+
|
65
|
+
def search_payment(payment_id)
|
66
|
+
url = sandbox_prefix + "/collections/#{payment_id}?access_token=#{access_token}"
|
67
|
+
Rest::exec(:get, url, nil, true)
|
68
|
+
end
|
69
|
+
|
70
|
+
def search_payments_where(params)
|
71
|
+
url = sandbox_prefix + "/collections/search"
|
72
|
+
params[:access_token] = access_token
|
73
|
+
Rest::exec(:get, url, { :params => params })
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_test_user(site_id)
|
77
|
+
url = "users/test_user?access_token=#{access_token}"
|
78
|
+
Rest::exec(:post, url, { :site_id => site_id }, true )
|
79
|
+
end
|
80
|
+
|
81
|
+
def refund_payment(payment_id)
|
82
|
+
url = "/collections/#{payment_id}?access_token=#{access_token}"
|
83
|
+
Rest::exec(:put, url, {:status => "refunded"}, true )
|
84
|
+
end
|
85
|
+
|
86
|
+
def cancel_payment(payment_id)
|
87
|
+
url = "/collections/#{payment_id}?access_token=#{access_token}"
|
88
|
+
Rest::exec(:put, url, {:status => "cancelled"}, true )
|
89
|
+
end
|
90
|
+
|
91
|
+
def sandbox_prefix
|
92
|
+
@sandbox ? "/sandbox":""
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
module Rest
|
98
|
+
URL = "https://api.mercadolibre.com/"
|
99
|
+
def exec(method, url, data=nil, json=false)
|
100
|
+
url = uri(url)
|
101
|
+
if !data.nil? and json
|
102
|
+
RestClient.send(method, url, data.to_json, :content_type => :json, :accept => :json) do |response, request, result|
|
103
|
+
build_response(response)
|
104
|
+
end
|
105
|
+
elsif data.nil? and json
|
106
|
+
RestClient.send(method, url, :accept => :json) do |response, request, result|
|
107
|
+
build_response(response)
|
108
|
+
end
|
109
|
+
else
|
110
|
+
RestClient.send(method, url, data) do |response, request, result|
|
111
|
+
build_response(response)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def build_response( response )
|
117
|
+
r = JSON.parse(response.force_encoding("UTF-8"))
|
118
|
+
r[:code] = response.code
|
119
|
+
return r
|
120
|
+
end
|
121
|
+
|
122
|
+
def uri(url)
|
123
|
+
URI.join(URL, url).to_s
|
124
|
+
end
|
125
|
+
|
126
|
+
module_function :exec, :uri, :build_response
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mercadopago/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mercadopago-api"
|
8
|
+
spec.version = Mercadopago::VERSION
|
9
|
+
spec.authors = ["Sergio Marin"]
|
10
|
+
spec.email = ["higher.vnf@gmail.com"]
|
11
|
+
spec.description = %q{Gem to manage the mercadopago api}
|
12
|
+
spec.summary = %q{This gem manage the Mercadopago api using rest-client and mercadopago rest api}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'json'
|
22
|
+
spec.add_dependency 'rest-client'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency 'test'
|
27
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Test Mercadopago Api Object' do
|
4
|
+
before do
|
5
|
+
@credenciales = MercadopagoTest::CREDENTIALS
|
6
|
+
end
|
7
|
+
|
8
|
+
it "The object must initialize only with credentials" do
|
9
|
+
api = MercadopagoTest.new(@credenciales[:client_id], @credenciales[:client_secret])
|
10
|
+
api.should be_kind_of(Mercadopago::Mp)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'Mercado Pago api' do
|
16
|
+
before do
|
17
|
+
@credenciales = MercadopagoTest::CREDENTIALS
|
18
|
+
@api = MercadopagoTest.new(@credenciales[:client_id], @credenciales[:client_secret])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "The object must initialize only with credentials" do
|
22
|
+
@api.should be_kind_of(Mercadopago::Mp)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "The sdk most have a token to work" do
|
26
|
+
@api.sdk.access_token.should_not be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "The object most accept config values" do
|
30
|
+
config = { :currency_id => "VEF", :sandbox => true }
|
31
|
+
@api.config = config
|
32
|
+
@api.config.should == config
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../lib/', 'mercadopagoapi.rb')
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
# setup test environment
|
5
|
+
class MercadopagoTest < Mercadopago::Mp
|
6
|
+
CREDENTIALS = {
|
7
|
+
:client_id => "5453328363694708",
|
8
|
+
:client_secret => "wxO9sB8Mer7wFzrjHUHLHC3iGygc4zgm"
|
9
|
+
}
|
10
|
+
|
11
|
+
CONFIG = {
|
12
|
+
:currency_id => "VEF",
|
13
|
+
:sandbox => true
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mercadopago-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergio Marin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rest-client
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: test
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Gem to manage the mercadopago api
|
98
|
+
email:
|
99
|
+
- higher.vnf@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/mercadopago-api.rb
|
111
|
+
- lib/mercadopago/api.rb
|
112
|
+
- lib/mercadopago/sdk.rb
|
113
|
+
- lib/mercadopago/version.rb
|
114
|
+
- lib/mercadopagoapi.rb
|
115
|
+
- mercadopago-api.gemspec
|
116
|
+
- spec/mercadopagoapi_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
homepage: ''
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.0.3
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: This gem manage the Mercadopago api using rest-client and mercadopago rest
|
142
|
+
api
|
143
|
+
test_files:
|
144
|
+
- spec/mercadopagoapi_spec.rb
|
145
|
+
- spec/spec_helper.rb
|