forked from aviabird/gringotts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Add multiple function clause for authorize and purchase
- Loading branch information
1 parent
ff46c64
commit 700a06b
Showing
3 changed files
with
48 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
||
|
@@ -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 | ||
|
@@ -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. | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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")) | ||
|
@@ -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 | ||
|
@@ -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 |
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