Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Format the code with the 1.6 formatter
Browse files Browse the repository at this point in the history
Successful Full Test Suite Run at 2018-07-16 10:45:03
  • Loading branch information
jknipp committed Aug 24, 2018
1 parent 2e8dbd8 commit 331c166
Show file tree
Hide file tree
Showing 36 changed files with 272 additions and 239 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
line_length: 120
]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ erl_crash.dump
*.ez
/doc
.tool-versions*
/.elixir_ls
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ use Mix.Config
# here (which is why it is important to import them last).
#

import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
64 changes: 36 additions & 28 deletions lib/spreedly.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,37 @@ defmodule Spreedly do

alias Spreedly.Environment

@spec add_gateway(Environment.t, String.t, map()) :: {:ok, any} | {:error, any}
@spec add_gateway(Environment.t(), String.t(), map()) :: {:ok, any} | {:error, any}
def add_gateway(env, gateway_type, gateway_params \\ %{}) do
post_request(env, add_gateway_path(), add_gateway_body(gateway_type, gateway_params))
end

@spec add_receiver(Environment.t, String.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec add_receiver(Environment.t(), String.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def add_receiver(env, receiver_type, options \\ []) do
post_request(env, add_receiver_path(), add_receiver_body(receiver_type, options))
end

@spec add_credit_card(Environment.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec add_credit_card(Environment.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def add_credit_card(env, options) do
post_request(env, add_payment_method_path(), add_credit_card_body(options))
end

@spec retain_payment_method(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec retain_payment_method(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def retain_payment_method(env, token) do
put_request(env, retain_payment_method_path(token))
end

@spec redact_gateway(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec redact_gateway(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def redact_gateway(env, token) do
put_request(env, redact_gateway_method_path(token))
end

@spec redact_payment_method(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec redact_payment_method(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def redact_payment_method(env, token) do
put_request(env, redact_payment_method_path(token))
end

@spec store_payment_method(Environment.t, String.t, String.t) :: {:ok, any} | {:error, any}
@spec store_payment_method(Environment.t(), String.t(), String.t()) :: {:ok, any} | {:error, any}
def store_payment_method(env, gateway_token, payment_method_token) do
post_request(env, store_payment_method_path(gateway_token), store_payment_method_body(payment_method_token))
end
Expand All @@ -91,11 +91,14 @@ defmodule Spreedly do
purchase(env, "gateway_token", "payment_method_token", 100, "USD", [order_id: "44", description: "My purchase"])
"""
@spec purchase(Environment.t, String.t, String.t, pos_integer, String.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec purchase(Environment.t(), String.t(), String.t(), pos_integer, String.t(), Keyword.t()) ::
{:ok, any} | {:error, any}
def purchase(env, gateway_token, payment_method_token, amount, currency_code \\ "USD", options \\ []) do
post_request(env,
purchase_path(gateway_token),
auth_or_purchase_body(payment_method_token, amount, currency_code, options))
post_request(
env,
purchase_path(gateway_token),
auth_or_purchase_body(payment_method_token, amount, currency_code, options)
)
end

@doc """
Expand All @@ -110,24 +113,27 @@ defmodule Spreedly do
authorization(env, "gateway_token", "payment_method_token", 100, "USD", [order_id: "44", description: "My auth"])
"""
@spec authorization(Environment.t, String.t, String.t, pos_integer, String.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec authorization(Environment.t(), String.t(), String.t(), pos_integer, String.t(), Keyword.t()) ::
{:ok, any} | {:error, any}
def authorization(env, gateway_token, payment_method_token, amount, currency_code \\ "USD", options \\ []) do
post_request(env,
authorization_path(gateway_token),
auth_or_purchase_body(payment_method_token, amount, currency_code, options))
post_request(
env,
authorization_path(gateway_token),
auth_or_purchase_body(payment_method_token, amount, currency_code, options)
)
end

@spec capture(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec capture(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def capture(env, transaction_token) do
post_request(env, capture_path(transaction_token))
end

@spec void(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec void(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def void(env, transaction_token) do
post_request(env, void_path(transaction_token))
end

@spec credit(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec credit(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def credit(env, transaction_token) do
post_request(env, credit_path(transaction_token))
end
Expand All @@ -147,32 +153,32 @@ defmodule Spreedly do
verify(env, "gateway_token", "payment_method_token", "USD", [retain_on_success: true])
"""
@spec verify(Environment.t, String.t, String.t, String.t | nil, Keyword.t) :: {:ok, any} | {:error, any}
@spec verify(Environment.t(), String.t(), String.t(), String.t() | nil, Keyword.t()) :: {:ok, any} | {:error, any}
def verify(env, gateway_token, payment_method_token, currency_code \\ nil, options \\ []) do
post_request(env, verify_path(gateway_token), verify_body(payment_method_token, currency_code, options))
end

@spec show_gateway(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec show_gateway(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def show_gateway(env, gateway_token) do
get_request(env, show_gateway_path(gateway_token))
end

@spec show_receiver(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec show_receiver(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def show_receiver(env, receiver_token) do
get_request(env, show_receiver_path(receiver_token))
end

@spec show_payment_method(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec show_payment_method(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def show_payment_method(env, payment_method_token) do
get_request(env, show_payment_method_path(payment_method_token))
end

@spec show_transaction(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec show_transaction(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def show_transaction(env, transaction_token) do
get_request(env, show_transaction_path(transaction_token))
end

@spec show_transcript(Environment.t, String.t) :: {:ok, any} | {:error, any}
@spec show_transcript(Environment.t(), String.t()) :: {:ok, any} | {:error, any}
def show_transcript(env, transaction_token) do
get_request(env, show_transcript_path(transaction_token), [], &transcript_response/1)
end
Expand All @@ -187,7 +193,7 @@ defmodule Spreedly do
list_payment_method_transactions(env, "token", [order: :desc, since_token: "token"])
"""
@spec list_payment_method_transactions(Environment.t, String.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec list_payment_method_transactions(Environment.t(), String.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def list_payment_method_transactions(env, payment_method_token, params \\ []) do
get_request(env, list_payment_method_transactions_path(payment_method_token), params)
end
Expand All @@ -202,7 +208,7 @@ defmodule Spreedly do
list_gateway_transactions(env, "token", order: :desc, since_token: "token"])
"""
@spec list_gateway_transactions(Environment.t, String.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec list_gateway_transactions(Environment.t(), String.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def list_gateway_transactions(env, gateway_token, params \\ []) do
get_request(env, list_gateway_transactions_path(gateway_token), params)
end
Expand Down Expand Up @@ -230,7 +236,7 @@ defmodule Spreedly do
list_transactions(env, order: :desc, since_token: "token", count: 100])
"""
@spec list_transactions(Environment.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec list_transactions(Environment.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def list_transactions(env, params \\ []) do
get_request(env, list_transactions_path(), params)
end
Expand All @@ -245,7 +251,7 @@ defmodule Spreedly do
list_created_gateways(env, [order: :desc, since_token: "token"])
"""
@spec list_created_gateways(Environment.t, Keyword.t) :: {:ok, any} | {:error, any}
@spec list_created_gateways(Environment.t(), Keyword.t()) :: {:ok, any} | {:error, any}
def list_created_gateways(env, params \\ []) do
get_request(env, list_created_gateways_path(), params)
end
Expand All @@ -259,8 +265,10 @@ defmodule Spreedly do
end

defp transcript_response({:error, %HTTPoison.Error{reason: reason}}), do: {:error, reason}

defp transcript_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}) do
{:ok, body}
end

defp transcript_response({:ok, %HTTPoison.Response{status_code: _, body: body}}), do: {:error, body}
end
21 changes: 12 additions & 9 deletions lib/spreedly/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ defmodule Spreedly.Base do
alias HTTPoison.{AsyncResponse, Error, Response}
alias Spreedly.Environment

@spec get_request(Environment.t | nil, String.t, Keyword.t, ((any) -> any)) :: {:ok, any} | {:error, any}
@spec get_request(Environment.t() | nil, String.t(), Keyword.t(), (any -> any)) :: {:ok, any} | {:error, any}
def get_request(env, path, params \\ [], response_callback \\ &process_response/1) do
api_request(:get, env, path, "", [params: params], response_callback)
end

@spec post_request(Environment.t, String.t, any) :: {:ok, any} | {:error, any}
@spec post_request(Environment.t(), String.t(), any) :: {:ok, any} | {:error, any}
def post_request(env, path, body \\ "") do
api_request(:post, env, path, body)
end

@spec put_request(Environment.t, String.t, any) :: {:ok, any} | {:error, any}
@spec put_request(Environment.t(), String.t(), any) :: {:ok, any} | {:error, any}
def put_request(env, path, body \\ "") do
api_request(:put, env, path, body)
end

@spec api_request(atom, Environment.t | nil, String.t, any, Keyword.t, ((any) -> any)) :: {:ok, any} | {:error, any}
@spec api_request(atom, Environment.t() | nil, String.t(), any, Keyword.t(), (any -> any)) ::
{:ok, any} | {:error, any}
defp api_request(method, env, path, body, options \\ [], response_callback \\ &process_response/1) do
method
|> request(path, body, headers(env), [{:recv_timeout, receive_timeout()} | options])
Expand All @@ -42,7 +43,7 @@ defmodule Spreedly.Base do
base_url() <> path
end

@spec process_response({:ok, Response.t | AsyncResponse.t} | {:error, Error.t}) :: {:ok, any} | {:error, any}
@spec process_response({:ok, Response.t() | AsyncResponse.t()} | {:error, Error.t()}) :: {:ok, any} | {:error, any}
defp process_response({:ok, %Response{status_code: code, body: body}}) when code in [200, 201, 202] do
ok_response(body)
end
Expand Down Expand Up @@ -76,7 +77,7 @@ defmodule Spreedly.Base do

defp extract_reason(body = ~s[{"errors":] <> _rest) do
parse(body)[:errors]
|> Enum.map_join("\n", &(&1.message))
|> Enum.map_join("\n", & &1.message)
end

defp extract_reason(body) do
Expand All @@ -90,18 +91,20 @@ defmodule Spreedly.Base do
defp map_from(body) do
body
|> parse()
|> Map.values
|> List.first
|> Map.values()
|> List.first()
end

defp parse(body) do
Poison.decode!(body, keys: :atoms)
end

@spec headers(Environment.t) :: headers
@spec headers(Environment.t()) :: headers
defp headers(nil), do: [content_type()]

defp headers(env) do
encoded = Base.encode64("#{env.environment_key}:#{env.access_secret}")

[
{"Authorization", "Basic #{encoded}"},
content_type()
Expand Down
5 changes: 2 additions & 3 deletions lib/spreedly/environment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ defmodule Spreedly.Environment do
defstruct [:environment_key, :access_secret]

@typedoc "The Spreedly environment made of up the environment key and access secret"
@type t :: %__MODULE__{environment_key: String.t, access_secret: String.t}
@type t :: %__MODULE__{environment_key: String.t(), access_secret: String.t()}

@spec new(String.t, String.t) :: t
@spec new(String.t(), String.t()) :: t
def new(environment_key, access_secret) do
%__MODULE__{environment_key: environment_key, access_secret: access_secret}
end

end
1 change: 0 additions & 1 deletion lib/spreedly/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,4 @@ defmodule Spreedly.Path do
def list_supported_gateways_path do
"/gateways_options.json"
end

end
52 changes: 26 additions & 26 deletions lib/spreedly/request_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,43 @@ defmodule Spreedly.RequestBody do

def add_gateway_body(gateway_type, gateway_params) do
%{
gateway: Map.merge(
%{gateway_type: gateway_type},
gateway_params
)
gateway:
Map.merge(
%{gateway_type: gateway_type},
gateway_params
)
}
|> Poison.encode!
|> Poison.encode!()
end

def add_receiver_body(receiver_type, options) do
%{receiver: receiver_details(receiver_type, options)}
|> Poison.encode!
|> Poison.encode!()
end

def add_credit_card_body(options) do
%{
payment_method:
%{
payment_method: %{
email: options[:email],
retained: options[:retained],
data: options[:data],
credit_card: credit_card_fields(options)
}
}
|> Poison.encode!
|> Poison.encode!()
end

def auth_or_purchase_body(payment_method_token, amount, currency_code, options) do
%{
transaction:
%{
payment_method_token: payment_method_token,
amount: amount,
currency_code: currency_code
} |> Map.merge(Map.new(options))
%{
payment_method_token: payment_method_token,
amount: amount,
currency_code: currency_code
}
|> Map.merge(Map.new(options))
}
|> Poison.encode!
|> Poison.encode!()
end

def credit_body(amount, currency_code) do
Expand All @@ -54,29 +55,29 @@ defmodule Spreedly.RequestBody do
def verify_body(payment_method_token, currency_code, options) do
%{
transaction:
%{
payment_method_token: payment_method_token,
currency_code: currency_code,
} |> Map.merge(Map.new(options))
%{
payment_method_token: payment_method_token,
currency_code: currency_code
}
|> Map.merge(Map.new(options))
}
|> Poison.encode!
|> Poison.encode!()
end

def store_payment_method_body(payment_method_token) do
%{
transaction:
%{
transaction: %{
payment_method_token: payment_method_token
}
}
|> Poison.encode!
|> Poison.encode!()
end

defp credit_card_fields(options) do
~w(first_name last_name full_name month year number verification_value
address1 address2 city state zip country phone_number company)a
|> Keyword.new(fn (x) -> {x, options[x]} end)
|> Map.new
|> Keyword.new(fn x -> {x, options[x]} end)
|> Map.new()
end

defp receiver_details(:test, options) do
Expand All @@ -86,5 +87,4 @@ defmodule Spreedly.RequestBody do
defp receiver_details(receiver_type, _options) do
%{receiver_type: receiver_type}
end

end
Loading

0 comments on commit 331c166

Please sign in to comment.