Packages
stripity_stripe
0.5.0
3.3.2
3.3.1
3.2.0
3.1.1
3.1.0
3.0.0
2.17.3
2.17.2
2.17.1
2.17.0
2.16.0
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.2
2.7.1
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.1
2.0.0
2.0.0-alpha.11
2.0.0-alpha.10
2.0.0-alpha.9
2.0.0-alpha.8
2.0.0-alpha.7
2.0.0-alpha.6
2.0.0-alpha.5
2.0.0-alpha.4
2.0.0-alpha.3
2.0.0-alpha.2
2.0.0-alpha.1
1.6.2
1.6.1
1.6.0
1.4.0
1.3.0
1.2.0
1.1.0
0.5.0
0.4.0
0.3.0
0.2.0
A Stripe client for Elixir.
Current section
Files
Jump to
Current section
Files
lib/stripe/subscriptions.ex
defmodule Stripe.Subscriptions do
@moduledoc """
Main API for working with Subscriptions at Stripe. Through this API you can:
- create
- update
- cancel
- cancel_all
- list all
- count all
"""
@endpoint "customers"
@doc """
Starts a subscription for the specified customer. Note that if you pass in the customer *and* subscription information, both will be created at the same time.
## Example
```
new_sub = [
email: "jill@test.com",
description: "Poop on the Potty",
plan: "standard",
source: [
object: "card",
number: "4111111111111111",
exp_month: 01,
exp_year: 2018,
cvc: 123,
name: "Jill Subscriber"
]
]
{:ok, sub} = Stripe.Subscriptions.create new_sub
```
You can also just pass along the customer id and the plan name:
```
new_sub = [
plan: plan_id, #like "standard",
customer: "customer_id"
]
{:ok, sub} = Stripe.Subscriptions.create new_sub
```
"""
def create(opts) do
customer_id = Keyword.get opts, :customer
plan_id = Keyword.get opts, :plan
create( customer_id, plan_id )
end
@doc """
Creates a subscription for the specified customer.
## Example
```
Stripe.Subscriptions.create "customer_id", "plan_id"
```
"""
def create(customer_id, plan_id) do
Stripe.make_request(:post, "#{@endpoint}/#{customer_id}/subscriptions", [plan: plan_id])
|> Stripe.Util.handle_stripe_response
end
@doc """
Returns a subscription; customer_id and subscription_id are required.
## Example
```
Stripe.Customers.get_subscription "customer_id", "subscription_id"
```
"""
def get(customer_id, sub_id) do
Stripe.make_request(:get, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}")
|> Stripe.Util.handle_stripe_response
end
@doc """
Changes a customer's subscription (plan, description, etc - see Stripe API for acceptable options).
Customer ID and Subscription ID are required for this.
## Example
```
Stripe.Subscriptions.change "customer_id", "subscription_id", "plan_id"
```
"""
def change(customer_id, sub_id, plan_id) do
Stripe.make_request(:post, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}", [plan: plan_id])
|> Stripe.Util.handle_stripe_response
end
@doc """
Cancels a subscription
## Example
```
Stripe.Subscriptions.cancel "customer_id", "subscription_id"
```
"""
def cancel(customer_id, sub_id) do
Stripe.make_request(:delete, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}")
|> Stripe.Util.handle_stripe_response
end
@doc """
Cancel all subscriptions for account.
#Example
```
Stripe.Subscriptions.cancel_all customer_id
```
"""
def cancel_all(customer_id) do
case all(customer_id) do
{:ok, subs} ->
Enum.each subs, fn sub -> cancel(customer_id, sub["id"]) end
{:error, err} -> raise err
end
end
@max_fetch_size 100
@doc """
List all subscriptions.
##Example
```
{:ok, subscriptions} = Stripe.Subscriptions.all customer_id
```
"""
def all( customer_id, accum \\ [], startingAfter \\ "") do
case Stripe.Util.list_raw("#{@endpoint}/#{customer_id}/subscriptions",@max_fetch_size, startingAfter) do
{:ok, resp} ->
case resp[:has_more] do
true ->
last_sub = List.last( resp[:data] )
all( customer_id, resp[:data] ++ accum, last_sub["id"] )
false ->
result = resp[:data] ++ accum
{:ok, result}
end
end
end
@doc """
Count total number of subscriptions.
## Example
```
{:ok, count} = Stripe.Subscriptions.count customer_id
```
"""
def count(customer_id) do
Stripe.Util.count "#{@endpoint}/#{customer_id}/subscriptions"
end
end