Packages

Elixir client for Mercado Pago API

Current section

Files

Jump to
mercadopago lib mercadopago structs preference.ex
Raw

lib/mercadopago/structs/preference.ex

defmodule Mercadopago.Structs.Item do
@moduledoc """
Represents an item in a Preference.
"""
@type t :: %__MODULE__{
id: String.t() | nil,
title: String.t() | nil,
description: String.t() | nil,
category_id: String.t() | nil,
picture_url: String.t() | nil,
quantity: pos_integer() | nil,
currency_id: String.t() | nil,
unit_price: float() | nil
}
defstruct [:id, :title, :description, :category_id, :picture_url, :quantity, :currency_id, :unit_price]
@doc false
@spec from_map(map()) :: t()
def from_map(attrs) when is_map(attrs) do
%__MODULE__{
id: attrs["id"],
title: attrs["title"],
description: attrs["description"],
category_id: attrs["category_id"],
picture_url: attrs["picture_url"],
quantity: attrs["quantity"],
currency_id: attrs["currency_id"],
unit_price: attrs["unit_price"]
}
end
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = struct) do
struct
|> Map.from_struct()
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|> Map.new()
end
end
defmodule Mercadopago.Structs.BackUrl do
@moduledoc """
Represents back URLs for a Preference.
"""
@type t :: %__MODULE__{
success: String.t() | nil,
pending: String.t() | nil,
failure: String.t() | nil
}
defstruct [:success, :pending, :failure]
@doc false
@spec from_map(map()) :: t()
def from_map(attrs) when is_map(attrs) do
%__MODULE__{
success: attrs["success"],
pending: attrs["pending"],
failure: attrs["failure"]
}
end
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = struct) do
struct
|> Map.from_struct()
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|> Map.new()
end
end
defmodule Mercadopago.Structs.Shipment do
@moduledoc """
Represents shipment information for a Preference.
"""
@type t :: %__MODULE__{
mode: String.t() | nil,
local_pickup: boolean() | nil,
dimensions: String.t() | nil,
default_shipping_method: pos_integer() | nil,
zip_code: String.t() | nil,
cost: float() | nil,
free_shipping: boolean() | nil,
receiver_address: map() | nil
}
defstruct [
:mode,
:local_pickup,
:dimensions,
:default_shipping_method,
:zip_code,
:cost,
:free_shipping,
:receiver_address
]
@doc false
@spec from_map(map()) :: t()
def from_map(attrs) when is_map(attrs) do
%__MODULE__{
mode: attrs["mode"],
local_pickup: attrs["local_pickup"],
dimensions: attrs["dimensions"],
default_shipping_method: attrs["default_shipping_method"],
zip_code: attrs["zip_code"],
cost: attrs["cost"],
free_shipping: attrs["free_shipping"],
receiver_address: attrs["receiver_address"]
}
end
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = struct) do
struct
|> Map.from_struct()
|> Enum.reject(fn {_k, v} -> is_nil(v) end)
|> Map.new()
end
end
defmodule Mercadopago.Structs.Preference do
@moduledoc """
Represents a Checkout Preference in Mercado Pago.
Preferences contain all the information needed to create a payment,
including items, payer info, back URLs, and more.
## Fields
* `:id` - Preference ID
* `:client_id` - Client ID
* `:collector_id` - Collector (seller) ID
* `:items` - List of items
* `:payer` - Payer information
* `:back_urls` - Redirect URLs after payment
* `:auto_return` - Auto-return mode ("approved", "all")
* `:notification_url` - Webhook notification URL
* `:external_reference` - Your internal reference ID
* `:expires` - Whether preference expires
* `:expiration_date_from` - Expiration start date
* `:expiration_date_to` - Expiration end date
* `:date_created` - Creation date
* `:date_of_expiration` - Expiration date
* `:last_updated` - Last update date
* `:live_mode` - Whether in live mode
* `:init_point` - Checkout URL
* `:sandbox_init_point` - Sandbox checkout URL
* `:payment_methods` - Payment method restrictions
* `:shipments` - Shipping information
* `:statement_descriptor` - Statement descriptor
* `:binary_mode` - Binary mode enabled
* `:metadata` - Custom metadata
"""
alias Mercadopago.Structs.{BackUrl, Item, Payer, Shipment}
@type t :: %__MODULE__{
id: String.t() | nil,
client_id: String.t() | nil,
collector_id: pos_integer() | nil,
items: [Item.t()] | nil,
payer: Payer.t() | nil,
back_urls: BackUrl.t() | nil,
auto_return: String.t() | nil,
notification_url: String.t() | nil,
external_reference: String.t() | nil,
expires: boolean() | nil,
expiration_date_from: String.t() | nil,
expiration_date_to: String.t() | nil,
date_created: String.t() | nil,
date_of_expiration: String.t() | nil,
last_updated: String.t() | nil,
live_mode: boolean() | nil,
init_point: String.t() | nil,
sandbox_init_point: String.t() | nil,
payment_methods: map() | nil,
shipments: Shipment.t() | nil,
statement_descriptor: String.t() | nil,
binary_mode: boolean() | nil,
metadata: map() | nil
}
defstruct [
:id,
:client_id,
:collector_id,
:items,
:payer,
:back_urls,
:auto_return,
:notification_url,
:external_reference,
:expires,
:expiration_date_from,
:expiration_date_to,
:date_created,
:date_of_expiration,
:last_updated,
:live_mode,
:init_point,
:sandbox_init_point,
:payment_methods,
:shipments,
:statement_descriptor,
:binary_mode,
:metadata
]
@doc false
@spec from_map(map()) :: t()
def from_map(attrs) when is_map(attrs) do
%__MODULE__{
id: attrs["id"],
client_id: attrs["client_id"],
collector_id: attrs["collector_id"],
items: parse_items(attrs["items"]),
payer: parse_payer(attrs["payer"]),
back_urls: parse_back_urls(attrs["back_urls"]),
auto_return: attrs["auto_return"],
notification_url: attrs["notification_url"],
external_reference: attrs["external_reference"],
expires: attrs["expires"],
expiration_date_from: attrs["expiration_date_from"],
expiration_date_to: attrs["expiration_date_to"],
date_created: attrs["date_created"],
date_of_expiration: attrs["date_of_expiration"],
last_updated: attrs["last_updated"],
live_mode: attrs["live_mode"],
init_point: attrs["init_point"],
sandbox_init_point: attrs["sandbox_init_point"],
payment_methods: attrs["payment_methods"],
shipments: parse_shipment(attrs["shipments"]),
statement_descriptor: attrs["statement_descriptor"],
binary_mode: attrs["binary_mode"],
metadata: attrs["metadata"]
}
end
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = struct) do
result = %{}
fields = [
:auto_return,
:notification_url,
:external_reference,
:expires,
:expiration_date_from,
:expiration_date_to,
:statement_descriptor,
:binary_mode,
:metadata
]
result =
Enum.reduce(fields, result, fn field, acc ->
value = Map.get(struct, field)
if value != nil, do: Map.put(acc, field, value), else: acc
end)
result = if struct.items, do: Map.put(result, :items, Enum.map(struct.items, &Item.to_map/1)), else: result
result = if struct.payer, do: Map.put(result, :payer, Payer.to_map(struct.payer)), else: result
result = if struct.back_urls, do: Map.put(result, :back_urls, BackUrl.to_map(struct.back_urls)), else: result
result = if struct.shipments, do: Map.put(result, :shipments, Shipment.to_map(struct.shipments)), else: result
result = if struct.payment_methods, do: Map.put(result, :payment_methods, struct.payment_methods), else: result
result
end
defp parse_items(nil), do: nil
defp parse_items(items) when is_list(items), do: Enum.map(items, &Item.from_map/1)
defp parse_payer(nil), do: nil
defp parse_payer(attrs), do: Payer.from_map(attrs)
defp parse_back_urls(nil), do: nil
defp parse_back_urls(attrs), do: BackUrl.from_map(attrs)
defp parse_shipment(nil), do: nil
defp parse_shipment(attrs), do: Shipment.from_map(attrs)
end