Class: MercadoPago::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mercadopago/client.rb

Overview

You can create a Client object to interact with a MercadoPago account through the API.

You will need client_id and client_secret. Client will save the token as part of its inner state. It will use it to call API methods.

Usage example:

mp_client = MercadoPago::Client.new(client_id, client_secret)

mp_client.create_preference(data)
mp_client.get_preference(preference_id)
mp_client.notification(payment_id)
mp_client.search(data)
mp_client.sandbox_mode(true/false)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Client

Creates an instance and stores the access_token to make calls to the MercadoPago API.

  • client_id

  • client_secret



40
41
42
43
# File 'lib/mercadopago/client.rb', line 40

def initialize(client_id, client_secret)
  load_tokens MercadoPago::Authentication.access_token(client_id,
                                                       client_secret)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



30
31
32
# File 'lib/mercadopago/client.rb', line 30

def access_token
  @access_token
end

#auto_paginateObject

Returns the value of attribute auto_paginate.



31
32
33
# File 'lib/mercadopago/client.rb', line 31

def auto_paginate
  @auto_paginate
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



30
31
32
# File 'lib/mercadopago/client.rb', line 30

def refresh_token
  @refresh_token
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



30
31
32
# File 'lib/mercadopago/client.rb', line 30

def sandbox
  @sandbox
end

Instance Method Details

#cancel_preapproval_payment(preapproval_id) ⇒ Object

Cancels a recurring payment.

  • preapproval_id: the id of the preapproval payment preference that will be canceled.



110
111
112
113
# File 'lib/mercadopago/client.rb', line 110

def cancel_preapproval_payment(preapproval_id)
  MercadoPago::Checkout.cancel_preapproval_payment(@access_token,
                                                   preapproval_id)
end

#create_preapproval_payment(data) ⇒ Object

Creates a recurring payment.

  • data: contains the data according to the recurring payment that will be created.



91
92
93
# File 'lib/mercadopago/client.rb', line 91

def create_preapproval_payment(data)
  MercadoPago::Checkout.create_preapproval_payment(@access_token, data)
end

#create_preference(data) ⇒ Object

Creates a payment preference.

  • data: contains the data according to the payment preference that will be created.



73
74
75
# File 'lib/mercadopago/client.rb', line 73

def create_preference(data)
  MercadoPago::Checkout.create_preference(@access_token, data)
end

#get(url, data = {}, headers = nil) ⇒ Object

Performs a generic GET to the given URL

  • url: the URL to request



163
164
165
166
167
168
# File 'lib/mercadopago/client.rb', line 163

def get(url, data = {}, headers = nil)
  data[:access_token] = @access_token
  query = URI.encode_www_form data

  MercadoPago::Request.wrap_get("#{url}?{query}", headers)
end

#get_preapproval_payment(preapproval_id) ⇒ Object

Returns the recurring payment.

  • preapproval_id: the id of the preapproval payment preference that will be retrieved.



100
101
102
103
# File 'lib/mercadopago/client.rb', line 100

def get_preapproval_payment(preapproval_id)
  MercadoPago::Checkout.get_preapproval_payment(@access_token,
                                                preapproval_id)
end

#get_preference(preference_id) ⇒ Object

Returns the payment preference.

  • preference_id: the id of the payment preference that will be retrieved.



82
83
84
# File 'lib/mercadopago/client.rb', line 82

def get_preference(preference_id)
  MercadoPago::Checkout.get_preference(@access_token, preference_id)
end

#notification(entity_id, topic = 'payment') ⇒ Object

Retrieves the latest information about a payment or a merchant order.

  • entity_id: the id of the entity (paymento or merchant order) to be checked.



120
121
122
123
124
125
126
127
# File 'lib/mercadopago/client.rb', line 120

def notification(entity_id, topic = 'payment')
  case topic.to_s
  when 'merchant_order'
    MercadoPago::MerchantOrder.notification(@access_token, entity_id)
  else # 'payment'
    MercadoPago::Collection.notification(@access_token, entity_id, @sandbox)
  end
end

#notification_authorized(authorized_id) ⇒ Object

Retrieves the latest information about the recurring payment after authorized.

  • authorized_id: the id of the recurring payment authorized to be checked.



134
135
136
137
# File 'lib/mercadopago/client.rb', line 134

def notification_authorized(authorized_id)
  MercadoPago::Collection.notification_authorized(@access_token,
                                                  authorized_id)
end

#notification_preapproval(preapproval_id) ⇒ Object

Retrieves the latest information about the recurring payment.

  • preapproval_id: the id of the recurring payment to be checked.



144
145
146
147
# File 'lib/mercadopago/client.rb', line 144

def notification_preapproval(preapproval_id)
  MercadoPago::Collection.notification_preapproval(@access_token,
                                                   preapproval_id)
end

#post(url, data, headers = nil) ⇒ Object

Performs a generic POST to the given URL

  • url: the URL to request

  • data: the data to send along the request



176
177
178
179
180
# File 'lib/mercadopago/client.rb', line 176

def post(url, data, headers = nil)
  payload = JSON.generate(data)

  MercadoPago::Request.wrap_post("#{url}?access_token=#{@access_token}", payload, headers)
end

#refresh_access_token(client_id, client_secret) ⇒ Object

Refreshes an access token.

  • client_id

  • client_secret



63
64
65
66
# File 'lib/mercadopago/client.rb', line 63

def refresh_access_token(client_id, client_secret)
  load_tokens(MercadoPago::Authentication
    .refresh_access_token(client_id, client_secret, @refresh_token))
end

#sandbox_mode(enable = nil) ⇒ Object

Enables or disables sandbox mode.

  • enable



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

def sandbox_mode(enable = nil)
  unless enable.nil?
    @sandbox = enable
  end
  @sandbox
end

#search(search_hash) ⇒ Object

Searches for collections that matches some of the search hash criteria.

  • search_hash: the search hash to find collections.



154
155
156
# File 'lib/mercadopago/client.rb', line 154

def search(search_hash)
  MercadoPago::Collection.search(@access_token, search_hash, @sandbox, auto_paginate)
end