Skip to content

Commit

Permalink
Add PaymentMethods module, with struct, list/2, attach/2, detach/2
Browse files Browse the repository at this point in the history
- card_present not listed on module, since there's little documentation on it
  • Loading branch information
begedin committed May 10, 2019
1 parent 67bf463 commit b1ee33c
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/stripe/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule Stripe.Converter do
order
order_return
payment_intent
payment_method
payout
plan
product
Expand Down
100 changes: 100 additions & 0 deletions lib/stripe/payment_methods/payment_method.ex
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
78 changes: 78 additions & 0 deletions test/stripe/payment_methods/payment_method_test.exs
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

0 comments on commit b1ee33c

Please sign in to comment.