Class: Mercadopago::SDK

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

Overview

Main entry point and factory for the MercadoPago Ruby SDK.

Holds the OAuth access token, the HTTP transport, and the request configuration. Every API resource is exposed as a method that returns a ready-to-use resource instance (e.g. sdk.payment, sdk.customer).

Examples:

Minimal setup

sdk = Mercadopago::SDK.new('APP_ACCESS_TOKEN')
result = sdk.payment.create(transaction_amount: 100, ...)

With custom options

opts = Mercadopago::RequestOptions.new(
  access_token: 'APP_ACCESS_TOKEN',
  connection_timeout: 120.0,
  integrator_id: 'INTEGRATOR_ID'
)
sdk = Mercadopago::SDK.new('APP_ACCESS_TOKEN', request_options: opts)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, http_client: nil, request_options: nil) ⇒ SDK

Creates a new SDK instance.

Parameters:

  • access_token (String)

    OAuth access token (required)

  • http_client (HttpClient, nil) (defaults to: nil)

    custom HTTP transport; defaults to a new HttpClient

  • request_options (RequestOptions, nil) (defaults to: nil)

    custom request config; defaults to a new RequestOptions

Raises:

  • (TypeError)

    if any parameter is not the expected type



35
36
37
38
39
# File 'lib/mercadopago/sdk.rb', line 35

def initialize(access_token, http_client: nil, request_options: nil)
  self.access_token = access_token
  self.http_client = http_client.nil? ? HttpClient.new : http_client
  self.request_options = request_options.nil? ? RequestOptions.new(access_token: access_token) : request_options
end

Instance Attribute Details

#access_tokenString

Returns OAuth access token used for API authentication.

Returns:

  • (String)

    OAuth access token used for API authentication



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

def access_token
  @access_token
end

#http_clientObject

Returns the value of attribute http_client.



27
# File 'lib/mercadopago/sdk.rb', line 27

attr_reader :access_token, :http_client

Instance Method Details

#advanced_paymentAdvancedPayment

Returns resource for split-payment operations (marketplace).

Returns:

  • (AdvancedPayment)

    resource for split-payment operations (marketplace)



42
43
44
# File 'lib/mercadopago/sdk.rb', line 42

def advanced_payment
  AdvancedPayment.new(request_options, http_client)
end

#cardCard

Returns resource for managing stored cards linked to a customer.

Returns:

  • (Card)

    resource for managing stored cards linked to a customer



47
48
49
# File 'lib/mercadopago/sdk.rb', line 47

def card
  Card.new(request_options, http_client)
end

#card_tokenCardToken

Returns resource for tokenising card data server-side.

Returns:

  • (CardToken)

    resource for tokenising card data server-side



52
53
54
# File 'lib/mercadopago/sdk.rb', line 52

def card_token
  CardToken.new(request_options, http_client)
end

#customerCustomer

Returns resource for managing customer records.

Returns:

  • (Customer)

    resource for managing customer records



57
58
59
# File 'lib/mercadopago/sdk.rb', line 57

def customer
  Customer.new(request_options, http_client)
end

#disbursement_refundDisbursementRefund

Returns resource for refunding advanced-payment disbursements.

Returns:



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

def disbursement_refund
  DisbursementRefund.new(request_options, http_client)
end

#identification_typeIdentificationType

Returns resource for listing supported ID document types.

Returns:



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

def identification_type
  IdentificationType.new(request_options, http_client)
end

#merchant_orderMerchantOrder

Returns resource for grouping payments into merchant orders.

Returns:

  • (MerchantOrder)

    resource for grouping payments into merchant orders



77
78
79
# File 'lib/mercadopago/sdk.rb', line 77

def merchant_order
  MerchantOrder.new(request_options, http_client)
end

#orderOrder

Returns resource for creating and managing orders (Checkout API).

Returns:

  • (Order)

    resource for creating and managing orders (Checkout API)



112
113
114
# File 'lib/mercadopago/sdk.rb', line 112

def order
  Order.new(request_options, http_client)
end

#order_transactionOrderTransaction

Returns resource for managing transactions within an order.

Returns:



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

def order_transaction
  OrderTransaction.new(request_options, http_client)
end

#paymentPayment

Returns resource for creating, searching, and managing payments.

Returns:

  • (Payment)

    resource for creating, searching, and managing payments



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

def payment
  Payment.new(request_options, http_client)
end

#payment_methodsPaymentMethods

Returns resource for listing available payment methods.

Returns:



87
88
89
# File 'lib/mercadopago/sdk.rb', line 87

def payment_methods
  PaymentMethods.new(request_options, http_client)
end

#preapprovalPreapproval

Returns resource for managing recurring subscriptions.

Returns:

  • (Preapproval)

    resource for managing recurring subscriptions



102
103
104
# File 'lib/mercadopago/sdk.rb', line 102

def preapproval
  Preapproval.new(request_options, http_client)
end

#preapproval_planPreapprovalPlan

Returns resource for managing subscription plan templates.

Returns:



107
108
109
# File 'lib/mercadopago/sdk.rb', line 107

def preapproval_plan
  PreapprovalPlan.new(request_options, http_client)
end

#preferencePreference

Returns resource for Checkout Pro payment preferences.

Returns:

  • (Preference)

    resource for Checkout Pro payment preferences



92
93
94
# File 'lib/mercadopago/sdk.rb', line 92

def preference
  Preference.new(request_options, http_client)
end

#refundRefund

Returns resource for issuing full or partial refunds on payments.

Returns:

  • (Refund)

    resource for issuing full or partial refunds on payments



97
98
99
# File 'lib/mercadopago/sdk.rb', line 97

def refund
  Refund.new(request_options, http_client)
end

#request_optionsRequestOptions

Returns the current request options, ensuring the access token is set.

If the stored request options have a nil access token, the SDK’s access token is injected automatically.

Returns:



151
152
153
154
# File 'lib/mercadopago/sdk.rb', line 151

def request_options
  @request_options.access_token = @access_token if @request_options.access_token.nil?
  @request_options
end

#request_options=(value) ⇒ Object

Parameters:

Raises:



139
140
141
142
143
# File 'lib/mercadopago/sdk.rb', line 139

def request_options=(value)
  raise TypeError, 'Param request_options must be a RequestOptions object' unless value.is_a?(RequestOptions)

  @request_options = value
end

#userUser

Returns resource for retrieving the authenticated user’s profile.

Returns:

  • (User)

    resource for retrieving the authenticated user’s profile



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

def user
  User.new(request_options, http_client)
end