-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PaymentMethods module, with struct, list/2, attach/2, detach/2
- card_present not listed on module, since there's little documentation on it
- Loading branch information
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ defmodule Stripe.Converter do | |
order | ||
order_return | ||
payment_intent | ||
payment_method | ||
payout | ||
plan | ||
product | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
defmodule Stripe.PaymentMethod do | ||
@moduledoc """ | ||
Work with Stripe payment method objects. | ||
Stripe API reference: https://stripe.com/docs/api/payment_methods | ||
""" | ||
|
||
use Stripe.Entity | ||
import Stripe.Request | ||
|
||
@type t :: %__MODULE__{ | ||
id: Stripe.id(), | ||
object: String.t(), | ||
billing_details: %{ | ||
address: Stripe.Types.address(), | ||
email: String.t() | nil, | ||
name: String.t() | nil, | ||
phone: String.t() | nil | ||
}, | ||
card: Stripe.Card.t() | nil, | ||
created: Stripe.timestamp(), | ||
customer: Stripe.id() | Stripe.Customer.t() | nil, | ||
livemode: boolean, | ||
metadata: Stripe.Types.metadata(), | ||
type: String.t() | ||
} | ||
|
||
defstruct [ | ||
:id, | ||
:object, | ||
:billing_details, | ||
:card, | ||
:created, | ||
:customer, | ||
:livemode, | ||
:metadata, | ||
:type | ||
] | ||
|
||
defp plural_endpoint() do | ||
"payment_methods" | ||
end | ||
|
||
defp plural_endpoint(%{payment_method: payment_method}) do | ||
plural_endpoint() <> "/" <> get_id!(payment_method) | ||
end | ||
|
||
@doc """ | ||
List all payment methods. | ||
""" | ||
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()} | ||
when params: %{ | ||
:customer => Stripe.id() | Stripe.Customer.t(), | ||
:type => String.t(), | ||
optional(:ending_before) => t | Stripe.id(), | ||
optional(:limit) => 1..100, | ||
optional(:starting_after) => t | Stripe.id() | ||
} | ||
def list(%{customer: _} = params, opts \\ []) do | ||
endpoint = plural_endpoint() | ||
|
||
new_request(opts) | ||
|> put_endpoint(endpoint) | ||
|> put_method(:get) | ||
|> put_params(params |> Map.update!(:customer, &get_id!/1)) | ||
|> make_request() | ||
end | ||
|
||
@doc """ | ||
Attach payment_method to customer | ||
""" | ||
@spec attach(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
when params: %{ | ||
:customer => Stripe.id() | Stripe.Customer.t(), | ||
:payment_method => Stripe.id() | t() | ||
} | ||
def attach(%{customer: customer, payment_method: _} = params, opts \\ []) do | ||
endpoint = plural_endpoint(params) <> "/attach" | ||
|
||
new_request(opts) | ||
|> put_endpoint(endpoint) | ||
|> put_method(:post) | ||
|> put_params(%{customer: get_id!(customer)}) | ||
|> make_request() | ||
end | ||
|
||
@doc """ | ||
Dettach payment_method from customer | ||
""" | ||
@spec dettach(params, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()} | ||
when params: %{:payment_method => Stripe.id() | t()} | ||
def dettach(%{payment_method: _} = params, opts \\ []) do | ||
endpoint = plural_endpoint(params) <> "/detach" | ||
|
||
new_request(opts) | ||
|> put_endpoint(endpoint) | ||
|> put_method(:post) | ||
|> make_request() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
defmodule Stripe.PaymentMethodTest do | ||
use Stripe.StripeCase, async: true | ||
|
||
describe "list/2" do | ||
test "lists all cards" do | ||
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) | ||
end | ||
|
||
test "lists all cards with customer provided as struct" do | ||
assert {:ok, %Stripe.List{data: cards}} = | ||
Stripe.PaymentMethod.list(%{ | ||
customer: %Stripe.Customer{id: "cus_123"}, | ||
type: "card" | ||
}) | ||
|
||
assert_stripe_requested(:get, "/v1/payment_methods?customer=cus_123&type=card") | ||
assert is_list(cards) | ||
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"} | ||
) | ||
end | ||
|
||
test "attaches payment method to customer with customer provided as struct" do | ||
assert {:ok, %Stripe.PaymentMethod{}} = | ||
Stripe.PaymentMethod.attach(%{ | ||
customer: %Stripe.Customer{id: "cus_123"}, | ||
payment_method: "pm_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 | ||
assert {:ok, %Stripe.PaymentMethod{}} = | ||
Stripe.PaymentMethod.attach(%{ | ||
customer: "cus_123", | ||
payment_method: %Stripe.PaymentMethod{id: "pm_123"} | ||
}) | ||
|
||
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/attach", | ||
body: %{customer: "cus_123"} | ||
) | ||
end | ||
end | ||
|
||
describe "dettach/2" do | ||
test "detaches payment method from customer" do | ||
assert {:ok, %Stripe.PaymentMethod{}} = | ||
Stripe.PaymentMethod.dettach(%{customer: "cus_123", payment_method: "pm_123"}) | ||
|
||
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/detach") | ||
end | ||
|
||
test "detaches payment method from customer with payment method provided as struct" do | ||
assert {:ok, %Stripe.PaymentMethod{}} = | ||
Stripe.PaymentMethod.dettach(%{ | ||
customer: "cus_123", | ||
payment_method: %Stripe.PaymentMethod{id: "pm_123"} | ||
}) | ||
|
||
assert_stripe_requested(:post, "/v1/payment_methods/pm_123/detach") | ||
end | ||
end | ||
end |