Skip to content

Commit

Permalink
Adapts Stripe with the money protocol
Browse files Browse the repository at this point in the history
* Fixes #5 and #109
* Moved the stripe_test to integration.
* Fixed credo issue in money integration test

Changes to MONEI
* Removed unnecessary clauses from MONEI
* Re-formatted source.
  • Loading branch information
oyeb authored and ashish173 committed Apr 22, 2018
1 parent fb4d5a0 commit 921853c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 179 deletions.
35 changes: 14 additions & 21 deletions lib/gringotts/gateways/base.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
defmodule Gringotts.Gateways.Base do
@moduledoc """
Dummy implementation of the Gringotts API
All gateway implementations must `use` this module as it provides (pseudo)
implementations for the all methods of the Gringotts API.
In case `GatewayXYZ` does not implement `unstore`, the following call would
not raise an error:
```
Gringotts.unstore(GatewayXYZ, "some_registration_id")
```
because this module provides an implementation.
"""

alias Gringotts.Response

defmacro __using__(_) do
Expand Down Expand Up @@ -38,27 +52,6 @@ defmodule Gringotts.Gateways.Base do
not_implemented()
end

defp http(method, path, params \\ [], opts \\ []) do
credentials = Keyword.get(opts, :credentials)
headers = [{"Content-Type", "application/x-www-form-urlencoded"}]
data = params_to_string(params)

HTTPoison.request(method, path, data, headers, [hackney: [basic_auth: credentials]])
end

defp money_to_cents(amount) when is_float(amount) do
trunc(amount * 100)
end

defp money_to_cents(amount) do
amount * 100
end

defp params_to_string(params) do
params |> Enum.filter(fn {_k, v} -> v != nil end)
|> URI.encode_query
end

@doc false
defp not_implemented do
{:error, Response.error(code: :not_implemented)}
Expand Down
53 changes: 14 additions & 39 deletions lib/gringotts/gateways/monei.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,11 @@ defmodule Gringotts.Gateways.Monei do
@base_url "https://test.monei-api.net"
@default_headers ["Content-Type": "application/x-www-form-urlencoded", charset: "UTF-8"]

@supported_currencies [
"AED", "AFN", "ANG", "AOA", "AWG", "AZN", "BAM", "BGN", "BRL", "BYN", "CDF",
"CHF", "CUC", "EGP", "EUR", "GBP", "GEL", "GHS", "MDL", "MGA", "MKD", "MWK",
"MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "PAB", "PEN", "PGK", "PHP",
"PKR", "PLN", "PYG", "QAR", "RSD", "RUB", "RWF", "SAR", "SCR", "SDG", "SEK",
"SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SYP", "SZL", "THB", "TJS", "TOP",
"TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VND", "VUV",
"WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"
]
@supported_currencies ~w(AED AFN ANG AOA AWG AZN BAM BGN BRL BYN CDF CHF CUC
EGP EUR GBP GEL GHS MDL MGA MKD MWK MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK
PHP PKR PLN PYG QAR RSD RUB RWF SAR SCR SDG SEK SGD SHP SLL SOS SRD STD SYP
SZL THB TJS TOP TRY TTD TWD TZS UAH UGX USD UYU UZS VND VUV WST XAF XCD XOF
XPF YER ZAR ZMW ZWL)

@version "v1"

Expand Down Expand Up @@ -435,7 +431,6 @@ defmodule Gringotts.Gateways.Monei do
]
end


# Makes the request to MONEI's network.
@spec commit(atom, String.t(), keyword, keyword) :: {:ok | :error, Response.t()}
defp commit(:post, endpoint, params, opts) do
Expand All @@ -447,7 +442,10 @@ defmodule Gringotts.Gateways.Monei do

validated_params ->
url
|> HTTPoison.post({:form, params ++ validated_params ++ auth_params(opts)}, @default_headers)
|> HTTPoison.post(
{:form, params ++ validated_params ++ auth_params(opts)},
@default_headers
)
|> respond
end
end
Expand All @@ -458,7 +456,7 @@ defmodule Gringotts.Gateways.Monei do
auth_params = auth_params(opts)
query_string = auth_params |> URI.encode_query()

base_url <> "?" <> query_string
(base_url <> "?" <> query_string)
|> HTTPoison.delete()
|> respond
end
Expand All @@ -472,7 +470,7 @@ defmodule Gringotts.Gateways.Monei do
common = [raw: body, status_code: 200]

with {:ok, decoded_json} <- decode(body),
{:ok, results} <- parse_response(decoded_json) do
{:ok, results} <- parse_response(decoded_json) do
{:ok, Response.success(common ++ results)}
else
{:not_ok, errors} ->
Expand Down Expand Up @@ -570,38 +568,15 @@ defmodule Gringotts.Gateways.Monei do
currency in @supported_currencies
end

defp parse_response(%{"result" => result} = data) do
{address, zip_code} = @avs_code_translator[result["avsResponse"]]

results = [
code: result["code"],
description: result["description"],
risk: data["risk"]["score"],
cvc_result: @cvc_code_translator[result["cvvResponse"]],
avs_result: [address: address, zip_code: zip_code],
raw: data,
token: data["registrationId"]
]

filtered = Enum.filter(results, fn {_, v} -> v != nil end)
verify(filtered)
end

defp verify(results) do
if String.match?(results[:code], ~r{^(000\.000\.|000\.100\.1|000\.[36])}) do
{:ok, results}
else
{:error, [{:reason, results[:description]} | results]}
end
end

defp make(action_type, _prefix, _param) when action_type in ["CP", "RF", "RV"], do: []

defp make(action_type, prefix, param) do
case prefix do
:register ->
if action_type in ["PA", "DB"], do: [createRegistration: true], else: []

_ -> Enum.into(param, [], fn {k, v} -> {"#{prefix}.#{k}", v} end)
_ ->
Enum.into(param, [], fn {k, v} -> {"#{prefix}.#{k}", v} end)
end
end

Expand Down
Loading

0 comments on commit 921853c

Please sign in to comment.