Skip to content

Commit

Permalink
1. Refactor wirecard module
Browse files Browse the repository at this point in the history
2. Add multiple function clause for authorize and purchase
  • Loading branch information
kartikjagdale committed Dec 15, 2017
1 parent ff46c64 commit 700a06b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 55 deletions.
98 changes: 45 additions & 53 deletions lib/kuber_hex/gateways/wire_card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Kuber.Hex.Gateways.WireCard do
"""
@valid_phone_format ~r/\+\d{1,3}(\(?\d{3}\)?)?\d{3}-\d{4}-\d{3}/
@default_currency "EUR"

@default_amount 100
use Kuber.Hex.Gateways.Base
use Kuber.Hex.Adapter, required_config: [:login, :password, :signature]

Expand Down Expand Up @@ -73,14 +73,22 @@ defmodule Kuber.Hex.Gateways.WireCard do
billing_address: address,
description: 'Wirecard remote test purchase',
email: "[email protected]",
ip: "127.0.0.1"
ip: "127.0.0.1",
test: true
]
"""
@spec authorize(Float, CreditCard, Keyword) :: { :ok, Map }
@spec authorize(Float, String.t, Keyword) :: { :ok, Map }
def authorize(money, payment_method, options \\ []) do
options = options
|> check_and_return_payment_method(payment_method)
def authorize(money, payment_method, options \\ [])

@spec authorize(Integer | Float, CreditCard, Keyword) :: { :ok, Map }
def authorize(money, %CreditCard{} = creditcard, options) do
options = add_to_opts(:credit_card, creditcard, options)
response = commit(:post, :preauthorization, money, options)
response
end

@spec authorize(Integer | Float, String.t, Keyword) :: { :ok, Map }
def authorize(money, authorization, options) when is_binary(authorization) do
options = add_to_opts(:preauthorization, authorization, options)
response = commit(:post, :preauthorization, money, options)
response
end
Expand All @@ -101,14 +109,20 @@ defmodule Kuber.Hex.Gateways.WireCard do
transaction. If a GuWID is given, rather than a CreditCard,
then then the :recurring option will be forced to "Repeated"
"""
@spec purchase(Float, String.t, Keyword) :: { :ok, Map }
@spec purchase(Float, CreditCard, Keyword) :: { :ok, Map }
def purchase(money, payment_method, options \\ []) do
options = options
|> check_and_return_payment_method(payment_method)
def purchase(money, payment_method, options \\ [])

@spec purchase(Float | Integer, CreditCard, Keyword) :: { :ok, Map }
def purchase(money, %CreditCard{} = creditcard, options) do
options = Keyword.put(options, :credit_card, creditcard)
commit(:post, :purchase, money, options)
end


@spec purchase(Float | Integer, String.t, Keyword) :: { :ok, Map }
def purchase(money, authorization, options) when is_binary(authorization) do
options = Keyword.put(options, :preauthorization, authorization)
commit(:post, :purchase, money, options)
end

@doc """
Void - A credit card purchase that a seller cancels after it has
been authorized but before it has been settled.
Expand Down Expand Up @@ -180,7 +194,7 @@ defmodule Kuber.Hex.Gateways.WireCard do
options = options
|> Keyword.put(:credit_card, creditcard)
|> Keyword.put(:recurring, "Initial")
money = options[:amount] || 100
money = options[:amount] || @default_amount
# Amex does not support authorization_check
case creditcard.brand do
"american_express" -> commit(:post, :preauthorization, money, options)
Expand All @@ -190,17 +204,9 @@ defmodule Kuber.Hex.Gateways.WireCard do

# =================== Private Methods ===================

# Check if paymenth method is creditcard or authorization code
defp check_and_return_payment_method(options, payment_method) do
case payment_method do
%CreditCard{} ->
options = options |> Keyword.put(:credit_card, payment_method)
_ ->
options = options |> Keyword.put(:preauthorization, payment_method)
end
options
end

# Add new key value to given keyword options
def add_to_opts(key, value, options), do: Keyword.put(options, key, value)

# Contact WireCard, make the XML request, and parse the
# reply into a Response object.
defp commit(method, action, money, options) do
Expand All @@ -209,7 +215,7 @@ defmodule Kuber.Hex.Gateways.WireCard do

headers = %{ "Content-Type" => "text/xml",
"Authorization" => encoded_credentials(options[:config][:login], options[:config][:password]) }
method |> HTTPoison.request(@test_url , request, headers) |> respond
method |> HTTPoison.request(base_url(options) , request, headers) |> respond
end

defp respond({:ok, %{status_code: 200, body: body}}) do
Expand All @@ -222,24 +228,10 @@ defmodule Kuber.Hex.Gateways.WireCard do
end

# Read the XML message from the gateway and check if it was successful,
# and also extract required return values from the response.
# For Error =>
# {:GuWID=>"C663288151298675530735",
# :AuthorizationCode=>"",
# :StatusType=>"INFO",
# :FunctionResult=>"NOK",
# :ERROR_Type=>"DATA_ERROR",
# :ERROR_Number=>"20080",
# :ERROR_Message=>"Could not find referenced transaction for GuWID C428094138244444404448.",
# :TimeStamp=>"2017-12-11 11:05:55",
# "ErrorCode"=>"20080",
# :Message=>"Could not find referenced transaction for GuWID C428094138244444404448."}
# =================================================
# For Response
# and also extract required return values from the response
# TODO: parse XML Response
defp parse(data) do
data
|> XmlToMap.naive_map
XmlToMap.naive_map(data)
end

# Generates the complete xml-message, that gets sent to the gateway
Expand Down Expand Up @@ -357,7 +349,7 @@ defmodule Kuber.Hex.Gateways.WireCard do
def add_invoice(money, options) do
[
add_amount(money, options),
element(:Currency, options[:currency] || @default_currency),
element(:Currency, currency(options)),
element(:CountryCode, options[:billing_address][:country]),
element(:RECURRING_TRANSACTION, [
element(:Type, (options[:recurring] || "Single"))
Expand All @@ -368,12 +360,12 @@ defmodule Kuber.Hex.Gateways.WireCard do
# Include the amount in the transaction-xml
# TODO: check for localized currency or currency
# localized_amount(money, options[:currency] || currency(money))
defp add_amount(money, options) do
element(:Amount, money)
end
defp add_amount(money, options), do: element(:Amount, money)

defp atom_to_upcase_string(atom) do
String.upcase to_string atom
atom
|> to_string
|> String.upcase
end

# Encode login and password in Base64 to supply as HTTP header
Expand All @@ -384,11 +376,11 @@ defmodule Kuber.Hex.Gateways.WireCard do
|> (&( "Basic "<> &1)).()
end

defp join_string(list_of_words, joiner) do
Enum.join(list_of_words, joiner)
end
defp join_string(list_of_words, joiner), do: Enum.join(list_of_words, joiner)

defp regex_match(regex, string) do
Regex.match?(regex, string)
end
defp regex_match(regex, string), do: Regex.match?(regex, string)

defp base_url(opts), do: if opts[:test], do: @test_url, else: @live_url

defp currency(opts), do: opts[:currency] || @default_currency
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Kuber.Hex.Mixfile do
defp deps do
[{:poison, "~> 3.1.0"},
{:httpoison, "~> 0.13.0"},
{:ex_doc, ">= 0.6.0", only: :dev},
{:ex_doc, "~> 0.16", only: :dev, runtime: false},
{:mock, ">= 0.1.0", only: :test},
{:xml_builder, "~> 0.1.1"},
{:elixir_xml_to_map, "~> 0.1"}]
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
%{"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"},
"earmark": {:hex, :earmark, "1.2.4", "99b637c62a4d65a20a9fb674b8cffb8baa771c04605a80c911c4418c69b75439", [], [], "hexpm"},
"elixir_xml_to_map": {:hex, :elixir_xml_to_map, "0.1.1", "57e924cd11731947bfd245ce57d0b8dd8b7168bf8edb20cd156a2982ca96fdfa", [], [{:erlsom, "~>1.4", [hex: :erlsom, repo: "hexpm", optional: false]}], "hexpm"},
"erlsom": {:hex, :erlsom, "1.4.1", "53dbacf35adfea6f0714fd0e4a7b0720d495e88c5e24e12c5dc88c7b62bd3e49", [], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.7.3", "8f52bfbfcbc0206dd08dd94aae86a3fd5330ba2f37c73cfea2918ed4c96d1769", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"ex_doc": {:hex, :ex_doc, "0.18.1", "37c69d2ef62f24928c1f4fdc7c724ea04aecfdf500c4329185f8e3649c915baf", [], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down

0 comments on commit 700a06b

Please sign in to comment.