Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update payout endpoint and tests #297

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Stripe.Converter do
line_item
list
oauth
payout
plan
refund
source
Expand Down
151 changes: 132 additions & 19 deletions lib/stripe/core_resources/payout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ defmodule Stripe.Payout do
"""

use Stripe.Entity
import Stripe.Request

@type t :: %__MODULE__{
id: Stripe.id,
object: String.t,
amount: integer,
arrival_date: Stripe.timestamp,
balance_transaction: Stripe.id | Stripe.BalanceTransaction.t | nil,
created: Stripe.timestamp,
currency: String.t,
description: String.t | nil,
destination: Stripe.id | Stripe.Card.t | Stripe.BankAccount.t | nil,
failure_balance_transaction: Stripe.id | Stripe.BalanceTransaction.t | nil,
failure_code: String.t | nil,
failure_message: String.t | nil,
livemode: boolean,
method: String.t,
source_type: String.t,
statement_descriptor: String.t | nil,
status: String.t,
type: String.t
}
id: Stripe.id(),
object: String.t(),
amount: integer,
arrival_date: Stripe.timestamp(),
balance_transaction: Stripe.id() | Stripe.BalanceTransaction.t() | nil,
created: Stripe.timestamp(),
currency: String.t(),
description: String.t() | nil,
destination: Stripe.id() | Stripe.Card.t() | Stripe.BankAccount.t() | nil,
failure_balance_transaction: Stripe.id() | Stripe.BalanceTransaction.t() | nil,
failure_code: String.t() | nil,
failure_message: String.t() | nil,
livemode: boolean,
method: String.t(),
source_type: String.t(),
statement_descriptor: String.t() | nil,
status: String.t(),
type: String.t()
}

defstruct [
:id,
Expand All @@ -48,4 +49,116 @@ defmodule Stripe.Payout do
:status,
:type
]

@plural_endpoint "payouts"

@doc """
Create a payout.

If your API key is in test mode, the supplied payment source (e.g., card) won't actually be
payoutd, though everything else will occur as if in live mode.
(Stripe assumes that the payout would have completed successfully).

See the [Stripe docs](https://stripe.com/docs/api#create_payout).
"""
@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
amount: pos_integer,
currency: String.t(),
description: String.t(),
destination: %{
:account => Stripe.id() | Stripe.Account.t(),
optional(:amount) => non_neg_integer
},
metadata: map,
source_type: String.t(),
statement_descriptor: String.t()
}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_params(params)
|> put_method(:post)
|> make_request()
end

@doc """
Retrieve a payout.

See the [Stripe docs](https://stripe.com/docs/api#retrieve_payout).
"""
@spec retrieve(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def retrieve(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
end

@doc """
Update a payout.

Updates the specified payout by setting the values of the parameters passed. Any parameters
not provided will be left unchanged.

This request accepts only the `:payout` or `:metadata`.

The payout to be updated may either be passed in as a struct or an ID.

See the [Stripe docs](https://stripe.com/docs/api#update_payout).
"""
@spec update(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
metadata: %{
optional(String.t()) => String.t(),
optional(atom) => String.t()
}
}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
List all payouts.

Returns a list of payouts. The payouts are returned in sorted order,
with the most recent payouts appearing first.

See the [Stripe docs](https://stripe.com/docs/api#list_payouts).
"""
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.of(t)} | {:error, Stripe.Error.t()}
when params: %{
arrival_date: Stripe.date_query(),
created: Stripe.date_query(),
destination: String.t(),
ending_before: t | Stripe.id(),
limit: 1..100,
starting_after: t | Stripe.id(),
status: String.t()
}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after])
|> make_request()
end

@doc """
Cancel a payout.

See the [Stripe docs](https://stripe.com/docs/api#cancel_payout).
"""
@spec cancel(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def cancel(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}" <> "/cancel")
|> put_method(:post)
|> make_request()
end
end
41 changes: 41 additions & 0 deletions test/stripe/core_resources/payout_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Stripe.PayoutTest do
use Stripe.StripeCase, async: true

describe "create/2" do
test "creates a card for a customer" do
params = %{amount: 100, currency: "USD", source_type: "card"}
assert {:ok, %Stripe.Payout{}} = Stripe.Payout.create(params)
assert_stripe_requested(:post, "/v1/payouts")
end
end

describe "retrieve/2" do
test "retrieves a card" do
assert {:ok, %Stripe.Payout{}} = Stripe.Payout.retrieve("py_123")
assert_stripe_requested(:get, "/v1/payouts/py_123")
end
end

describe "update/2" do
test "updates a card" do
assert {:ok, %Stripe.Payout{}} = Stripe.Payout.update("py_123", %{metadata: %{foo: "bar"}})
assert_stripe_requested(:post, "/v1/payouts/py_123")
end
end

describe "list/2" do
test "lists all cards" do
assert {:ok, %Stripe.List{data: payouts}} = Stripe.Payout.list()
assert_stripe_requested(:get, "/v1/payouts")
assert is_list(payouts)
assert %Stripe.Payout{} = hd(payouts)
end
end

describe "cancel/1" do
test "cancels a payout" do
assert {:ok, %Stripe.Payout{}} = Stripe.Payout.cancel("py_123")
assert_stripe_requested(:get, "/v1/payouts/cancel")
end
end
end