Skip to content

Commit

Permalink
Add create/2, retrieve/2. update/3
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed May 20, 2019
1 parent 436b837 commit 3ed1737
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
62 changes: 62 additions & 0 deletions lib/stripe/payment_methods/payment_method.ex
Expand Up @@ -45,6 +45,68 @@ defmodule Stripe.PaymentMethod do
plural_endpoint() <> "/" <> get_id!(payment_method)
end

@type billing_details :: %{
optional(:address) => Stripe.Types.address(),
optional(:email) => String.t(),
optional(:name) => String.t(),
optional(:phone) => String.t()
}

@type card :: %{
:exp_month => integer,
:exp_year => integer,
:number => String.t(),
:cvc => String.t()
}

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

@doc """
Retrieve a payment method.
"""
@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 card.
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(:billing_details) => billing_details(),
optional(:card) => card(),
optional(:metadata) => Stripe.Types.metadata()
}
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 """
List all payment methods.
"""
Expand Down
36 changes: 32 additions & 4 deletions test/stripe/payment_methods/payment_method_test.exs
Expand Up @@ -3,7 +3,8 @@ defmodule Stripe.PaymentMethodTest do

describe "list/2" do
test "lists all cards" do
assert {:ok, %Stripe.List{data: cards}} = Stripe.PaymentMethod.list(%{customer: "cus_123", type: "card"})
assert {:ok, %Stripe.List{data: cards}} =
Stripe.PaymentMethod.list(%{customer: "cus_123", type: "card"})

assert_stripe_requested(:get, "/v1/payment_methods?customer=cus_123&type=card")
assert is_list(cards)
Expand All @@ -21,12 +22,35 @@ defmodule Stripe.PaymentMethodTest do
end
end

describe "create/2" do
test "creates a payment method" do
assert {:ok, _} = Stripe.PaymentMethod.create(%{type: "card"})
assert_stripe_requested(:post, "/v1/payment_methods")
end
end

describe "retrieve/1" do
test "retrieves a payment method" do
assert {:ok, _} = Stripe.PaymentMethod.retrieve("pm_123")
assert_stripe_requested(:get, "/v1/payment_methods/pm_123")
end
end

describe "update/2" do
test "updates a payment method" do
assert {:ok, _} = Stripe.PaymentMethod.update("pm_123", %{})
assert_stripe_requested(:post, "/v1/payment_methods/pm_123")
end
end

describe "attach/2" do
test "attaches payment method to customer" do
assert {:ok, %Stripe.PaymentMethod{}} =
Stripe.PaymentMethod.attach(%{customer: "cus_123", payment_method: "pm_123"})

assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach", body: %{customer: "cus_123"})
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach",
body: %{customer: "cus_123"}
)
end

test "attaches payment method to customer with customer provided as struct" do
Expand All @@ -36,7 +60,9 @@ defmodule Stripe.PaymentMethodTest do
payment_method: "pm_123"
})

assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach", body: %{customer: "cus_123"})
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach",
body: %{customer: "cus_123"}
)
end

test "attaches payment method to customer with payment method provided as struct" do
Expand All @@ -46,7 +72,9 @@ defmodule Stripe.PaymentMethodTest do
payment_method: %Stripe.PaymentMethod{id: "pm_123"}
})

assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach", body: %{customer: "cus_123"})
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach",
body: %{customer: "cus_123"}
)
end
end

Expand Down

0 comments on commit 3ed1737

Please sign in to comment.