Skip to content

Commit

Permalink
Typo for retrieving invoice items + Adds handler for Usage endpoint (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
Caroline Twigg authored and snewcomer committed Feb 6, 2019
1 parent f5e92fc commit 0c94360
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/stripe/subscriptions/line_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defmodule Stripe.LineItem do
} | %{}
def retrieve(id, params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint("invoices" <> "/#{get_id!(id)}" <> "lines")
|> put_endpoint("invoices" <> "/#{get_id!(id)}/" <> "lines")
|> put_method(:get)
|> put_params(params)
|> make_request()
Expand Down
72 changes: 72 additions & 0 deletions lib/stripe/subscriptions/usage.ex
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
4 changes: 3 additions & 1 deletion test/stripe/subscriptions/line_item_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Stripe.LineItemTest do

describe "retrieve/2" do
test "retrieves an invoice" do
assert {:ok, %Stripe.Invoice{}} = Stripe.LineItem.retrieve("in_1234")
assert {:ok, %Stripe.List{data: line_items}} = Stripe.LineItem.retrieve("in_1234")
assert_stripe_requested(:get, "/v1/invoices/in_1234/lines")
assert is_list(line_items)
assert %Stripe.LineItem{} = hd(line_items)
end
end
end
60 changes: 60 additions & 0 deletions test/stripe/subscriptions/usage_test.exs
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

0 comments on commit 0c94360

Please sign in to comment.