Skip to content

Commit

Permalink
Checked Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
anantanant2015 committed May 7, 2018
1 parent 96000d8 commit b9239e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
58 changes: 34 additions & 24 deletions lib/gringotts/gateways/adyen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Gringotts.Gateways.Adyen do
| Action | Method |
| ------ | ------ |
## The `opts` argument
Expand All @@ -24,7 +24,7 @@ defmodule Gringotts.Gateways.Adyen do
| Config parameter | ADYEN secret |
| ------- | ---- |
[home]: https://www.adyen.com/
[docs]: https://docs.adyen.com/developers
Expand Down Expand Up @@ -59,6 +59,7 @@ defmodule Gringotts.Gateways.Adyen do
@spec purchase(Money.t(), String.t(), keyword) :: {:ok | :error, Response.t()}
def purchase(amount, card, opts) do
{auth_atom, auth_response} = authorize(amount, card, opts)

case auth_atom do
:ok -> capture(auth_response.id, amount, opts)
_ -> {auth_atom, auth_response}
Expand All @@ -81,10 +82,11 @@ defmodule Gringotts.Gateways.Adyen do
case Keyword.get(opts, :requestParameters) do
nil ->
body = get_authorize_params(rec_card, amount, opts)

_ ->
body = Enum.into(opts[:requestParameters], get_authorize_params(rec_card, amount, opts))
end

Poison.encode!(body)
end

Expand All @@ -93,45 +95,49 @@ defmodule Gringotts.Gateways.Adyen do
card: card_map(rec_card),
amount: amount_map(amount),
merchantAccount: opts[:config][:account]
}
}
end

defp capture_and_refund_params(id, amount, opts) do
case Keyword.get(opts, :requestParameters) do
nil ->
nil ->
body = get_capture_and_refund_params(id, amount, opts)
_ ->
body = Enum.into(opts[:requestParameters], get_capture_and_refund_params(id, amount, opts))

_ ->
body =
Enum.into(opts[:requestParameters], get_capture_and_refund_params(id, amount, opts))
end

Poison.encode!(body)
end

defp get_capture_and_refund_params(id, amount, opts) do
%{
"originalReference": id,
"modificationAmount": amount_map(amount),
"merchantAccount": opts[:config][:account]
originalReference: id,
modificationAmount: amount_map(amount),
merchantAccount: opts[:config][:account]
}
end

defp void_params(id, opts) do
case Keyword.get(opts, :requestParameters) do
nil ->
body = get_void_params(id, opts)

_ ->
body = Enum.into(opts[:requestParameters], get_void_params(id, opts))
end

Poison.encode!(body)
end

defp get_void_params(id, opts) do
%{
"originalReference": id,
"merchantAccount": opts[:config][:account]
originalReference: id,
merchantAccount: opts[:config][:account]
}
end

defp card_map(%CreditCard{} = rec_card) do
%{
number: rec_card.number,
Expand All @@ -144,6 +150,7 @@ defmodule Gringotts.Gateways.Adyen do

defp amount_map(amount) do
{currency, int_value, _} = Money.to_integer(amount)

%{
value: int_value,
currency: currency
Expand All @@ -152,6 +159,7 @@ defmodule Gringotts.Gateways.Adyen do

defp commit(method, endpoint, params, opts) do
head = headers(opts)

method
|> HTTPoison.request(base_url(opts) <> endpoint, params, headers(opts))
|> respond(opts)
Expand All @@ -163,19 +171,20 @@ defmodule Gringotts.Gateways.Adyen do
gateway_code = parsed_resp["status"]

status = if gateway_code == 200 || response.status_code == 200, do: :ok, else: :error

{status,
%Response{
id: parsed_resp["pspReference"],
status_code: response.status_code,
gateway_code: gateway_code,
reason: parsed_resp["errorType"],
message: parsed_resp["message"] || parsed_resp["resultCode"] || parsed_resp["response"],
raw: response.body
}}
%Response{
id: parsed_resp["pspReference"],
status_code: response.status_code,
gateway_code: gateway_code,
reason: parsed_resp["errorType"],
message:
parsed_resp["message"] || parsed_resp["resultCode"] || parsed_resp["response"],
raw: response.body
}}

:error ->
{:error,
%Response{raw: response.body, reason: "could not parse ADYEN response"}}
{:error, %Response{raw: response.body, reason: "could not parse ADYEN response"}}
end
end

Expand All @@ -191,6 +200,7 @@ defmodule Gringotts.Gateways.Adyen do

defp headers(opts) do
arg = get_in(opts, [:config, :username]) <> ":" <> get_in(opts, [:config, :password])

[
{"Authorization", "Basic #{Base.encode64(arg)}"}
| @headers
Expand Down
6 changes: 4 additions & 2 deletions test/integration/gateways/adyen_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ defmodule Gringotts.Integration.Gateways.AdyenTest do

@amount Money.new(4200, :EUR)

@reference "payment-#{DateTime.utc_now |> DateTime.to_string |> String.replace([" ", ":", "."], "-")}"
@reference "payment-#{
DateTime.utc_now() |> DateTime.to_string() |> String.replace([" ", ":", "."], "-")
}"

@card %CreditCard{
brand: "VISA",
Expand All @@ -35,7 +37,7 @@ defmodule Gringotts.Integration.Gateways.AdyenTest do
Application.delete_env(:gringotts, Gateway)
end)
end

setup do
[opts: [requestParameters: %{"reference" => @reference}]]
end
Expand Down

0 comments on commit b9239e8

Please sign in to comment.