Skip to content

Commit

Permalink
add initial billing checkout step (refers #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreapavoni committed Oct 2, 2015
1 parent 54d5267 commit 3fa1bc1
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
9 changes: 4 additions & 5 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ config :nova, Nova.Repo,
port: System.get_env("NOVA_DB_PORT"),
pool_size: 10

config :dogma,
exclude: [
~r(\Atest/),
~r(\Aconfig/)
]
config :nova, :payment_gateway,
type: Commerce.Billing.Gateways.Bogus,
credentials: {"", ""},
default_currency: "USD"
5 changes: 5 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ config :nova, Nova.Repo,
database: "nova_test",
port: System.get_env("NOVA_DB_PORT"),
pool: Ecto.Adapters.SQL.Sandbox

config :nova, :payment_gateway,
type: Commerce.Billing.Gateways.Bogus,
credentials: {"", ""},
default_currency: "USD"
13 changes: 12 additions & 1 deletion lib/nova.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Nova do
# Start the Ecto repository
worker(Nova.Repo, []),
# Here you could define other workers and supervisors as children
# worker(Nova.Worker, [arg1, arg2, arg3]),
worker(Commerce.Billing.Worker, payment_settings)
]

# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
Expand All @@ -28,4 +28,15 @@ defmodule Nova do
Nova.Endpoint.config_change(changed, removed)
:ok
end

defp payment_settings do
config = Application.get_env(:nova, :payment_gateway)
type = Dict.get(config, :type)
settings = %{
credentials: Dict.get(config, :credentials),
default_currency: Dict.get(config, :default_currency)
}

[type, settings, [name: :payment_gateway]]
end
end
56 changes: 56 additions & 0 deletions lib/nova/checkout/billing.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Nova.Checkout.Billing do
@moduledoc """
Provides `:billing` behaviour for checkout.
This mostly serves as an example to build a custom checkout.
"""

alias Commerce.Billing

defmacro __using__(_opts) do
quote do
@doc """
Defines billing behaviour for checkout.
"""
def checkout(order, :billing, params) do
result = Billing.authorize(
:payment_gateway,
order.total,
get_card(params["credit_card"]),
billing_address: get_address(params["billing_address"]),
description: "Payment for order #{order.id}"
)

case result do
{:ok, payment_response} -> {:ok, order, payment_response}
{:error, payment_response} -> {:error, order, payment_response.error}
end
end

defp get_card(params) do
%Billing.CreditCard{
name: params["name"],
number: params["number"],
expiration: get_expiration(params["year"], params["month"]),
cvc: params["cvc"]
}
end

defp get_expiration(year, month) when byte_size(year) > 0 and byte_size(month) > 0 do
{String.to_integer(year), String.to_integer(month)}
end
defp get_expiration(_, _), do: {0, 0}

defp get_address(params) do
%Billing.Address{
street1: params["street1"],
street2: params["street2"],
city: params["city"],
region: params["region"],
country: params["country"],
postal_code: params["postal_code"]
}
end
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ defmodule Nova.Mixfile do
{:phoenix_html, "~> 2.2"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:cowboy, "~> 1.0"},
{:commerce_billing, github: "joshnuss/commerce_billing"},
{:ex_spec, "~> 0.3.0", only: :test},
# using git version to support fixtures/2
{:ecto_fixtures, git: "https://github.com/dockyard/ecto_fixtures.git", branch: "master", only: :test},
{:ecto_fixtures, github: "dockyard/ecto_fixtures", branch: "master", only: :test},
{:dogma, "~> 0.0", only: :dev},
{:excoveralls, only: ~w(dev test)a}
]
Expand Down
8 changes: 7 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
%{"cowboy": {:hex, :cowboy, "1.0.3"},
%{"commerce_billing": {:git, "https://github.com/joshnuss/commerce_billing.git", "b5a49ac82c09de9bb70e7da1ef5f5f6231a485b9", []},
"cowboy": {:hex, :cowboy, "1.0.3"},
"cowlib": {:hex, :cowlib, "1.0.1"},
"decimal": {:hex, :decimal, "1.1.0"},
"dogma": {:hex, :dogma, "0.0.8"},
"ecto": {:hex, :ecto, "1.0.3"},
"ecto_fixtures": {:git, "https://github.com/dockyard/ecto_fixtures.git", "21be22d47737626098304768cf661d79bb281301", [branch: "master"]},
"ex_doc": {:hex, :ex_doc, "0.10.0"},
"ex_spec": {:hex, :ex_spec, "0.3.0"},
"excoveralls": {:hex, :excoveralls, "0.3.11"},
"exjsx": {:hex, :exjsx, "3.2.0"},
"fs": {:hex, :fs, "0.9.2"},
"hackney": {:hex, :hackney, "1.3.2"},
"httpoison": {:hex, :httpoison, "0.7.4"},
"idna": {:hex, :idna, "1.0.2"},
"jazz": {:hex, :jazz, "0.2.1"},
"jsx": {:hex, :jsx, "2.6.2"},
"meck": {:hex, :meck, "0.8.3"},
"mock": {:hex, :mock, "0.1.1"},
"phoenix": {:hex, :phoenix, "1.0.2"},
"phoenix_ecto": {:hex, :phoenix_ecto, "1.2.0"},
"phoenix_html": {:hex, :phoenix_html, "2.2.0"},
Expand Down
39 changes: 39 additions & 0 deletions test/nova/checkout/billing_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Nova.Checkout.BillingTest do
use Nova.ModelCase

defmodule TestCart do
use Nova.Checkout.Billing
end

setup do
order = fixtures(:orders).orders.base

{:ok, order: order}
end

@params %{
"billing_address" => %{
"street1" => "example street1",
"street2" => "example street2",
"city" => "Testville",
"region" => "Region",
"country" => "IT",
"postal_code" => "12345"
},
"credit_card" => %{
"name" => "John Doe",
"number" => "4111111111111111",
"month" => "10",
"year" => "2019",
"cvc" => "123"
}
}

describe "checkout/3" do
context "with valid data" do
it "sends billing address and credit card to payment gateway", ctx do
assert {:ok, _, _} = TestCart.checkout(ctx.order, :billing, @params)
end
end
end
end

0 comments on commit 3fa1bc1

Please sign in to comment.