Module: Mercadopago::Order::Request

Defined in:
lib/mercadopago/resources/order/request.rb

Overview

Typed, optional Request model for building an Orders API create body.

This module is an optional alternative to passing a plain Hash to #create. Each class mirrors a section of the Orders API request body (see the canonical field reference derived from the Go/.NET SDKs) and exposes #to_hash, which produces the exact snake_case Hash the API expects.

Field names, types, and nesting come strictly from the canonical Go (sdk-go/pkg/order/request.go) definition, cross-validated against .NET. Nothing is invented.

Serialization contract (DD-3)

#to_hash omits nil fields (via .compact) so the resulting JSON is identical to what an integrator would build by hand with a Hash. Nested Request objects and arrays of them are converted recursively.

Usage

req = Mercadopago::Order::Request::CreateRequest.new(
type: 'online',
total_amount: '1000.00',
payer: Mercadopago::Order::Request::PayerRequest.new(
  email: 'buyer@example.com'
)
)
sdk.order.create(req)   # accepts the typed object OR a plain Hash

Every class is built with Data.define (Ruby 3.2+; the gem requires

3.3.0). All members default to nil so only the fields you set are

sent.

Defined Under Namespace

Modules: HashConversion Classes: AddressRequest, AutomaticPaymentsRequest, ConfigRequest, CreateRequest, IdentificationRequest, IntegrationDataRequest, InvoicePeriodRequest, ItemRequest, PayerRequest, PaymentMethodRequest, PaymentRequest, PhoneRequest, ShipmentRequest, SponsorRequest, StoredCredentialRequest, SubscriptionDataRequest, SubscriptionSequenceRequest, TransactionRequest, TransactionSecurityRequest

Class Method Summary collapse

Class Method Details

.build_hash(members) ⇒ Hash{Symbol => Object}

Builds a snake_case Hash from a Data member Hash, omitting nils and recursively converting nested Request objects / arrays (DD-3).

Parameters:

  • members (Hash{Symbol => Object})

    to_h of a Data instance

Returns:

  • (Hash{Symbol => Object})

    compacted, recursively-converted Hash



63
64
65
66
67
68
69
# File 'lib/mercadopago/resources/order/request.rb', line 63

def self.build_hash(members)
  members.each_with_object({}) do |(key, value), acc|
    next if value.nil?

    acc[key] = deep_to_hash(value)
  end
end

.deep_to_hash(value) ⇒ Object

Recursively converts a value to its plain-Hash / primitive form.

  • A Request object (anything responding to to_hash that is not a Hash) is converted via its own to_hash.
  • An Array is mapped element-wise.
  • Any other value (String, Integer, Boolean, Hash, nil) passes through.

Parameters:

  • value (Object)

    value to normalize

Returns:

  • (Object)

    plain-Hash / primitive representation



48
49
50
51
52
53
54
55
56
# File 'lib/mercadopago/resources/order/request.rb', line 48

def self.deep_to_hash(value)
  if value.is_a?(Array)
    value.map { |element| deep_to_hash(element) }
  elsif !value.is_a?(Hash) && value.respond_to?(:to_hash)
    value.to_hash
  else
    value
  end
end