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



36
37
38
# File 'lib/mercadopago/client.rb', line 36

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.



27
28
29
# File 'lib/mercadopago/client.rb', line 27

def access_token
  @access_token
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



27
28
29
# File 'lib/mercadopago/client.rb', line 27

def refresh_token
  @refresh_token
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



27
28
29
# File 'lib/mercadopago/client.rb', line 27

def sandbox
  @sandbox
end

Instance Method Details

#create_preapproval_payment(data) ⇒ Object

Creates a recurring payment.

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



85
86
87
# File 'lib/mercadopago/client.rb', line 85

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.



67
68
69
# File 'lib/mercadopago/client.rb', line 67

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

#get_preapproval_payment(preapproval_id) ⇒ Object

Returns the recurring payment.

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



94
95
96
# File 'lib/mercadopago/client.rb', line 94

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.



76
77
78
# File 'lib/mercadopago/client.rb', line 76

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.



103
104
105
106
107
108
109
110
# File 'lib/mercadopago/client.rb', line 103

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.



117
118
119
# File 'lib/mercadopago/client.rb', line 117

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.



126
127
128
# File 'lib/mercadopago/client.rb', line 126

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

#refresh_access_token(client_id, client_secret) ⇒ Object

Refreshes an access token.

  • client_id

  • client_secret



58
59
60
# File 'lib/mercadopago/client.rb', line 58

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



45
46
47
48
49
50
# File 'lib/mercadopago/client.rb', line 45

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.



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

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