Skip to content

Commit

Permalink
Add tax_rate struct and api calls (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored May 28, 2019
2 parents d61b4bf + 38ca50a commit 0cb24e0
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ defmodule Stripe.Converter do
subscription
subscription_item
subscription_schedule
tax_rate
transfer
transfer_reversal
token
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/subscriptions/subscription_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ defmodule Stripe.SubscriptionSchedule do
@doc """
Releases a subscription schedule
Takes the subscription schedule `id.
Takes the subscription schedule `id`.
"""

@spec release(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
Expand Down
122 changes: 122 additions & 0 deletions lib/stripe/subscriptions/tax_rate.ex
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
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ defmodule Stripe.Mixfile do
],
Billing: [
Stripe.Coupon,
Stripe.CreditNote,
Stripe.Discount,
Stripe.Invoice,
Stripe.Invoiceitem,
Expand All @@ -123,7 +124,9 @@ defmodule Stripe.Mixfile do
Stripe.Plan,
Stripe.Subscription,
Stripe.SubscriptionItem,
Stripe.SubscriptionItem.Usage
Stripe.SubscriptionItem.Usage,
Stripe.SubscriptionSchedule,
Stripe.TaxRate
],
Connect: [
Stripe.Account,
Expand Down
54 changes: 54 additions & 0 deletions test/stripe/subscriptions/tax_rate_test.exs
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

0 comments on commit 0cb24e0

Please sign in to comment.