-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tax_rate struct and api calls (#491)
- Loading branch information
Showing
5 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
defmodule Stripe.TaxRate do | ||
@moduledoc """ | ||
Work with Stripe TaxRate objects. | ||
""" | ||
|
||
use Stripe.Entity | ||
import Stripe.Request | ||
|
||
@type t :: %__MODULE__{ | ||
id: Stripe.id(), | ||
object: String.t(), | ||
active: boolean, | ||
created: Stripe.timestamp(), | ||
description: String.t() | nil, | ||
display_name: String.t(), | ||
inclusive: boolean, | ||
jurisdiction: String.t() | nil, | ||
livemode: boolean, | ||
metadata: Stripe.Types.metadata(), | ||
percentage: integer | ||
} | ||
|
||
defstruct [ | ||
:id, | ||
:object, | ||
:active, | ||
:created, | ||
:description, | ||
:display_name, | ||
:inclusive, | ||
:jurisdiction, | ||
:livemode, | ||
:metadata, | ||
:percentage | ||
] | ||
|
||
@plural_endpoint "tax_rates" | ||
|
||
@doc """ | ||
Create a tax rate. | ||
""" | ||
@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
when params: | ||
%{ | ||
:percentage => number, | ||
:display_name => String.t(), | ||
:inclusive => boolean, | ||
optional(:active) => boolean, | ||
optional(:description) => String.t(), | ||
optional(:metadata) => Stripe.Types.metadata(), | ||
optional(:jurisdiction) => 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 tax rate. | ||
""" | ||
@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 tax rate. | ||
Takes the `id` and a map of changes. | ||
""" | ||
@spec update(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
when params: | ||
%{ | ||
optional(:percentage) => number, | ||
optional(:display_name) => String.t(), | ||
optional(:inclusive) => boolean, | ||
optional(:active) => boolean, | ||
optional(:description) => String.t(), | ||
optional(:metadata) => Stripe.Types.metadata(), | ||
optional(:jurisdiction) => 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 tax rates. | ||
""" | ||
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()} | ||
when params: | ||
%{ | ||
optional(:active) => boolean, | ||
optional(:created) => Stripe.date_query(), | ||
optional(:ending_before) => t | Stripe.id(), | ||
optional(:limit) => 1..100, | ||
optional(:inclusive) => boolean, | ||
optional(:percentage) => number, | ||
optional(:starting_after) => t | Stripe.id() | ||
} | ||
| %{} | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule Stripe.TaxRateTest do | ||
use Stripe.StripeCase, async: true | ||
|
||
describe "create/2" do | ||
test "creates a TaxRate for a customer" do | ||
params = %{ | ||
display_name: "VAT", | ||
description: "VAT Germany", | ||
jurisdiction: "DE", | ||
percentage: 19.0, | ||
inclusive: false | ||
} | ||
|
||
assert {:ok, %Stripe.TaxRate{}} = Stripe.TaxRate.create(params) | ||
assert_stripe_requested(:post, "/v1/tax_rates") | ||
end | ||
|
||
test "returns an error TaxRate" do | ||
params = %{ | ||
display_name: "VAT", | ||
description: "VAT Germany", | ||
jurisdiction: "DE", | ||
inclusive: false | ||
} | ||
|
||
assert {:error, %Stripe.Error{}} = Stripe.TaxRate.create(params) | ||
assert_stripe_requested(:post, "/v1/tax_rates") | ||
end | ||
end | ||
|
||
describe "retrieve/2" do | ||
test "retrieves a TaxRate" do | ||
assert {:ok, %Stripe.TaxRate{}} = Stripe.TaxRate.retrieve("txr_1EXapq2eZvKYlo2CHmXqULaR") | ||
assert_stripe_requested(:get, "/v1/tax_rates/txr_1EXapq2eZvKYlo2CHmXqULaR") | ||
end | ||
end | ||
|
||
describe "update/2" do | ||
test "updates a TaxRate" do | ||
params = %{metadata: %{foo: "bar"}} | ||
assert {:ok, plan} = Stripe.TaxRate.update("txr_1EXapq2eZvKYlo2CHmXqULaR", params) | ||
assert_stripe_requested(:post, "/v1/tax_rates/#{plan.id}") | ||
end | ||
end | ||
|
||
describe "list/2" do | ||
test "lists all TaxRates" do | ||
assert {:ok, %Stripe.List{data: plans}} = Stripe.TaxRate.list() | ||
assert_stripe_requested(:get, "/v1/tax_rates") | ||
assert is_list(plans) | ||
assert %Stripe.TaxRate{} = hd(plans) | ||
end | ||
end | ||
end |