Skip to content

Commit

Permalink
Merge pull request #16 from williamhogman/master
Browse files Browse the repository at this point in the history
Allow deleting subscriptions with options
  • Loading branch information
robconery committed Dec 20, 2015
2 parents d70a116 + f2c659a commit 2dd6da0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
13 changes: 2 additions & 11 deletions lib/stripe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,15 @@ defmodule Stripe do
Returns dict
"""
def make_request_with_key( method, endpoint, key, body \\ [], headers \\ [], options \\ []) do

rb = Stripe.URI.encode_query(body)
rh = req_headers( key )
|> Dict.merge(headers)
|> Dict.to_list

{:ok, response} = case method do
:get -> get( endpoint, rh, options)
:put -> put( endpoint, rb, rh, options)
:head -> head( endpoint, rh, options)
:post -> post( endpoint, rb, rh, options)
:patch -> patch( endpoint, rb, rh, options)
:delete -> delete( endpoint, rh, options)
:options -> options( endpoint, rh, options)
end
{:ok, response} = request(method, endpoint, rb, rh, options)
response.body
end

@doc """
Boilerplate code to make requests with the key read from config or env.see config_or_env_key/0
Args:
Expand Down
8 changes: 4 additions & 4 deletions lib/stripe/subscriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ defmodule Stripe.Subscriptions do
```
Stripe.Subscriptions.cancel "customer_id", "subscription_id"
Stripe.Subscriptions.cancel "customer_id", "subscription_id", [at_period_end: true]
```
"""
def cancel(customer_id, sub_id) do
Stripe.make_request(:delete, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}")
|> Stripe.Util.handle_stripe_response
def cancel(customer_id, sub_id, opts \\ []) do
Stripe.make_request(:delete, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}", opts)
|> Stripe.Util.handle_stripe_response
end

@doc """
Cancel all subscriptions for account.
Expand Down
25 changes: 18 additions & 7 deletions test/stripe/subscription_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Stripe.SubscriptionTest do
customer = Helper.create_test_customer "subscription_test@localhost"
{:ok, sub1} = Stripe.Subscriptions.create customer.id, "test-std"
{:ok, sub2} = Stripe.Subscriptions.create customer.id, "test-dlx"
{:ok, sub3} = Stripe.Subscriptions.create customer.id, "test-dlx"

on_exit fn ->
Helper.delete_test_plans
Expand All @@ -16,17 +17,17 @@ defmodule Stripe.SubscriptionTest do
Stripe.Customers.delete customer.id
end

{:ok, [ customer: customer, sub1: sub1, sub2: sub2 ] }
{:ok, [ customer: customer, sub1: sub1, sub2: sub2, sub3: sub3 ] }
end

@tag disabled: false
test "Count works", %{customer: customer, sub1: _, sub2: _} do
case Stripe.Subscriptions.count customer.id do
{:ok, cnt} -> assert cnt == 2
{:ok, cnt} -> assert cnt == 3
{:error, err} -> flunk err
end
end

@tag disabled: false
test "Retrieving single works", %{customer: customer, sub1: sub1, sub2: _} do
case Stripe.Subscriptions.get customer.id, sub1.id do
Expand All @@ -39,7 +40,7 @@ defmodule Stripe.SubscriptionTest do
test "Retrieve all works", %{customer: customer} do
case Stripe.Subscriptions.all customer.id do
{:ok, subs} ->
assert Enum.count(subs) == 2
assert Enum.count(subs) == 3
{:error, err} -> flunk err
end
end
Expand All @@ -64,11 +65,21 @@ defmodule Stripe.SubscriptionTest do
{:error, err} -> flunk err
end
end


@tag disabled: false
test "Cancel at period end works", %{customer: customer, sub3: sub3} do
case Stripe.Subscriptions.cancel(customer.id, sub3.id, [at_period_end: true]) do
{:ok, canceled_sub} ->
assert canceled_sub[:status] == "active"
assert canceled_sub[:cancel_at_period_end] == true
{:error, err} -> flunk err
end
end

@tag disabled: false
test "Cancel all works", %{customer: customer, sub1: _, sub2: _} do
Stripe.Subscriptions.cancel_all customer.id
{:ok, cnt} = Stripe.Subscriptions.count(customer.id)
Stripe.Subscriptions.cancel_all customer.id
{:ok, cnt} = Stripe.Subscriptions.count(customer.id)
assert cnt == 0
end
end

0 comments on commit 2dd6da0

Please sign in to comment.