Packages

Elixir client for Mercado Pago API

Current section

Files

Jump to
mercadopago lib mercadopago structs customer.ex
Raw

lib/mercadopago/structs/customer.ex

defmodule Mercadopago.Structs.Customer do
@moduledoc """
Represents a Customer in Mercado Pago.
Customers allow you to save payer information and card tokens
for recurring payments.
## Fields
* `:id` - Customer ID
* `:email` - Customer email
* `:first_name` - First name
* `:last_name` - Last name
* `:phone` - Phone information
* `:identification` - Identification document
* `:address` - Customer address
* `:default_address` - Default address ID
* `:addresses` - List of addresses
* `:cards` - List of saved cards
* `:date_created` - Creation date
* `:date_last_updated` - Last update date
* `:description` - Customer description
* `:default_card` - Default card ID
* `:metadata` - Custom metadata
* `:live_mode` - Whether in live mode
"""
alias Mercadopago.Structs.{Address, Identification, Phone}
@type t :: %__MODULE__{
id: String.t() | nil,
email: String.t() | nil,
first_name: String.t() | nil,
last_name: String.t() | nil,
phone: Phone.t() | nil,
identification: Identification.t() | nil,
address: Address.t() | nil,
default_address: String.t() | nil,
addresses: [map()] | nil,
cards: [map()] | nil,
date_created: String.t() | nil,
date_last_updated: String.t() | nil,
description: String.t() | nil,
default_card: String.t() | nil,
metadata: map() | nil,
live_mode: boolean() | nil
}
defstruct [
:id,
:email,
:first_name,
:last_name,
:phone,
:identification,
:address,
:default_address,
:addresses,
:cards,
:date_created,
:date_last_updated,
:description,
:default_card,
:metadata,
:live_mode
]
@doc false
@spec from_map(map()) :: t()
def from_map(attrs) when is_map(attrs) do
%__MODULE__{
id: attrs["id"],
email: attrs["email"],
first_name: attrs["first_name"],
last_name: attrs["last_name"],
phone: parse_phone(attrs["phone"]),
identification: parse_identification(attrs["identification"]),
address: parse_address(attrs["address"]),
default_address: attrs["default_address"],
addresses: attrs["addresses"],
cards: attrs["cards"],
date_created: attrs["date_created"],
date_last_updated: attrs["date_last_updated"],
description: attrs["description"],
default_card: attrs["default_card"],
metadata: attrs["metadata"],
live_mode: attrs["live_mode"]
}
end
@doc false
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = struct) do
struct
|> Map.take([
:id,
:email,
:first_name,
:last_name,
:date_created,
:date_last_updated,
:description,
:default_address,
:default_card,
:live_mode
])
|> add_phone(struct.phone)
|> add_identification(struct.identification)
|> add_address(struct.address)
|> add_metadata(struct.metadata)
end
defp add_phone(result, nil), do: result
defp add_phone(result, phone), do: Map.put(result, :phone, Phone.to_map(phone))
defp add_identification(result, nil), do: result
defp add_identification(result, identification),
do: Map.put(result, :identification, Identification.to_map(identification))
defp add_address(result, nil), do: result
defp add_address(result, address), do: Map.put(result, :address, Address.to_map(address))
defp add_metadata(result, nil), do: result
defp add_metadata(result, metadata), do: Map.put(result, :metadata, metadata)
defp parse_phone(nil), do: nil
defp parse_phone(attrs), do: Phone.from_map(attrs)
defp parse_identification(nil), do: nil
defp parse_identification(attrs), do: Identification.from_map(attrs)
defp parse_address(nil), do: nil
defp parse_address(attrs), do: Address.from_map(attrs)
end