Packages

A production-grade, fully-typed Elixir client for the SumUp API (Checkouts, Readers, Customers, Transactions, Payouts, Receipts, Members, Memberships, Roles and Merchants) with telemetry, retries, cursor streaming, webhook handling, and RFC 9457 aware error handling.

Current section

Files

Jump to
sumup lib sumup payouts.ex
Raw

lib/sumup/payouts.ex

defmodule Sumup.Payouts do
@moduledoc """
Payouts: a date-ranged report of funds settled to (or deducted from) a
merchant's bank account. API version `v1.0`.
Unlike every other resource, this endpoint supports content negotiation
between JSON and CSV via the `format:` option — `list/3` returns
`{:ok, [Sumup.Payout.t()]}` for `format: :json` (the default) and
`{:ok, csv_string}` for `format: :csv`.
"""
alias Sumup.{Client, Config, Payout}
@list_schema NimbleOptions.new!(
start_date: [
type: :string,
required: true,
doc: "ISO 8601 date, e.g. `\"2026-01-01\"`."
],
end_date: [
type: :string,
required: true,
doc: "ISO 8601 date, e.g. `\"2026-01-31\"`."
],
format: [type: {:in, [:json, :csv]}, default: :json, doc: "Response format."],
limit: [type: :pos_integer, doc: "Max number of entries to return (1-9999)."],
order: [
type: {:in, [:asc, :desc]},
default: :asc,
doc: "Sort direction by date."
]
)
@doc """
Fetches the payout report for a merchant over a date range.
#{NimbleOptions.docs(@list_schema)}
"""
@spec list(Config.t(), String.t(), keyword()) ::
{:ok, [Payout.t()]} | {:ok, String.t()} | {:error, Sumup.Error.t()}
def list(%Config{} = config, merchant_code, opts) do
opts = NimbleOptions.validate!(opts, @list_schema)
{format, opts} = Keyword.pop!(opts, :format)
response_as = if format == :csv, do: :text, else: :json
query =
opts
|> Map.new()
|> Map.put(:format, Atom.to_string(format))
|> Map.update(:order, nil, &Atom.to_string/1)
result =
Client.request(config, :get, "/v1.0/merchants/#{merchant_code}/payouts",
query: query,
as: response_as,
headers: if(format == :csv, do: [{"accept", "text/csv"}], else: []),
resource: :payouts,
operation: :list
)
case {format, result} do
{:json, {:ok, body}} -> {:ok, body |> List.wrap() |> Enum.map(&Payout.from_json/1)}
{:csv, {:ok, _body}} -> result
{_, {:error, _} = error} -> error
end
end
end