From 57d8b2e4501085e9a2953165570f5ffc3002f545 Mon Sep 17 00:00:00 2001 From: snewcomer Date: Fri, 27 Apr 2018 11:15:26 -0700 Subject: [PATCH] Add balance transactions and endpoints --- lib/stripe/core_resources/balance.ex | 41 +++++++++++++++++++ .../core_resources/balance_transaction.ex | 3 +- test/stripe/core_resources/balance_test.exs | 12 ++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lib/stripe/core_resources/balance.ex b/lib/stripe/core_resources/balance.ex index ea4b98b7..77fdd900 100644 --- a/lib/stripe/core_resources/balance.ex +++ b/lib/stripe/core_resources/balance.ex @@ -49,4 +49,45 @@ defmodule Stripe.Balance do |> put_method(:get) |> make_request() end + + @doc """ + Retrieves a balance transaction + + See the [Stripe docs](https://stripe.com/docs/api#balance_transaction_retrieve). + """ + @spec retrieve_transaction(String.t(), Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} + def retrieve_transaction(id, opts \\ []) do + new_request(opts) + |> put_endpoint(@endpoint <> "/history/" <> id) + |> put_method(:get) + |> make_request() + end + + @doc """ + List balance history + + See the [Stripe docs](https://stripe.com/docs/api#balance_history). + """ + @spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()} + when params: %{ + optional(:available_on) => Stripe.date_query(), + optional(:created) => Stripe.date_query(), + optional(:currency) => String.t(), + optional(:ending_before) => t | Stripe.id(), + optional(:limit) => 1..100, + optional(:payout) => String.t(), + optional(:source) => %{ + optional(:object) => String.t() + }, + optional(:starting_after) => t | Stripe.id(), + optional(:type) => String.t() + } + def list(params \\ %{}, opts \\ []) do + new_request(opts) + |> put_endpoint(@endpoint <> "/history") + |> put_method(:get) + |> put_params(params) + |> cast_to_id([:available_on, :created, :ending_before, :starting_after]) + |> make_request() + end end diff --git a/lib/stripe/core_resources/balance_transaction.ex b/lib/stripe/core_resources/balance_transaction.ex index 510d7431..7cd90c23 100644 --- a/lib/stripe/core_resources/balance_transaction.ex +++ b/lib/stripe/core_resources/balance_transaction.ex @@ -18,10 +18,10 @@ defmodule Stripe.BalanceTransaction do created: Stripe.timestamp(), currency: String.t(), description: String.t() | nil, + exchange_rate: integer | nil, fee: integer, fee_details: list(Stripe.Types.fee()) | [], net: integer, - # TODO: clarify these source: Stripe.id() | Stripe.Source.t() | nil, status: String.t(), type: String.t() @@ -35,6 +35,7 @@ defmodule Stripe.BalanceTransaction do :created, :currency, :description, + :exchange_rate, :fee, :fee_details, :net, diff --git a/test/stripe/core_resources/balance_test.exs b/test/stripe/core_resources/balance_test.exs index 9d71294b..14b01b6e 100644 --- a/test/stripe/core_resources/balance_test.exs +++ b/test/stripe/core_resources/balance_test.exs @@ -1,6 +1,18 @@ defmodule Stripe.BalanceTest do use Stripe.StripeCase, async: true + test "is listable" do + assert {:ok, %Stripe.List{data: balances}} = Stripe.Balance.list() + assert_stripe_requested(:get, "/v1/balance/history") + assert is_list(balances) + assert %Stripe.BalanceTransaction{} = hd(balances) + end + + test "transaction is retrievable" do + assert {:ok, %Stripe.BalanceTransaction{}} = Stripe.Balance.retrieve_transaction("b_123") + assert_stripe_requested(:get, "/v1/balance/history/#{"b_123"}") + end + test "is retrievable" do assert {:ok, %Stripe.Balance{}} = Stripe.Balance.retrieve() assert_stripe_requested(:get, "/v1/balance")