Packages
stripity_stripe
2.2.1
3.3.2
3.3.1
3.2.0
3.1.1
3.1.0
3.0.0
2.17.3
2.17.2
2.17.1
2.17.0
2.16.0
2.15.1
2.15.0
2.14.1
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.2
2.7.1
2.7.0
2.6.0
2.5.0
2.4.0
2.3.0
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.1
2.0.0
2.0.0-alpha.11
2.0.0-alpha.10
2.0.0-alpha.9
2.0.0-alpha.8
2.0.0-alpha.7
2.0.0-alpha.6
2.0.0-alpha.5
2.0.0-alpha.4
2.0.0-alpha.3
2.0.0-alpha.2
2.0.0-alpha.1
1.6.2
1.6.1
1.6.0
1.4.0
1.3.0
1.2.0
1.1.0
0.5.0
0.4.0
0.3.0
0.2.0
A Stripe client for Elixir.
Current section
Files
Jump to
Current section
Files
lib/stripe/payment_methods/card.ex
defmodule Stripe.Card do
@moduledoc """
Work with Stripe card objects.
You can:
- Create a card
- Retrieve a card
- Update a card
- Delete a card
If you have been using an old version of the library, note that the functions which take an
`owner_type` argument are now deprecated.
The owner type is indicated by setting either the `recipient` or `customer`
```
Stripe API reference: https://stripe.com/docs/api#cards
"""
use Stripe.Entity
import Stripe.Request
@type t :: %__MODULE__{
id: Stripe.id(),
object: String.t(),
account: Stripe.id() | Stripe.Account.t() | nil,
address_city: String.t() | nil,
address_country: String.t() | nil,
address_line1: String.t() | nil,
address_line1_check: String.t() | nil,
address_line2: String.t() | nil,
address_state: String.t() | nil,
address_zip: String.t() | nil,
address_zip_check: String.t() | nil,
available_payout_methods: list(String.t()) | nil,
brand: String.t(),
country: String.t() | nil,
currency: String.t() | nil,
customer: Stripe.id() | Stripe.Customer.t() | nil,
cvc_check: String.t() | nil,
default_for_currency: boolean | nil,
deleted: boolean | nil,
dynamic_last4: String.t() | nil,
exp_month: integer,
exp_year: integer,
fingerprint: String.t() | nil,
funding: String.t(),
last4: String.t(),
metadata: Stripe.Types.metadata(),
name: String.t() | nil,
recipient: Stripe.id() | Stripe.Recipient.t() | nil,
tokenization_method: String.t() | nil
}
defstruct [
:id,
:object,
:account,
:address_city,
:address_country,
:address_line1,
:address_line1_check,
:address_line2,
:address_state,
:address_zip,
:address_zip_check,
:available_payout_methods,
:brand,
:country,
:currency,
:customer,
:cvc_check,
:default_for_currency,
:deleted,
:dynamic_last4,
:exp_month,
:exp_year,
:fingerprint,
:funding,
:last4,
:metadata,
:name,
:recipient,
:tokenization_method
]
defp plural_endpoint(%{customer: id}) do
"customers/" <> id <> "/sources"
end
@doc """
Create a card.
This requires a `token` created by a library like Stripe.js.
For PCI compliance reasons you should not send a card's number or CVC
to your own server.
If you want to create a card with your server without a token, you
can use the low-level API.
"""
@spec create(params, Keyword.t()) :: {:ok, t} | {:error, Stripe.Error.t()}
when params: %{
:customer => Stripe.id() | Stripe.Customer.t(),
:source => Stripe.id() | Stripe.Source.t(),
optional(:metadata) => Stripe.Types.metadata(),
}
def create(%{customer: _, source: _} = params, opts \\ []) do
new_request(opts)
|> put_endpoint(params |> plural_endpoint())
|> put_params(params |> Map.delete(:customer))
|> put_method(:post)
|> make_request()
end
@doc """
Retrieve a card.
"""
@spec retrieve(Stripe.id() | t, map, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def retrieve(id, %{customer: _} = params, opts \\ []) do
endpoint = params |> plural_endpoint()
new_request(opts)
|> put_endpoint(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: %{
:id => String.t(),
optional(:address_city) => String.t(),
optional(:address_country) => String.t(),
optional(:address_line1) => String.t(),
optional(:address_line2) => String.t(),
optional(:address_state) => String.t(),
optional(:address_zip) => String.t(),
optional(:exp_month) => String.t(),
optional(:exp_year) => String.t(),
optional(:metadata) => Stripe.Types.metadata(),
optional(:name) => String.t(),
}
def update(id, %{customer: _} = params, opts \\ []) do
endpoint = params |> plural_endpoint()
new_request(opts)
|> put_endpoint(endpoint <> "/#{get_id!(id)}")
|> put_method(:post)
|> put_params(params |> Map.delete(:customer))
|> make_request()
end
@doc """
Delete a card.
"""
@spec delete(Stripe.id() | t, map, Stripe.options()) :: {:ok, t} | {:error, Stripe.Error.t()}
def delete(id, %{customer: _} = params, opts \\ []) do
endpoint = params |> plural_endpoint()
new_request(opts)
|> put_endpoint(endpoint <> "/#{get_id!(id)}")
|> put_method(:delete)
|> make_request()
end
@doc """
List all cards.
"""
@spec list(params, Stripe.options()) :: {:ok, Stripe.List.t(t)} | {:error, Stripe.Error.t()}
when params: %{
:customer => Stripe.id() | Stripe.Customer.t(),
optional(:ending_before) => t | Stripe.id(),
optional(:limit) => 1..100,
optional(:starting_after) => t | Stripe.id(),
}
def list(%{customer: _} = params, opts \\ []) do
endpoint = params |> plural_endpoint()
params = params |> Map.put(:object, "card")
new_request(opts)
|> prefix_expansions()
|> put_endpoint(endpoint)
|> put_method(:get)
|> put_params(params |> Map.delete(:customer))
|> make_request()
end
end