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

Raises:



14
15
16
17
18
19
20
21
# 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?
    ![500, 429].include?(response.code) || response['id']
  end
  raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
  response.json
end

#create_token(card_token_data) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
# File 'lib/mp_api/client.rb', line 32

def create_token(card_token_data)
  response = post("/card_tokens", body: card_token_data, headers: @headers) do |response|
    p response.json unless response.success?
    ![500, 429].include?(response.code) || response['id']
  end
  raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
  response.json
end

#get_payment(payment_id) ⇒ Object

Raises:



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

def get_payment(payment_id)
  response = get("/payments/#{payment_id}") do |response|
    p response.json unless response.success?
    ![500, 429].include?(response.code) || response['id']
  end
  raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
  response.json
end

#get_payment_methodsObject

Raises:



50
51
52
53
54
55
56
57
# File 'lib/mp_api/client.rb', line 50

def get_payment_methods
  response = get("/payment_methods") do |response|
    p response.json unless response.success?
    ![500, 429].include?(response.code) || response['id']
  end
  raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
  response.json
end

#search_payment_methods(query) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
# File 'lib/mp_api/client.rb', line 41

def search_payment_methods(query)
  response = get("/payment_methods/search", params: query) do |response|
    p response.json unless response.success?
    ![500, 429].include?(response.code) || response.json['results'][0]
  end
  raise RequestError.new(response.json['message']) if ![200,201].include?(response.code)
  response.json
end