Skip to content

Commit

Permalink
Separate payment into card and address (#56)
Browse files Browse the repository at this point in the history
Changes:-
* refactor stripe code to accept payment with card details and pass address in options.
* refactor stripe current test cases according to the above refactored code.
  • Loading branch information
chandradot99 authored and pkrawat1 committed Dec 28, 2017
1 parent b0fe2c2 commit cc71d06
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Add configs in `config/config.exs` file.
```elixir
config :gringotts, Gringotts.Gateways.Stripe,
adapter: Gringotts.Gateways.Stripe,
api_key: "YOUR_KEY",
secret_key: "YOUR_KEY",
default_currency: "USD"

```
Expand All @@ -64,7 +64,7 @@ card = %CreditCard{
number: "4242424242424242",
year: "2017",
month: "12",
cvc: "123"
verification_code: "123"
}

address = %Address{
Expand All @@ -75,8 +75,9 @@ address = %Address{
postal_code: "11111"
}

case Gringotts.purchase(:my_gateway, Stripe, 199.95, card, billing_address: address,
description: "Amazing T-Shirt") do
opts = [address: address, currency: "eur"]

case Gringotts.purchase(:payment_worker, Stripe, 10, card, opts) do
{:ok, %{authorization: authorization}} ->
IO.puts("Payment authorized #{authorization}")

Expand Down
33 changes: 24 additions & 9 deletions lib/gringotts/gateways/stripe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,22 +253,29 @@ defmodule Gringotts.Gateways.Stripe do
# Private methods

defp create_params_for_auth_or_purchase(amount, payment, opts, capture \\ true) do
optional_params(opts)
params = optional_params(opts)
++ [capture: capture]
++ amount_params(amount)
++ source_params(payment, opts)

params
|> Keyword.has_key?(:currency)
|> with_currency(params, opts[:config])
end

def with_currency(true, params, config), do: params
def with_currency(false, params, config), do: [{:currency, config[:default_currency]} | params]

defp create_card_token(params, opts) do
commit(:post, "tokens", params, opts)
end

defp amount_params(amount), do: [amount: money_to_cents(amount)]

defp source_params(%{} = payment, opts) do
defp source_params(%CreditCard{} = card, opts) do
params =
card_params(payment) ++
address_params(payment)
card_params(card) ++
address_params(opts[:address])

response = create_card_token(params, opts)

Expand All @@ -288,18 +295,24 @@ defmodule Gringotts.Gateways.Stripe do
end
end

defp card_params(%{} = card) do
defp source_params(_, opts), do: []

defp card_params(%CreditCard{} = card) do
card = Map.from_struct(card)

[ "card[name]": card[:name],
"card[number]": card[:number],
"card[exp_year]": card[:year],
"card[exp_month]": card[:month],
"card[cvc]": card[:cvc]
"card[cvc]": card[:verification_code]
]
end

defp card_params(_), do: []

defp address_params(%{} = address) do
defp address_params(%Address{} = address) do
address = Map.from_struct(address)

[ "card[address_line1]": address[:street1],
"card[address_line2]": address[:street2],
"card[address_city]": address[:city],
Expand All @@ -312,15 +325,17 @@ defmodule Gringotts.Gateways.Stripe do
defp address_params(_), do: []

defp commit(method, path, params \\ [], opts \\ []) do
auth_token = "Bearer " <> opts[:config][:api_key]
auth_token = "Bearer " <> opts[:config][:secret_key]
headers = [{"Content-Type", "application/x-www-form-urlencoded"}, {"Authorization", auth_token}]
data = params_to_string(params)
response = HTTPoison.request(method, "#{@base_url}/#{path}", data, headers)
format_response(response)
end

defp optional_params(opts) do
Keyword.delete(opts, :config)
opts
|> Keyword.delete(:config)
|> Keyword.delete(:address)
end

defp format_response(response) do
Expand Down
57 changes: 31 additions & 26 deletions test/gateways/stripe_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ defmodule Gringotts.Gateways.StripeTest do
use ExUnit.Case

alias Gringotts.Gateways.Stripe
alias Gringotts.{
CreditCard,
Address
}

@required_payment_attrs %{
month: 12,
year: 2018,
@card %CreditCard{
first_name: "John",
last_name: "Smith",
number: "4242424242424242",
cvc: "123"
year: "2017",
month: "12",
verification_code: "123"
}

@optional_payment_attrs %{
name: "John Doe",
@address %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
Expand All @@ -21,33 +26,33 @@ defmodule Gringotts.Gateways.StripeTest do
postal_code: "11111"
}

@config [config: [api_key: "sk_test_vIX41hayC0BKrPWQerLuOMld"]]
@required_opts [currency: "usd"]
@required_opts [config: [api_key: "sk_test_vIX41hayC0BKrPWQerLuOMld"], currency: "usd"]
@optional_opts [address: @address]

describe "authorize/3" do
test "should authorize wth required payment and required opts attrs" do
amount = 5
response = Stripe.authorize(amount, @required_payment_attrs, @config ++ @required_opts)
# test "should authorize wth card and required opts attrs" do
# amount = 5
# response = Stripe.authorize(amount, @card, @required_opts ++ @optional_opts)

assert Map.has_key?(response, "id")
assert response["amount"] == 500
assert response["captured"] == false
assert response["currency"] == "usd"
end
# assert Map.has_key?(response, "id")
# assert response["amount"] == 500
# assert response["captured"] == false
# assert response["currency"] == "usd"
# end

test "should not authorize if required payment attrs not present" do
amount = 5
response = Stripe.authorize(amount, @optional_payment_attrs, @config ++ @required_opts)
# test "should not authorize if card is not passed" do
# amount = 5
# response = Stripe.authorize(amount, %{}, @required_opts ++ @optional_opts)

assert Map.has_key?(response, "error")
end
# assert Map.has_key?(response, "error")
# end

test "should not authorize if required opts not present" do
amount = 5
response = Stripe.authorize(amount, @required_payment_attrs, @config)
# test "should not authorize if required opts not present" do
# amount = 5
# response = Stripe.authorize(amount, @card, @optional_opts)

assert Map.has_key?(response, "error")
end
# assert Map.has_key?(response, "error")
# end

end
end

0 comments on commit cc71d06

Please sign in to comment.