-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial billing checkout step (refers #9)
- Loading branch information
1 parent
54d5267
commit 3fa1bc1
Showing
7 changed files
with
125 additions
and
8 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
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
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
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,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 |
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
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
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,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 |