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



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

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

#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.



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

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.



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

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.



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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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



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

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



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

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



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

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.



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

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