forked from beam-community/stripity-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typo for retrieving invoice items + Adds handler for Usage endpoint (b…
- Loading branch information
Showing
4 changed files
with
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule Stripe.SubscriptionItem.Usage do | ||
@moduledoc """ | ||
Work with Stripe usage record objects. | ||
Stripe API reference: https://stripe.com/docs/api/usage_records | ||
""" | ||
|
||
use Stripe.Entity | ||
import Stripe.Request | ||
|
||
@type t :: %__MODULE__{ | ||
id: Stripe.id(), | ||
object: String.t(), | ||
invoice: Stripe.id() | nil, | ||
livemode: boolean, | ||
quantity: non_neg_integer, | ||
subscription_item: Stripe.id() | Stripe.SubscriptionItem.t(), | ||
timestamp: Stripe.timestamp() | ||
} | ||
|
||
defstruct [ | ||
:id, | ||
:object, | ||
:invoice, | ||
:livemode, | ||
:quantity, | ||
:subscription_item, | ||
:timestamp | ||
] | ||
|
||
@plural_endpoint "subscription_items" | ||
|
||
@doc """ | ||
Creates a usage record for a specified subscription item id and date, and fills it with a quantity. | ||
""" | ||
def create(id, params, opts \\ []) | ||
|
||
@doc """ | ||
Creates a usage record for a specified subscription item id and date, and fills it with a quantity. | ||
""" | ||
@spec create(Stripe.id(), params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
when params: %{ | ||
:quantity => float | pos_integer | 0, | ||
:timestamp => Stripe.timestamp() | non_neg_integer, | ||
optional(:action) => String.t() | ||
} | ||
def create(id, params, opts) do | ||
new_request(opts) | ||
|> put_endpoint("#{@plural_endpoint}/#{id}/usage_records") | ||
|> put_method(:post) | ||
|> put_params(params) | ||
|> make_request() | ||
end | ||
|
||
@doc """ | ||
List all subscription item period summaries | ||
""" | ||
@spec list(Stripe.id(), params, Stripe.options) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()} | ||
when params: %{ | ||
optional(:ending_before) => t | Stripe.id(), | ||
optional(:limit) => 1..100, | ||
optional(:starting_after) => t | Stripe.id() | ||
} | ||
def list(id, params \\ %{}, opts \\ []) do | ||
new_request(opts) | ||
|> put_endpoint("#{@plural_endpoint}/#{id}/usage_record_summaries") | ||
|> 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,60 @@ | ||
defmodule Stripe.SubscriptionItem.UsageTest do | ||
use Stripe.StripeCase, async: true | ||
|
||
describe "create/2" do | ||
test "create usage record" do | ||
item_id = "si_123" | ||
|
||
params = %{ | ||
quantity: 10, | ||
timestamp: 1543335582 | ||
} | ||
|
||
assert {:ok, record} = Stripe.SubscriptionItem.Usage.create(item_id, params) | ||
assert %{subscription_item: _sub_id} = record | ||
assert_stripe_requested(:post, "/v1/subscription_items/#{item_id}/usage_records") | ||
end | ||
|
||
test "create usage record with subsctiption item" do | ||
item = | ||
%Stripe.SubscriptionItem{ | ||
id: "si_123" | ||
} | ||
|
||
params = %{ | ||
quantity: 10, | ||
timestamp: 1543335582 | ||
} | ||
|
||
assert {:ok, record} = Stripe.SubscriptionItem.Usage.create(Map.get(item, :id), params) | ||
assert %{subscription_item: _sub_id} = record | ||
assert_stripe_requested(:post, "/v1/subscription_items/#{item.id}/usage_records") | ||
end | ||
end | ||
|
||
describe "list/1" do | ||
test "list usage records for subscription items" do | ||
item_id = "si_123" | ||
|
||
assert {:ok, %Stripe.List{data: usages}} = Stripe.SubscriptionItem.Usage.list(item_id) | ||
assert_stripe_requested(:get, "/v1/subscription_items/#{item_id}/usage_record_summaries") | ||
assert is_list(usages) | ||
assert %{subscription_item: _sub_item_id} = hd(usages) | ||
end | ||
end | ||
|
||
describe "list/2" do | ||
test "list usage records for subscription items with params" do | ||
item_id = "si_123" | ||
|
||
params = %{ | ||
limit: 10 | ||
} | ||
|
||
assert {:ok, %Stripe.List{data: usages}} = Stripe.SubscriptionItem.Usage.list(item_id, params) | ||
assert_stripe_requested(:get, "/v1/subscription_items/#{item_id}/usage_record_summaries") | ||
assert is_list(usages) | ||
assert %{subscription_item: _sub_item_id} = hd(usages) | ||
end | ||
end | ||
end |