Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relay Orders #364

Merged
merged 10 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ defmodule Stripe.Converter do
line_item
list
oauth
order
order_return
payout
plan
product
refund
sku
source
subscription
subscription_item
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/core_resources/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Stripe.Event do
| Stripe.Product.t()
| Stripe.Recipient.t()
| Stripe.Review.t()
| Stripe.SKU.t()
| Stripe.Sku.t()
| Stripe.Source.t()
| Stripe.Transfer.t()
| map
Expand Down
5 changes: 1 addition & 4 deletions lib/stripe/payment_methods/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ defmodule Stripe.Card do
@doc """
Delete a card.
"""
@spec delete(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
:id => String.t(),
}
@spec delete(Stripe.id() | t, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, %{customer: _} = params, opts \\ []) do
endpoint = params |> plural_endpoint()

Expand Down
128 changes: 128 additions & 0 deletions lib/stripe/relay/order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ defmodule Stripe.Order do
"""

use Stripe.Entity
import Stripe.Request

@type card_info :: %{
exp_month: number,
exp_year: number,
number: String.t(),
object: String.t(),
cvc: String.t(),
address_city: String.t() | nil,
address_country: String.t() | nil,
address_line1: String.t() | nil,
address_line2: String.t() | nil,
name: String.t() | nil,
address_state: String.t() | nil,
address_zip: String.t() | nil
}

@type t :: %__MODULE__{
id: Stripe.id(),
Expand Down Expand Up @@ -87,4 +103,116 @@ defmodule Stripe.Order do
:updated,
:upstream_id
]

@endpoint "orders"

@doc """
Create a order.
"""
@spec create(params, Keyword.t()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
:currency => String.t(),
optional(:coupon) => Stripe.id() | Stripe.Coupon.t(),
optional(:customer) => Stripe.id() | Stripe.Customer.t(),
optional(:email) => String.t(),
optional(:items) => Stripe.List.t(Stripe.OrderItem.t()),
optional(:metadata) => Stripe.Types.metadata(),
optional(:shipping) => map
}
def create(%{currency: _} = params, opts \\ []) do
new_request(opts)
|> put_endpoint(@endpoint)
|> put_params(params)
|> put_method(:post)
|> make_request()
end

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

@doc """
Update a order.

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(:coupon) => Stripe.id() | Stripe.Coupon.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:selected_shipping_method) => String.t(),
optional(:shipping) => map,
optional(:status) => String.t()
}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
Pay an order.
"""
@spec pay(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:application_fee) => non_neg_integer,
optional(:customer) => Stripe.id() | Stripe.Customer.t(),
optional(:source) => Stripe.id() | Stripe.Card.t() | Stripe.Customer.t() | card_info,
optional(:email) => String.t(),
optional(:metadata) => Stripe.Types.metadata()
}
def pay(id, params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@endpoint <> "/#{get_id!(id)}/" <> "pay")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
return an order.
"""
@spec return(Stripe.id() | t, params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:items) => Stripe.List.t(Stripe.OrderItem.t())
}
def return(id, params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@endpoint <> "/#{get_id!(id)}/" <> "returns")
|> put_method(:post)
|> put_params(params)
|> make_request()
end

@doc """
List all orders.
"""
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
optional(:customer) => Stripe.id() | Stripe.Customer.t(),
optional(:ending_before) => t | Stripe.id(),
optional(:ids) => Stripe.List.t(Stripe.id()),
optional(:limit) => 1..100,
optional(:starting_after) => t | Stripe.id(),
optional(:status) => String.t(),
optional(:status_transitions) => map,
optional(:upstream_ids) => Stripe.List.t(Stripe.id())
}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:customer, :ending_before, :starting_after])
|> make_request()
end
end
2 changes: 1 addition & 1 deletion lib/stripe/relay/order_item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Stripe.OrderItem do
amount: pos_integer,
currency: String.t(),
description: String.t(),
parent: nil | Stripe.id() | Stripe.SKU.t(),
parent: nil | Stripe.id() | Stripe.Sku.t(),
quantity: nil | pos_integer,
type: String.t()
}
Expand Down
13 changes: 11 additions & 2 deletions lib/stripe/relay/order_return.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,20 @@ defmodule Stripe.OrderReturn do
@doc """
List all returns.
"""
@spec list(Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
def list(opts \\ []) do
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
optional(:created) => Stripe.date_query(),
optional(:ending_before) => t | Stripe.id(),
optional(:ids) => Stripe.List.t(Stripe.id()),
optional(:limit) => 1..100,
optional(:order) => Stripe.Order.t(),
optional(:starting_after) => t | Stripe.id()
}
def list(params \\ %{}, opts \\ []) do
new_request(opts)
|> put_endpoint(@plural_endpoint)
|> put_method(:get)
|> put_params(params)
|> make_request()
end
end
109 changes: 108 additions & 1 deletion lib/stripe/relay/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Stripe.Relay.Product do
"""

use Stripe.Entity
import Stripe.Request

@type t :: %__MODULE__{
id: Stripe.id(),
Expand All @@ -31,7 +32,9 @@ defmodule Stripe.Relay.Product do
width: float
},
shippable: boolean,
skus: Stripe.List.t(Stripe.SKU.t()),
skus: Stripe.List.t(Stripe.Sku.t()),
statement_descriptor: String.t(),
unit_label: String.t(),
updated: Stripe.timestamp(),
url: String.t()
}
Expand All @@ -52,7 +55,111 @@ defmodule Stripe.Relay.Product do
:package_dimensions,
:shippable,
:skus,
:statement_descriptor,
:unit_label,
:updated,
:url
]

@endpoint "products"

@doc """
Create a product.
"""
@spec create(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
optional(:caption) => String.t(),
optional(:deactive_on) => [Stripe.id()],
optional(:description) => String.t(),
optional(:id) => String.t(),
optional(:images) => [Stripe.id()],
optional(:description) => String.t(),
optional(:attributes) => list,
:name => String.t(),
:type => String.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:package_dimensions) => map,
optional(:shippable) => boolean,
optional(:url) => String.t()
} | %{}
def create(params, opts \\ []) do
new_request(opts)
|> put_endpoint(@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(@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(:active) => boolean,
optional(:attributes) => list,
optional(:caption) => String.t(),
optional(:deactive_on) => [Stripe.id()],
optional(:description) => String.t(),
optional(:images) => [Stripe.id()],
optional(:metadata) => Stripe.Types.metadata(),
optional(:name) => String.t(),
optional(:package_dimensions) => map,
optional(:shippable) => boolean,
optional(:url) => String.t()
} | %{}
def update(id, params, opts \\ []) do
new_request(opts)
|> put_endpoint(@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(@endpoint <> "/#{get_id!(id)}")
|> put_method(:delete)
|> make_request()
end

@doc """
List all product.
"""
@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(:ids) => Stripe.List.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(@endpoint)
|> put_method(:get)
|> put_params(params)
|> cast_to_id([:ending_before, :starting_after])
|> make_request()
end
end
Loading