Skip to content

Commit

Permalink
add the ability to create new credit cards with stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorganIO committed Apr 18, 2017
1 parent 2731c10 commit 45e70fa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
18 changes: 7 additions & 11 deletions apps/ryal_core/lib/ryal/web/models/payment_gateways/stripe.ex
Expand Up @@ -16,9 +16,7 @@ defmodule Ryal.PaymentGateway.Stripe do
end

defp create_object(schema, type, path, stripe_base) do
response = stripe_base
<> path
|> HTTPotion.post([body: params(type, schema)])
response = HTTPotion.post(stripe_base <> path, [body: params(type, schema)])

with {:ok, body} <- Poison.decode(response.body),
do: {:ok, body["id"]}
Expand Down Expand Up @@ -52,14 +50,12 @@ defmodule Ryal.PaymentGateway.Stripe do

defp params(:credit_card, credit_card) do
URI.encode_query %{
source: %{
object: "card",
exp_month: credit_card.month,
exp_year: credit_card.year,
number: credit_card.number,
cvc: credit_card.cvc,
name: credit_card.name
}
"source[object]" => "card",
"source[exp_month]" => credit_card.month,
"source[exp_year]" => credit_card.year,
"source[number]" => credit_card.number,
"source[cvc]" => credit_card.cvc,
"source[name]" => credit_card.name
}
end

Expand Down
24 changes: 24 additions & 0 deletions apps/ryal_core/test/fixtures/stripe/credit_card.json
@@ -0,0 +1,24 @@
{
"id": "card_1AA3En2BZSQJcNSQ77orWzVS",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"customer": "cus_AUIXSS9KUHRv5H",
"cvc_check": null,
"dynamic_last4": null,
"exp_month": 8,
"exp_year": 2018,
"funding": "credit",
"last4": "4242",
"metadata": {},
"name": null,
"tokenization_method": null
}
42 changes: 34 additions & 8 deletions apps/ryal_core/test/models/payment_gateways/stripe_test.exs
Expand Up @@ -4,32 +4,58 @@ defmodule Ryal.PaymentGateway.StripeTest do
alias Dummy.User
alias Ryal.PaymentGateway
alias Ryal.PaymentGateway.Stripe
alias Ryal.PaymentMethod.CreditCard
alias Ryal.PaymentMethod
alias Plug.Conn

setup do
[bypass: Bypass.open]
end

describe ".create/3" do
test "will return an id for a customer", %{bypass: bypass} do
setup do
user = %User{}
|> User.changeset(%{email: "ryal@example.com"})
|> Repo.insert!

[user: user]
end

test "will return an id for a customer", %{bypass: bypass, user: user} do
Bypass.expect bypass, fn(conn) ->
assert "/v1/customers" == conn.request_path
assert "POST" == conn.method

Conn.resp(conn, 201, read_fixture("stripe/customer.json"))
end

user = %User{}
|> User.changeset(%{email: "ryal@example.com"})
|> Repo.insert!

result = Stripe.create(:customer, user, bypass_endpoint(bypass))
assert {:ok, "cus_AMUcqwTDYlbBSp"} == result
end

test "will return an id for a credit card" do
user = %CreditCard{}
test "will return an id for a credit card", %{bypass: bypass, user: user} do
Bypass.expect bypass, fn(conn) ->
assert "/v1/credit_cards" == conn.request_path
assert "POST" == conn.method

Conn.resp(conn, 201, read_fixture("stripe/credit_card.json"))
end

credit_card = %PaymentMethod{}
|> PaymentMethod.changeset(%{
type: "credit_card",
user_id: user.id,
proxy: %{
name: "Bobby Orr",
number: "4242 4242 4242 4242",
month: "08",
year: "2029",
cvc: "123"
}
})
|> Ryal.repo.insert!

result = Stripe.create(:credit_card, credit_card.proxy.data, bypass_endpoint(bypass))
assert {:ok, "card_1AA3En2BZSQJcNSQ77orWzVS"} == result
end
end

Expand Down

0 comments on commit 45e70fa

Please sign in to comment.