diff --git a/lib/stripe/account.ex b/lib/stripe/account.ex index 037ebb26..58889e0c 100644 --- a/lib/stripe/account.ex +++ b/lib/stripe/account.ex @@ -45,33 +45,6 @@ defmodule Stripe.Account do @spec do_retrieve(String.t, list) :: {:ok, t} | {:error, Exception.t} defp do_retrieve(endpoint, opts \\ []) do - IO.inspect opts - case Stripe.request(:get, endpoint, %{}, %{}, opts) do - {:ok, result} -> {:ok, to_struct(result)} - {:error, error} -> {:error, error} - end - end - - defp to_struct(response) do - %__MODULE__{ - id: Map.get(response, "id"), - business_name: Map.get(response, "business_name"), - business_primary_color: Map.get(response, "business_primary_color"), - business_url: Map.get(response, "business_url"), - charges_enabled: Map.get(response, "charges_enabled"), - country: Map.get(response, "country"), - default_currency: Map.get(response, "default_currency"), - details_submitted: Map.get(response, "details_submitted"), - display_name: Map.get(response, "display_name"), - email: Map.get(response, "email"), - managed: Map.get(response, "managed"), - metadata: Map.get(response, "metadata"), - statement_descriptor: Map.get(response, "statement_descriptor"), - support_email: Map.get(response, "support_email"), - support_phone: Map.get(response, "support_phone"), - support_url: Map.get(response, "support_url"), - timezone: Map.get(response, "timezone"), - transfers_enabled: Map.get(response, "transfers_enabled") - } + Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts) end end diff --git a/lib/stripe/card.ex b/lib/stripe/card.ex index 57bb111f..10575238 100644 --- a/lib/stripe/card.ex +++ b/lib/stripe/card.ex @@ -91,11 +91,7 @@ defmodule Stripe.Card do @spec retrieve(source, String.t, String.t, Keyword.t) :: {:ok, t} | {:error, Exception.t} def retrieve(owner_type, owner_id, card_id, opts \\ []) do endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id - - case Stripe.request(:get, endpoint, %{}, %{}, opts) do - {:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)} - {:error, error} -> {:error, error} - end + Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts) end @doc """ @@ -106,17 +102,7 @@ defmodule Stripe.Card do @spec update(source, String.t, String.t, map, Keyword.t) :: {:ok, t} | {:error, Exception.t} def update(owner_type, owner_id, card_id, changes, opts \\ []) do endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id - - card = - changes - |> Util.map_keys_to_atoms() - |> Map.take(@valid_update_keys) - |> Util.drop_nil_keys() - - case Stripe.request(:post, endpoint, card, %{}, opts) do - {:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)} - {:error, error} -> {:error, error} - end + Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts) end @doc """ @@ -125,10 +111,6 @@ defmodule Stripe.Card do @spec delete(source, String.t, String.t, Keyword.t) :: :ok | {:error, Exception.t} def delete(owner_type, owner_id, card_id, opts \\ []) do endpoint = endpoint_for_owner(owner_type, owner_id) <> "/" <> card_id - - case Stripe.request(:delete, endpoint, %{}, %{}, opts) do - {:ok, _} -> :ok - {:error, error} -> {:error, error} - end + Stripe.Request.delete(endpoint, opts) end end diff --git a/lib/stripe/customer.ex b/lib/stripe/customer.ex index 9286cacb..1d04e6c2 100644 --- a/lib/stripe/customer.ex +++ b/lib/stripe/customer.ex @@ -40,16 +40,7 @@ defmodule Stripe.Customer do @spec create(t, Keyword.t) :: {:ok, t} | {:error, Exception.t} def create(customer, opts \\ []) do endpoint = @plural_endpoint - - customer = - Map.from_struct(customer) - |> Map.take(@valid_create_keys) - |> Util.drop_nil_keys() - - case Stripe.request(:post, endpoint, customer, %{}, opts) do - {:ok, result} -> {:ok, to_struct(result)} - {:error, error} -> {:error, error} - end + Stripe.Request.create(endpoint, customer, @valid_create_keys, %__MODULE__{}, opts) end @doc """ @@ -58,10 +49,7 @@ defmodule Stripe.Customer do @spec retrieve(binary, Keyword.t) :: {:ok, t} | {:error, Exception.t} def retrieve(id, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - case Stripe.request(:get, endpoint, %{}, %{}, opts) do - {:ok, result} -> {:ok, to_struct(result)} - {:error, error} -> {:error, error} - end + Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts) end @doc """ @@ -72,17 +60,7 @@ defmodule Stripe.Customer do @spec update(t, map, list) :: {:ok, t} | {:error, Exception.t} def update(id, changes, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - - customer = - changes - |> Util.map_keys_to_atoms() - |> Map.take(@valid_update_keys) - |> Util.drop_nil_keys() - - case Stripe.request(:post, endpoint, customer, %{}, opts) do - {:ok, result} -> {:ok, to_struct(result)} - {:error, error} -> {:error, error} - end + Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts) end @doc """ @@ -91,30 +69,6 @@ defmodule Stripe.Customer do @spec delete(binary, list) :: :ok | {:error, Exception.t} def delete(id, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - - case Stripe.request(:delete, endpoint, %{}, %{}, opts) do - {:ok, _} -> :ok - {:error, error} -> {:error, error} - end - end - - defp to_struct(response) do - %__MODULE__{ - id: Map.get(response, "id"), - account_balance: Map.get(response, "account_balance"), - business_vat_id: Map.get(response, "business_vat_id"), - created: Util.get_date(response, "created"), - currency: Map.get(response, "currency"), - default_source: Map.get(response, "default_source"), - delinquent: Map.get(response, "delinquent"), - description: Map.get(response, "description"), - discount: Map.get(response, "discount"), - email: Map.get(response, "email"), - livemode: Map.get(response, "livemode"), - metadata: Map.get(response, "metadata"), - shipping: Map.get(response, "shipping"), - sources: Map.get(response, "sources"), - subscriptions: Map.get(response, "subscriptions") - } + Stripe.Request.delete(endpoint, opts) end end diff --git a/lib/stripe/plan.ex b/lib/stripe/plan.ex index 1039d774..574362dc 100644 --- a/lib/stripe/plan.ex +++ b/lib/stripe/plan.ex @@ -40,16 +40,7 @@ defmodule Stripe.Plan do @spec create(t, Keyword.t) :: stripe_response def create(plan, opts \\ []) do endpoint = @plural_endpoint - - plan = - plan - |> Map.take(@valid_create_keys) - |> Util.drop_nil_keys() - - case Stripe.request(:post, endpoint, plan, %{}, opts) do - {:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)} - {:error, error} -> {:error, error} - end + Stripe.Request.create(endpoint, plan, @valid_create_keys, %__MODULE__{}, opts) end @doc """ @@ -58,10 +49,7 @@ defmodule Stripe.Plan do @spec retrieve(binary, Keyword.t) :: stripe_response def retrieve(id, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - case Stripe.request(:get, endpoint, %{}, %{}, opts) do - {:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)} - {:error, error} -> {:error, error} - end + Stripe.Request.retrieve(endpoint, %__MODULE__{}, opts) end @doc """ @@ -72,17 +60,7 @@ defmodule Stripe.Plan do @spec update(t, map, list) :: stripe_response def update(id, changes, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - - plan = - changes - |> Util.map_keys_to_atoms() - |> Map.take(@valid_update_keys) - |> Util.drop_nil_keys() - - case Stripe.request(:post, endpoint, plan, %{}, opts) do - {:ok, result} -> {:ok, Util.stripe_response_to_struct(%__MODULE__{}, result)} - {:error, error} -> {:error, error} - end + Stripe.Request.update(endpoint, changes, @valid_update_keys, %__MODULE__{}, opts) end @doc """ @@ -91,10 +69,6 @@ defmodule Stripe.Plan do @spec delete(binary, list) :: stripe_delete_response def delete(id, opts \\ []) do endpoint = @plural_endpoint <> "/" <> id - - case Stripe.request(:delete, endpoint, %{}, %{}, opts) do - {:ok, _} -> :ok - {:error, error} -> {:error, error} - end + Stripe.Request.delete(endpoint, opts) end end diff --git a/lib/stripe/request.ex b/lib/stripe/request.ex new file mode 100644 index 00000000..39954ac9 --- /dev/null +++ b/lib/stripe/request.ex @@ -0,0 +1,49 @@ +defmodule Stripe.Request do + alias Stripe.Util + + @type stripe_response :: {:ok, struct} | {:error, Exception.t} + @type stripe_delete_response :: :ok | {:error, Exception.t} + + @spec create(String.t, struct, map, struct, Keyword.t) :: stripe_response + def create(endpoint, struct, valid_keys, return_struct, opts) do + body = + struct + |> Map.take(valid_keys) + |> Util.drop_nil_keys() + + case Stripe.request(:post, endpoint, body, %{}, opts) do + {:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} + {:error, error} -> {:error, error} + end + end + + @spec retrieve(String.t, struct, Keyword.t) :: stripe_response + def retrieve(endpoint, return_struct, opts) do + case Stripe.request(:get, endpoint, %{}, %{}, opts) do + {:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} + {:error, error} -> {:error, error} + end + end + + @spec update(String.t, map, map, struct, Keyword.t) :: stripe_response + def update(endpoint, changes, valid_keys, return_struct, opts) do + body = + changes + |> Util.map_keys_to_atoms() + |> Map.take(valid_keys) + |> Util.drop_nil_keys() + + case Stripe.request(:post, endpoint, body, %{}, opts) do + {:ok, result} -> {:ok, Util.stripe_response_to_struct(return_struct, result)} + {:error, error} -> {:error, error} + end + end + + @spec delete(String.t, Keyword.t) :: stripe_delete_response + def delete(endpoint, opts) do + case Stripe.request(:delete, endpoint, %{}, %{}, opts) do + {:ok, _} -> :ok + {:error, error} -> {:error, error} + end + end +end