Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 80 additions & 18 deletions lib/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ defmodule PayPal.API do
@moduledoc """
Documentation for PayPal.API. This module is about the base HTTP functionality
"""
@base_url_sandbox "https://api.sandbox.paypal.com/v1/"
@base_url_live "https://api.paypal.com/v1/"
require Logger
@base_url_sandbox_version_one "https://api.sandbox.paypal.com/v1/"
@base_url_live_version_one "https://api.paypal.com/v1/"
@base_url_sandbox_version_two "https://api.sandbox.paypal.com/v2/"
@base_url_live_version_two "https://api.paypal.com/v2/"

@doc """
Requests an OAuth token from PayPal, returns a tuple containing the token and seconds till expiry.
Expand All @@ -25,16 +28,22 @@ defmodule PayPal.API do
@spec get_oauth_token :: {atom, any}
def get_oauth_token do
headers = %{"Content-Type" => "application/x-www-form-urlencoded"}
options = [hackney: [basic_auth: {PayPal.Config.get.client_id, PayPal.Config.get.client_secret}]]

options = [
hackney: [basic_auth: {PayPal.Config.get().client_id, PayPal.Config.get().client_secret}]
]

form = {:form, [grant_type: "client_credentials"]}

case HTTPoison.post(base_url() <> "oauth2/token", form, headers, options) do
{:ok, %{status_code: 401}} ->
{:error, :unauthorised}

{:ok, %{body: body, status_code: 200}} ->
%{access_token: access_token, expires_in: expires_in} = Poison.decode!(body, keys: :atoms)
{:ok, {access_token, expires_in}}
_->

_ ->
{:error, :bad_network}
end
end
Expand All @@ -58,21 +67,27 @@ defmodule PayPal.API do
{:ok, {"XXXXXXXXXXXXXX", 32000}}

"""
@spec get(String.t) :: {atom, any}
@spec get(String.t()) :: {atom, any}
def get(url) do
case HTTPoison.get(base_url() <> url, headers()) do
{:ok, %{status_code: 401}} ->
{:error, :unauthorised}

{:ok, %{body: body, status_code: 200}} ->
{:ok, Poison.decode!(body, keys: :atoms)}

{:ok, %{status_code: 404}} ->
{:ok, :not_found}

{:ok, %{status_code: 400}} ->
{:ok, :not_found}

{:ok, %{status_code: 204}} ->
{:ok, :no_content}
{:ok, %{body: body}}->

{:ok, %{body: body}} ->
{:error, body}

_ ->
{:error, :bad_network}
end
Expand All @@ -97,25 +112,33 @@ defmodule PayPal.API do
{:ok, {"XXXXXXXXXXXXXX", 32000}}

"""
@spec post(String.t, map) :: {atom, any}
@spec post(String.t(), map) :: {atom, any}
def post(url, data) do
{:ok, data} = Poison.encode(data)

case HTTPoison.post(base_url() <> url, data, headers()) do
{:ok, %{status_code: 401}} ->
{:error, :unauthorised}

{:ok, %{body: body, status_code: 200}} ->
{:ok, Poison.decode!(body, keys: :atoms)}

{:ok, %{body: body, status_code: 201}} ->
{:ok, Poison.decode!(body, keys: :atoms)}

{:ok, %{status_code: 404}} ->
{:ok, :not_found}

{:ok, %{status_code: 204}} ->
{:ok, nil}

{:ok, %{status_code: 400}} ->
{:error, :malformed_request}

{:ok, %{body: body}} = resp ->
IO.inspect resp
IO.inspect(resp)
{:error, body}

_ ->
{:error, :bad_network}
end
Expand All @@ -140,45 +163,84 @@ defmodule PayPal.API do
{:ok, {"XXXXXXXXXXXXXX", 32000}}

"""
@spec patch(String.t, map) :: {atom, any}
@spec patch(String.t(), map) :: {atom, any}
def patch(url, data) do
{:ok, data} = Poison.encode(data)

case HTTPoison.patch(base_url() <> url, data, headers()) do
{:ok, %{status_code: 401}} ->
{:error, :unauthorised}

{:ok, %{status_code: 200}} ->
{:ok, nil}

{:ok, %{body: body, status_code: 201}} ->
{:ok, Poison.decode!(body, keys: :atoms)}

{:ok, %{status_code: 404}} ->
{:ok, :not_found}

{:ok, %{status_code: 204}} ->
{:ok, :no_content}

{:ok, %{status_code: 400}} ->
{:error, :malformed_request}

{:ok, %{body: body}} = resp ->
IO.inspect resp
{:error, body}

_ ->
{:error, :bad_network}
end
end

@spec headers :: map
defp headers do
%{"Authorization" => "Bearer #{Application.get_env(:pay_pal, :access_token)}", "Content-Type" => "application/json"}
%{
"Authorization" => "Bearer #{Application.get_env(:pay_pal, :access_token)}",
"Content-Type" => "application/json"
}
end

@spec base_url :: String.t
@spec base_url :: String.t()
defp base_url do
case Application.get_env(:pay_pal, :environment) do
:sandbox -> @base_url_sandbox
:live -> @base_url_live
:prod -> @base_url_live
:sandbox ->
base_url_sandbox_version

:live ->
base_url_live_version

:prod ->
base_url_live_version

_ ->
Logger.warn("[PayPal] No `env` found in config, use `sandbox` as default.")
base_url_sandbox_version
end
end

defp base_url_sandbox_version do
case Application.get_env(:pay_pal, :version) do
:v2 ->
Logger.info("[PayPal] is using version_2 sandbox url.")
@base_url_sandbox_version_two

_ ->
Logger.info("[PayPal] is using version_1 sandbox url.")
@base_url_sandbox_version_one
end
end

defp base_url_live_version do
case Application.get_env(:pay_pal, :version) do
:v2 ->
Logger.info("[PayPal] is using version_2 live url.")
@base_url_live_version_two

_ ->
require Logger
Logger.warn "[PayPal] No `env` found in config, use `sandbox` as default."
@base_url_sandbox
Logger.info("[PayPal] is using version_1 live url.")
@base_url_live_version_one
end
end
end
16 changes: 12 additions & 4 deletions lib/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ defmodule PayPal.Config do
iex(1)> PayPal.Config.get
%{client_id: "CLIENT_ID", client_secret: "CLIENT_SECRET"}
"""
@spec get :: %{client_id: String.t, client_secret: String.t}
@spec get :: %{client_id: String.t(), client_secret: String.t()}
def get do
case !is_nil(System.get_env("PAYPAL_CLIENT_ID")) && !is_nil(System.get_env("PAYPAL_CLIENT_SECRET")) do
case !is_nil(System.get_env("PAYPAL_CLIENT_ID")) &&
!is_nil(System.get_env("PAYPAL_CLIENT_SECRET")) do
true ->
%{client_id: System.get_env("PAYPAL_CLIENT_ID"), client_secret: System.get_env("PAYPAL_CLIENT_SECRET")}
%{
client_id: System.get_env("PAYPAL_CLIENT_ID"),
client_secret: System.get_env("PAYPAL_CLIENT_SECRET")
}

_ ->
%{client_id: Application.get_env(:pay_pal, :client_id), client_secret: Application.get_env(:pay_pal, :client_secret)}
%{
client_id: Application.get_env(:pay_pal, :client_id),
client_secret: Application.get_env(:pay_pal, :client_secret)
}
end
end
end
8 changes: 4 additions & 4 deletions lib/payments/authorizations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule PayPal.Payments.Authorizations do
iex> PayPal.Payments.Authorizations.show(authorization_id)
{:ok, authorization}
"""
@spec show(String.t) :: {atom, any}
@spec show(String.t()) :: {atom, any}
def show(authorization_id) do
PayPal.API.get("payments/authorization/#{authorization_id}")
end
Expand All @@ -46,7 +46,7 @@ defmodule PayPal.Payments.Authorizations do
})
{:ok, capture}
"""
@spec capture(String.t, map) :: {atom, any}
@spec capture(String.t(), map) :: {atom, any}
def capture(authorization_id, params) do
PayPal.API.post("payments/authorization/#{authorization_id}/capture", params)
end
Expand All @@ -66,7 +66,7 @@ defmodule PayPal.Payments.Authorizations do
iex> PayPal.Payments.Authorizations.void(authorization_id)
{:ok, authorization}
"""
@spec void(String.t) :: {atom, any}
@spec void(String.t()) :: {atom, any}
def void(authorization_id) do
PayPal.API.post("payments/authorization/#{authorization_id}/void", nil)
end
Expand All @@ -91,7 +91,7 @@ defmodule PayPal.Payments.Authorizations do
})
{:ok, authorization}
"""
@spec reauthorize(String.t, map) :: {atom, any}
@spec reauthorize(String.t(), map) :: {atom, any}
def reauthorize(authorization_id, params) do
PayPal.API.post("payments/authorization/#{authorization_id}/reauthorize", params)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/payments/captures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule PayPal.Payments.Captures do
iex> PayPal.Payments.Captures.show(capture_id)
{:ok, capture}
"""
@spec show(String.t) :: {atom, any}
@spec show(String.t()) :: {atom, any}
def show(capture_id) do
PayPal.API.get("payments/capture/#{capture_id}")
end
Expand All @@ -45,7 +45,7 @@ defmodule PayPal.Payments.Captures do
})
{:ok, refund}
"""
@spec refund(String.t, map) :: {atom, any}
@spec refund(String.t(), map) :: {atom, any}
def refund(payment_id, params) do
PayPal.API.post("payments/capture/#{payment_id}/refund", params)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/payments/orders.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule PayPal.Payments.Orders do
iex> PayPal.Payments.Orders.show(order_id)
{:ok, order}
"""
@spec show(String.t) :: {atom, any}
@spec show(String.t()) :: {atom, any}
def show(order_id) do
PayPal.API.get("payments/order/#{order_id}")
end
Expand All @@ -45,7 +45,7 @@ defmodule PayPal.Payments.Orders do
})
{:ok, refund}
"""
@spec authorize(String.t, map) :: {atom, any}
@spec authorize(String.t(), map) :: {atom, any}
def authorize(payment_id, params) do
PayPal.API.post("payments/orders/#{payment_id}/authorize", params)
end
Expand All @@ -71,7 +71,7 @@ defmodule PayPal.Payments.Orders do
})
{:ok, capture}
"""
@spec capture(String.t, map) :: {atom, any}
@spec capture(String.t(), map) :: {atom, any}
def capture(order_id, params) do
PayPal.API.post("payments/orders/#{order_id}/capture", params)
end
Expand All @@ -91,7 +91,7 @@ defmodule PayPal.Payments.Orders do
iex> PayPal.Payments.Orders.void(order_id)
{:ok, order}
"""
@spec void(String.t) :: {atom, any}
@spec void(String.t()) :: {atom, any}
def void(order_id) do
PayPal.API.post("payments/order/#{order_id}/void", nil)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/payments/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ defmodule PayPal.Payments.Payments do
iex> PayPal.Payments.Payments.execute(payment_id, payer_id)
{:ok, payment}
"""
@spec execute(String.t, String.t) :: {atom, any}
@spec execute(String.t(), String.t()) :: {atom, any}
def execute(payment_id, payer_id) do
PayPal.API.post("payments/payment/#{payment_id}/execute", %{payer_id: payer_id})
end
Expand All @@ -200,7 +200,7 @@ defmodule PayPal.Payments.Payments do
iex> PayPal.Payments.Payments.show(payment_id)
{:ok, payment}
"""
@spec show(String.t) :: {atom, any}
@spec show(String.t()) :: {atom, any}
def show(payment_id) do
PayPal.API.get("payments/payment/#{payment_id}")
end
Expand Down Expand Up @@ -247,7 +247,7 @@ defmodule PayPal.Payments.Payments do
{:ok, payment}

"""
@spec update(String.t, list) :: {atom, any}
@spec update(String.t(), list) :: {atom, any}
def update(payment_id, changes) do
PayPal.API.patch("payments/payment/#{payment_id}", changes)
end
Expand All @@ -272,6 +272,7 @@ defmodule PayPal.Payments.Payments do
case PayPal.API.get("payments/payment?#{URI.encode_query(query)}") do
{:ok, %{payments: payments}} ->
{:ok, payments}

error ->
error
end
Expand Down
8 changes: 4 additions & 4 deletions lib/payments/payouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule PayPal.Payments.Payouts do
]
}
"""
@spec get_payouts_batch(binary) :: { :ok, map } | { :error, any}
@spec get_payouts_batch(binary) :: {:ok, map} | {:error, any}
def get_payouts_batch(payout_batch_id) when is_binary(payout_batch_id) do
PayPal.API.get("payments/payouts/#{payout_batch_id}")
end
Expand Down Expand Up @@ -183,7 +183,7 @@ defmodule PayPal.Payments.Payouts do
}
}
"""
@spec get_payout(binary) :: { :ok, map } | { :error, any }
@spec get_payout(binary) :: {:ok, map} | {:error, any}
def get_payout(payout_id) when is_binary(payout_id) do
PayPal.API.get("payments/payouts-item/#{payout_id}")
end
Expand Down Expand Up @@ -245,8 +245,8 @@ defmodule PayPal.Payments.Payouts do
}
}
"""
@spec cancel(binary) :: { :ok, map } | { :error, any}
@spec cancel(binary) :: {:ok, map} | {:error, any}
def cancel(payout_id) when is_binary(payout_id) do
PayPal.API.post("payments/payouts-item/#{payout_id}/cancel", %{})
end
end
end
3 changes: 1 addition & 2 deletions lib/payments/refunds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ defmodule PayPal.Payments.Refunds do
iex> PayPal.Payments.Refunds.show(refund_id)
{:ok, refund}
"""
@spec show(String.t) :: {atom, any}
@spec show(String.t()) :: {atom, any}
def show(refund_id) do
PayPal.API.get("payments/refund/#{refund_id}")
end

end
Loading