Class: MpApi::Client

Inherits:
Ac::Base
  • Object
show all
Defined in:
lib/mp_api/client.rb

Constant Summary collapse

BASE_URL =
"https://api.mercadopago.com/v1/"
MAX_RETRIES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = MpApi.configuration.access_token) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
# File 'lib/mp_api/client.rb', line 6

def initialize(access_token=MpApi.configuration.access_token)
  @headers = {
    "Content-Type": "application/json",
    'x-idempotency-key' => SecureRandom.uuid
  }
  super access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



5
6
7
# File 'lib/mp_api/client.rb', line 5

def access_token
  @access_token
end

Instance Method Details

#create_payment(body) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/mp_api/client.rb', line 14

def create_payment(body)
  response = post("/payments", body: body, headers: @headers) do |response|
    p response.json unless response.success?
    response.json['id']
  end
  response.json
end

#create_token(card_token_data) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mp_api/client.rb', line 30

def create_token(card_token_data)
  response = post("/card_tokens", body: card_token_data, headers: @headers) do |response|
    p response.json unless response.success?
    response.json['id']
  end
  response.json
end

#get_payment(payment_id) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mp_api/client.rb', line 22

def get_payment(payment_id)
    response = get("/payments/#{payment_id}") do |response|
      p response.json unless response.success?
      response.json['id']
    end
    response.json
end

#get_payment_methodsObject



46
47
48
49
50
51
52
# File 'lib/mp_api/client.rb', line 46

def get_payment_methods
  response = get("/payment_methods") do |response|
    p response.json unless response.success?
    response.json['id']
  end
  response.json
end

#search_payment_methods(query) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/mp_api/client.rb', line 38

def search_payment_methods(query)
  response = get("/payment_methods/search", params: query) do |response|
    p response.json unless response.success?
    response.json['results'][0]
  end
  response.json
end