lib/requests/payments/sources/provider_token.ex

defmodule Requests.Payments.Sources.ProviderToken do
  alias Customers.AccountHolder

  @type t :: %__MODULE__{
          type: String.t(),
          payment_method: integer(),
          token: integer(),
          account_holder: AccountHolder.t()
        }

  @enforce_keys [:type, :payment_method, :token, :account_holder]
  defstruct [
    :type,
    :payment_method,
    :token,
    :account_holder
  ]

  def build(raw_params) when is_map(raw_params) do
    params =
      Enum.into(raw_params, %{}, fn {key, value} -> {String.to_existing_atom(key), value} end)

    %{
      type: params[:type],
      payment_method: params[:payment_method],
      token: params[:token],
      account_holder: AccountHolder.build(params[:account_holder])
    }
  end

  def build(_), do: nil
end