Skip to content

Commit

Permalink
Add create card for an existing customer
Browse files Browse the repository at this point in the history
  • Loading branch information
juljimm committed Feb 5, 2016
1 parent 688b933 commit dfb5762
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/stripe/cards.ex
@@ -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
41 changes: 41 additions & 0 deletions test/stripe/card_test.exs
@@ -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
13 changes: 13 additions & 0 deletions test/test_helper.exs
Expand Up @@ -19,6 +19,19 @@ defmodule Helper do
Stripe.Plans.delete "test-dlx"
end

def create_test_token do
params = [
card: [
number: "4242424242424242",
exp_month: 8,
exp_year: 2016,
cvc: "314"
]
]
{:ok, token} = Stripe.Tokens.create(params)
token
end

def create_test_customer( email ) do
new_customer = [
email: "#{email}",
Expand Down

0 comments on commit dfb5762

Please sign in to comment.