defmodule ExCoinbase.Checkout do import ExCoinbase.Utils, only: [response_processing: 1, build_body: 1] @moduledoc """ All routes for checkout """ @url "https://api.commerce.coinbase.com/checkouts" defstruct [:name, :description, :pricing_type, :local_price, :requested_info] @doc """ Lists all the checkouts """ @spec list :: {:ok, {integer(), Poison.Parser.t()}} | {:error, Exception.t()} | {:error, HTTPoison.Error.t()} def list do HTTPoison.get(@url, headers()) |> response_processing() end @doc """ Show a single checkout by id """ @spec show(integer() | String.t()) :: {:ok, {integer(), Poison.Parser.t()}} | {:error, Exception.t()} | {:error, HTTPoison.Error.t()} def show(checkout_id) do HTTPoison.get("#{@url}/#{checkout_id}", headers()) |> response_processing() end @doc """ Create a single checkout """ @spec create(%ExCoinbase.Checkout{}) :: {:ok, {integer(), Poison.Parser.t()}} | {:error, Exception.t()} | {:error, HTTPoison.Error.t()} def create(fields) do with {:ok, body} <- build_body(fields) do HTTPoison.post(@url, body, headers()) |> response_processing() else exception -> exception end end @doc """ Update a single checkout by id sending body with the updates """ @spec update(%ExCoinbase.Checkout{}, String.t()) :: {:ok, {integer(), Poison.Parser.t()}} | {:error, Exception.t()} | {:error, HTTPoison.Error.t()} def update(fields, checkout_id) do with {:ok, body} <- build_body(fields) do HTTPoison.put("#{@url}/#{checkout_id}", body, headers()) |> response_processing() else exception -> exception end end @doc """ Delete a single checkout by id """ @spec delete(integer()) :: {:ok, {integer(), Poison.Parser.t()}} | {:error, Exception.t()} | {:error, HTTPoison.Error.t()} def delete(checkout_id) do HTTPoison.delete("#{@url}/#{checkout_id}", headers()) |> response_processing() end defp get_api_key do Application.fetch_env!(:ex_coinbase, :api_key) end defp headers do ["X-CC-Api-Key": get_api_key(), "X-CC-Version": "2018-03-22", "content-type": "application/json"] end end