Skip to content

Commit

Permalink
Add endpoints to Subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Nov 13, 2017
1 parent ca79e74 commit a699a1a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 29 deletions.
113 changes: 85 additions & 28 deletions lib/stripe/subscriptions/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ defmodule Stripe.Subscription do
```
"""
use Stripe.Entity
import Stripe.Request
alias Stripe.Util

@type t :: %__MODULE__{
Expand Down Expand Up @@ -140,64 +141,120 @@ defmodule Stripe.Subscription do
@doc """
Create a subscription.
"""
@spec create(map, Keyword.t) :: {:ok, t} | {:error, Stripe.api_error_struct}
def create(changes, opts \\ []) do
Stripe.Request.create(@plural_endpoint, changes, opts)
@spec create(params, Stripe.options) :: {:ok, t} | {:error, Stripe.Error.t}
when params: %{
application_fee_percent: float,
coupon: Stripe.id | Stripe.Coupon.t,
items: [
%{
:plan => Stripe.id | Stripe.Plan.t,
optional(:quantity) => non_neg_integer
}
],
metadata: %{
optional(String.t) => String.t
},
tax_percent: float,
trial_end: Stripe.timestamp,
trial_period_days: non_neg_integer
}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_params(params)
|> put_method(:post)
|> cast_to_id([:coupon])
|> make_request()
end

@doc """
Retrieve a subscription.
"""
@spec retrieve(String.t, Keyword.t) :: {:ok, t} | {:error, Stripe.api_error_struct}
@spec retrieve(Stripe.id | t, Stripe.options) :: {:ok, t} | {:error, Stripe.Error.t}
def retrieve(id, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id
Stripe.Request.retrieve(endpoint, opts)
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:get)
|> make_request()
end

@doc """
Update a subscription.
Takes the `id` and a map of changes.
"""
@spec update(String.t, map, list) :: {:ok, t} | {:error, Stripe.api_error_struct}
def update(id, changes, opts \\ []) do
endpoint = @plural_endpoint <> "/" <> id
Stripe.Request.update(endpoint, changes, opts)
@spec update(Stripe.id | t, params, Stripe.options) :: {:ok, t} | {:error, Stripe.Error.t}
when params: %{
application_fee_percent: float,
coupon: Stripe.id | Stripe.Coupon.t,
items: [
%{
:plan => Stripe.id | Stripe.Plan.t,
optional(:quantity) => non_neg_integer
}
],
metadata: %{
optional(String.t) => String.t
},
prorate: boolean,
proration_date: Stripe.timestamp,
source: Stripe.id | Stripe.Source.t,
tax_percent: float,
trial_end: Stripe.timestamp
}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params)
|> cast_to_id([:coupon, :source])
|> make_request()
end

@doc """
Delete a subscription.
Takes the `id` and an optional map of `params`.
"""
@spec delete(String.t, map, list) :: :ok | {:error, Stripe.api_error_struct}
def delete(subscription, params \\ %{}, opts \\ []) do
id = Util.normalize_id(subscription)
endpoint = @plural_endpoint <> "/" <> id
Stripe.Request.delete(endpoint, params, opts)
@spec delete(Stripe.id | t, Stripe.options) :: {:ok, t} | {:error, Stripe.Error.t}
def delete(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:delete)
|> make_request()
end

@doc """
List all subscriptions.
"""
@spec list(map, Keyword.t) :: {:ok, Stripe.List.t} | {:error, Stripe.api_error_struct}
@spec list(params, Stripe.options) :: {:ok, Stripe.List.of(t)} | {:error, Stripe.Error.t}
when params: %{
created: Stripe.date_query,
customer: Stripe.Customer.t | Stripe.id,
ending_before: t | Stripe.id,
limit: 1..100,
plan: Stripe.Plan.t | Stripe.id,
starting_after: t | Stripe.id,
status: :trialing | :active | :past_due | :canceled | :unpaid | :all
}
def list(params \\ %{}, opts \\ []) do
endpoint = @plural_endpoint

params
|> Stripe.Request.retrieve(endpoint, opts)
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:customer, :ending_before, :plan, :starting_after])
|> make_request()
end

@doc """
Deletes the discount on a subscription.
"""
@spec delete_discount(t) :: {:ok, t} | {:error, Stripe.api_error_struct}
def delete_discount(%Stripe.Subscription{id: id} = subscription, opts \\ []) do
endpoint = @plural_endpoint <> "/#{id}/discount"

with {:ok, _} <- Stripe.Request.delete(endpoint, %{}, opts)
do
{:ok, %{subscription | discount: nil}}
end
@spec delete_discount(Stripe.id | t, Stripe.options) :: {:ok, t} | {:error, Stripe.Error.t}
def delete_discount(id, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}/discount")
|> put_method(:delete)
|> make_request()
end

end
3 changes: 2 additions & 1 deletion test/stripe/subscriptions/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule Stripe.SubscriptionTest do

test "delete_discount/2 deletes a subscription's discount" do
{:ok, subscription} = Stripe.Subscription.retrieve("sub_123")
assert {:ok, %Stripe.Subscription{discount: nil}} = Stripe.Subscription.delete_discount(subscription)
assert {:ok, coupon} = Stripe.Coupon.retrieve("35OFF")
assert {:ok, coupon} = Stripe.Subscription.delete_discount(subscription)
end
end

0 comments on commit a699a1a

Please sign in to comment.