lib/requests/payments/sources.ex

defmodule Requests.Payments.Sources do
    @moduledoc false
  @sources [
    "ali_pay_cn",
    "ali_pay_hk",
    "ali_pay_plus",
    "bank_account",
    "benifit",
    "card",
    "customer",
    "eps",
    "fawry",
    "g_cash",
    "giro_pay",
    "id",
    "ideal",
    "network_token",
    "pay_pal",
    "provider_token",
    "sofort",
    "token",
    "we_chat_pay"
  ]

  def build(%{type: type} = params) when type in @sources do
    type
    |> Macro.camelize()
    |> build_atom()
    |> String.to_existing_atom()
    |> Kernel.apply(:build, [params])
  end

  def build(%{"type" => type} = params) when type in @sources do
    type
    |> Macro.camelize()
    |> build_atom()
    |> String.to_existing_atom()
    |> Kernel.apply(:build, [params])
  end

  def build(_), do: nil

  defp build_atom(string) when is_binary(string) do
    "#{__MODULE__}.#{string}"
  end
end