-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create card for an existing customer
- Loading branch information
Showing
3 changed files
with
102 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule Stripe.Cards do | ||
@moduledoc """ | ||
Main API for working with Cards at Stripe. Through this API you can: | ||
- create | ||
(API ref https://stripe.com/docs/api#subscriptions) | ||
""" | ||
@endpoint "customers" | ||
|
||
@doc """ | ||
Create a card for the specified customer. | ||
## Example | ||
``` | ||
new_card = [ | ||
source: token, | ||
metadata: [ | ||
... | ||
] | ||
] | ||
{:ok, card} = Stripe.Cards.create customer_id, new_card | ||
``` | ||
""" | ||
def create(customer_id, opts) do | ||
create customer_id, opts, Stripe.config_or_env_key | ||
end | ||
|
||
@doc """ | ||
Create a card for the specified customer using given api key. | ||
## Example | ||
``` | ||
new_card = [ | ||
source: token, | ||
metadata: [ | ||
... | ||
] | ||
] | ||
{:ok, card} = Stripe.Cards.create customer_id, new_card | ||
``` | ||
""" | ||
def create(customer_id, opts, key) do | ||
Stripe.make_request_with_key(:post, "#{@endpoint}/#{customer_id}/sources", key, opts) | ||
|> Stripe.Util.handle_stripe_response | ||
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,41 @@ | ||
defmodule Stripe.CardTest do | ||
use ExUnit.Case | ||
|
||
#these tests are dependent on the execution order | ||
# ExUnit.configure w/ seed: 0 was set | ||
setup_all do | ||
Stripe.Customers.delete_all | ||
customer = Helper.create_test_customer "card_test1@localhost" | ||
on_exit fn -> | ||
Stripe.Customers.delete customer.id | ||
end | ||
|
||
{:ok, [customer: customer]} | ||
end | ||
|
||
|
||
@tag disabled: false | ||
test "Create w/opts works", %{customer: customer} do | ||
token = Helper.create_test_token | ||
opts = [ | ||
source: token.id | ||
] | ||
case Stripe.Cards.create customer.id, opts do | ||
{:ok, card} -> assert card.customer == customer.id | ||
{:error, err} -> flunk err | ||
end | ||
end | ||
|
||
@tag disabled: false | ||
test "Create w/opts w/key works", %{customer: customer} do | ||
token = Helper.create_test_token | ||
opts = [ | ||
source: token.id | ||
] | ||
case Stripe.Cards.create customer.id, opts, Stripe.config_or_env_key do | ||
{:ok, card} -> assert card.customer == customer.id | ||
{:error, err} -> flunk err | ||
end | ||
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