Skip to content

Commit

Permalink
add product struct (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored and begedin committed Apr 11, 2018
1 parent f4fd4cd commit 8ab712c
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule Stripe.Converter do
order_return
payout
plan
product
refund
source
subscription
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/core_resources/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule Stripe.Event do
| Stripe.OrderReturn.t()
| Stripe.Payout.t()
| Stripe.Plan.t()
| Stripe.Relay.Product.t()
| Stripe.Product.t()
| Stripe.Recipient.t()
| Stripe.Review.t()
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/relay/product.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Stripe.Product do
defmodule Stripe.Relay.Product do
@moduledoc """
Work with Stripe products.
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/relay/sku.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Stripe.SKU do
}
| nil,
price: non_neg_integer,
product: Stripe.id() | Stripe.Product.t(),
product: Stripe.id() | Stripe.Relay.Product.t(),
updated: Stripe.timestamp()
}

Expand Down
8 changes: 4 additions & 4 deletions lib/stripe/subscriptions/plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ defmodule Stripe.Plan do
livemode: boolean,
metadata: Stripe.Types.metadata(),
name: String.t(),
statement_descriptor: String.t() | nil,
trial_period_days: integer | nil
nickname: String.t() | nil,
product: Stripe.id() | Stripe.Product.t()
}

defstruct [
Expand All @@ -63,8 +63,8 @@ defmodule Stripe.Plan do
:livemode,
:metadata,
:name,
:statement_descriptor,
:trial_period_days
:nickname,
:product
]

@plural_endpoint "plans"
Expand Down
150 changes: 150 additions & 0 deletions lib/stripe/subscriptions/product.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
defmodule Stripe.Product do
@moduledoc """
Work with Stripe product objects.
You can:
- Create a product
- Retrieve a product
- Update a product
- Delete a product
- List products
Stripe API reference: https://stripe.com/docs/api#service_products
"""

use Stripe.Entity
import Stripe.Request

@type t :: %__MODULE__{
id: Stripe.id(),
object: String.t(),
active: boolean | nil,
attributes: list | nil,
caption: String.t() | nil,
created: Stripe.timestamp(),
deactivate_on: list,
description: String.t() | nil,
images: list,
livemode: boolean,
metadata: Stripe.Types.metadata(),
name: String.t(),
package_dimensions: map | nil,
shippable: boolean | nil,
skus: list,
statement_descriptor: String.t() | nil,
type: String.t() | nil,
updated: Stripe.timestamp(),
url: String.t() | nil
}

defstruct [
:id,
:object,
:active,
:attributes,
:caption,
:created,
:deactivate_on,
:description,
:images,
:livemode,
:metadata,
:name,
:package_dimensions,
:shippable,
:skus,
:statement_descriptor,
:type,
:updated,
:url
]

@plural_endpoint "products"

@doc """
Create a product.
"""
@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:id) => String.t(),
optional(:attributes) => list,
:name => String.t(),
:type => String.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:statement_descriptor) => String.t(),
optional(:unit_label) => String.t()
} | %{}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_params(params)
|> put_method(:post)
|> make_request()
end

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

@doc """
Update a product.
Takes the `id` and a map of changes.
"""
@spec update(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:attributes) => list,
optional(:name) => String.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:statement_descriptor) => String.t()
} | %{}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
Delete a product.
"""
@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 products.
"""
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
optional(:active) => boolean,
optional(:created) => Stripe.date_query(),
optional(:ending_before) => t | Stripe.id(),
optional(:limit) => 1..100,
optional(:shippable) => boolean,
optional(:starting_after) => t | Stripe.id(),
optional(:type) => String.t(),
optional(:url) => String.t()
} | %{}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after])
|> make_request()
end
end
1 change: 1 addition & 0 deletions test/stripe/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Stripe.UtilTest do
assert object_name_to_module("line_item") == Stripe.LineItem
assert object_name_to_module("list") == Stripe.List
assert object_name_to_module("plan") == Stripe.Plan
assert object_name_to_module("product") == Stripe.Product
assert object_name_to_module("refund") == Stripe.Refund
assert object_name_to_module("subscription") == Stripe.Subscription
assert object_name_to_module("token") == Stripe.Token
Expand Down

0 comments on commit 8ab712c

Please sign in to comment.