Skip to content

Commit

Permalink
access via header
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgodinho-meli committed Aug 3, 2020
1 parent 97bb94c commit 2ada1f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 43 deletions.
72 changes: 30 additions & 42 deletions lib/mercadopago.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#MercadoPago Integration Library
#Access MercadoPago for payments integration
#
#@author @maticompiano
#@contributors @chrismo
# MercadoPago Integration Library
# Access MercadoPago for payments integration

require 'rubygems'
require 'json'
Expand Down Expand Up @@ -68,9 +65,7 @@ def get_payment(id)
return e.message
end

uri_prefix = @sandbox ? "/sandbox" : ""

@rest_client.get(uri_prefix + "/v1/payments/" + id + "?access_token=" + access_token)
@rest_client.get("/v1/payments/" + id, access_token)

end

Expand All @@ -86,7 +81,7 @@ def get_authorized_payment(id)
return e.message
end

@rest_client.get("/authorized_payments/" + id + "?access_token=" + access_token)
@rest_client.get("/authorized_payments/" + id, access_token)
end

# Refund accredited payment
Expand All @@ -99,7 +94,7 @@ def refund_payment(id)


refund_status = {}
@rest_client.post("/v1/payments/" + id + "/refunds?access_token=" + access_token, refund_status)
@rest_client.post("/v1/payments/" + id + "/refunds". access_token, refund_status)

end

Expand All @@ -112,7 +107,7 @@ def cancel_payment(id)
end

cancel_status = {"status" => "cancelled"}
@rest_client.put("/v1/payments/" + id + "?access_token=" + access_token, cancel_status)
@rest_client.put("/v1/payments/" + id, access_token, cancel_status)
end

# Cancel preapproval payment
Expand All @@ -124,7 +119,7 @@ def cancel_preapproval_payment(id)
end

cancel_status = {"status" => "cancelled"}
@rest_client.put("/preapproval/" + id + "?access_token=" + access_token, cancel_status)
@rest_client.put("/preapproval/" + id, access_token, cancel_status)
end

# Search payments according to filters, with pagination
Expand All @@ -140,9 +135,7 @@ def search_payment(filters, offset=0, limit=0)

filters = build_query(filters)

uri_prefix = @sandbox ? "/sandbox" : ""

@rest_client.get(uri_prefix + "/v1/payments/search?" + filters + "&access_token=" + access_token)
@rest_client.get("/v1/payments/search?" + filters, access_token)

end

Expand All @@ -154,7 +147,7 @@ def create_preference(preference)
return e.message
end

@rest_client.post("/checkout/preferences?access_token=" + access_token, preference)
@rest_client.post("/checkout/preferences", access_token, preference)
end

# Update a checkout preference
Expand All @@ -165,7 +158,7 @@ def update_preference(id, preference)
return e.message
end

@rest_client.put("/checkout/preferences/" + id + "?access_token=" + access_token, preference)
@rest_client.put("/checkout/preferences/" + id, access_token, preference)
end

# Get a checkout preference
Expand All @@ -176,7 +169,7 @@ def get_preference(id)
return e.message
end

@rest_client.get("/checkout/preferences/" + id + "?access_token=" + access_token)
@rest_client.get("/checkout/preferences/" + id, access_token)
end

# Create a preapproval payment
Expand All @@ -187,7 +180,7 @@ def create_preapproval_payment(preapproval_payment)
return e.message
end

@rest_client.post("/preapproval?access_token=" + access_token, preapproval_payment)
@rest_client.post("/preapproval", access_token, preapproval_payment)
end

# Get a preapproval payment
Expand All @@ -198,7 +191,7 @@ def get_preapproval_payment(id)
return e.message
end

@rest_client.get("/preapproval/" + id + "?access_token=" + access_token)
@rest_client.get("/preapproval/" + id, access_token)
end

# Generic resource get
Expand All @@ -207,21 +200,21 @@ def get(uri, params = nil, authenticate = true)
params = Hash.new
end

access_token = nil

if authenticate
begin
access_token = get_access_token
rescue => e
return e.message
end

params["access_token"] = access_token
end

if not params.empty?
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
end

@rest_client.get(uri)
@rest_client.get(uri, access_token)
end

# Generic resource post
Expand All @@ -236,13 +229,11 @@ def post(uri, data, params = nil)
return e.message
end

params["access_token"] = access_token

if not params.empty?
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
end

@rest_client.post(uri, data)
@rest_client.post(uri, access_token, data)
end

# Generic resource put
Expand All @@ -257,13 +248,11 @@ def put(uri, data, params = nil)
return e.message
end

params["access_token"] = access_token

if not params.empty?
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
end

@rest_client.put(uri, data)
@rest_client.put(uri, access_token, data)
end

# Generic resource delete
Expand All @@ -278,13 +267,11 @@ def delete(uri, params = nil)
return e.message
end

params["access_token"] = access_token

if not params.empty?
uri << (if uri.include? "?" then "&" else "?" end) << build_query(params)
end

@rest_client.delete(uri)
@rest_client.delete(uri, access_token)
end

def build_query(params)
Expand Down Expand Up @@ -317,7 +304,7 @@ def set_debug_logger(debug_logger)
@http.set_debug_output debug_logger
end

def exec(method, uri, data, content_type)
def exec(method, uri, access_token, data, content_type)
if not data.nil? and content_type == MIME_JSON
data = data.to_json
end
Expand All @@ -327,7 +314,8 @@ def exec(method, uri, data, content_type)
'x-tracking-id' => "platform:"+RUBY_VERSION.split('.')[0]+"|"+RUBY_VERSION+",type:SDK"+MERCADO_PAGO_VERSION+",so;",
'User-Agent' => "MercadoPago Ruby SDK v" + MERCADO_PAGO_VERSION,
'Content-type' => content_type,
'Accept' => MIME_JSON
'Accept' => MIME_JSON,
'access_token' => access_token
}

api_result = @http.send_request(method, uri, data, headers)
Expand All @@ -338,20 +326,20 @@ def exec(method, uri, data, content_type)
}
end

def get(uri, content_type=MIME_JSON)
exec("GET", uri, nil, content_type)
def get(uri, access_token, content_type=MIME_JSON)
exec("GET", uri, access_token, nil, content_type)
end

def post(uri, data = nil, content_type=MIME_JSON)
exec("POST", uri, data, content_type)
def post(uri, access_token, data = nil, content_type=MIME_JSON)
exec("POST", uri, access_token, data, content_type)
end

def put(uri, data = nil, content_type=MIME_JSON)
exec("PUT", uri, data, content_type)
def put(uri, access_token, data = nil, content_type=MIME_JSON)
exec("PUT", uri, access_token, data, content_type)
end

def delete(uri, content_type=MIME_JSON)
exec("DELETE", uri, nil, content_type)
def delete(uri, access_token, content_type=MIME_JSON)
exec("DELETE", uri, access_token, nil, content_type)
end
end
end
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
MERCADO_PAGO_VERSION = '1.1.0' unless defined?(MERCADO_PAGO_VERSION)
MERCADO_PAGO_VERSION = '1.2.1' unless defined?(MERCADO_PAGO_VERSION)
PRODUCT_ID = 'bc32a7vtrpp001u8nhjg'

0 comments on commit 2ada1f5

Please sign in to comment.