Skip to content

Commit

Permalink
Merge branch 'master' of github.com:aviabird/gringotts
Browse files Browse the repository at this point in the history
  • Loading branch information
pkrawat1 committed Dec 23, 2017
2 parents 4f06ca2 + 35bce54 commit 6b2d76f
Show file tree
Hide file tree
Showing 15 changed files with 964 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before_install:
- mix local.rebar --force
- mix deps.get
script:
- mix coveralls.travis --trace
- mix coveralls.travis --include integration
after_script:
- MIX_ENV=docs mix deps.get
- MIX_ENV=docs mix inch.report
25 changes: 21 additions & 4 deletions lib/gringotts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@ defmodule Gringotts do
@moduledoc ~S"""
Gringotts is a payment gateway integration library supporting many gateway integrations.
Where the configuration for `Gringotts` must be in your application
environment, usually defined in your `config/config.exs`:
## Configuration
The configuration for `Gringotts` must be in your application environment,
usually defined in your `config/config.exs` and are **mandatory**:
**Global Configuration**
The global configuration sets the library level configurations to interact with the gateway.
If the mode is not set then by 'default' the sandbox account is selected.
To integrate with the sandbox account set.
config :gringotts, :global_config,
mode: :test
To integrate with the live account set.
config :gringotts, :global_config,
mode: :prod
**Gateway Configuration**
The gateway level configurations are for fields related to a specific gateway.
config :Gringotts, Gringotts.Gateways.Stripe,
adapter: Gringotts.Gateways.Stripe,
api_key: "sk_test_vIX41hC0sdfBKrPWQerLuOMld",
Expand Down Expand Up @@ -268,7 +285,7 @@ defmodule Gringotts do
# time error reporting.
defp validate_config(gateway) do
# Keep the key name and adapter the same in the config in application
config = Application.get_env(:Gringotts, gateway)
config = Application.get_env(:gringotts, gateway)
gateway.validate_config(config)
end
end
2 changes: 1 addition & 1 deletion lib/gringotts/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Gringotts.Application do

def start(_type, _args) do
import Supervisor.Spec, warn: false
app_config = Application.get_all_env(:Gringotts)
app_config = Application.get_all_env(:gringotts)
adapters = Enum.filter(app_config, fn({key, klist}) -> klist != [] end)
|> Enum.map(fn({key, klist}) -> Keyword.get(klist, :adapter) end)

Expand Down
41 changes: 25 additions & 16 deletions lib/gringotts/gateways/authorize_net.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
@spec purchase(Float, CreditCard, Keyword) :: Tuple
def purchase(amount, payment, opts) do
request_data = add_auth_purchase(amount, payment, opts, @transaction_type[:purchase])
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
@spec authorize(Float, CreditCard, Keyword) :: Tuple
def authorize(amount, payment, opts) do
request_data = add_auth_purchase(amount, payment, opts, @transaction_type[:authorize])
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand All @@ -156,7 +156,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
@spec capture(String.t, Float, Keyword) :: Tuple
def capture(id, amount, opts) do
request_data = normal_capture(amount, id, opts, @transaction_type[:capture])
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand All @@ -181,7 +181,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
@spec refund(Float, String.t, Keyword) :: Tuple
def refund(amount, id, opts) do
request_data = normal_refund(amount, id, opts, @transaction_type[:refund])
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand All @@ -205,7 +205,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
@spec void(String.t, Keyword) :: Tuple
def void(id, opts) do
request_data = normal_void(id, opts, @transaction_type[:void])
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand Down Expand Up @@ -239,7 +239,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
opts[:customer_profile_id] -> create_customer_payment_profile(card, opts) |> generate
true -> create_customer_profile(card, opts) |> generate
end
response_data = commit(:post, request_data)
response_data = commit(:post, request_data, opts)
respond(response_data)
end

Expand All @@ -256,14 +256,14 @@ defmodule Gringotts.Gateways.AuthorizeNet do
"""
@spec unstore(String.t, Keyword) :: Tuple
def unstore(customer_profile_id, opts) do
request_data = delete_customer_profile(customer_profile_id, opts)
response_data = commit(:post, request_data)
request_data = delete_customer_profile(customer_profile_id, opts) |> generate
response_data = commit(:post, request_data, opts)
respond(response_data)
end

# method to make the api request with params
defp commit(method, payload) do
path = @test_url
defp commit(method, payload, opts) do
path = base_url(opts)
headers = @header
HTTPoison.request(method, path, payload, headers)
end
Expand All @@ -279,9 +279,9 @@ defmodule Gringotts.Gateways.AuthorizeNet do
raw_response[@response_type[:error_response]] ->
response_check(raw_response[@response_type[:error_response]], raw_response)
raw_response[@response_type[:customer_profile_response]] ->
response_check(@response_type[:customer_profile_response], raw_response)
response_check(raw_response[@response_type[:customer_profile_response]], raw_response)
raw_response[@response_type[:customer_payment_profile_response]] ->
response_check(@response_type[:customer_payment_profile_response], raw_response)
response_check(raw_response[@response_type[:customer_payment_profile_response]], raw_response)
raw_response[@response_type[:delete_customer_profile]] ->
response_check(raw_response[@response_type[:delete_customer_profile]], raw_response)
end
Expand All @@ -302,7 +302,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
{:error, Response.error(raw: raw_response)}
end

#------------------- Helper functions for the interface functions-------------------
#------------------- Helper functions for the interface functions-------------------

# function for formatting the request as an xml for purchase and authorize method
defp add_auth_purchase(amount, payment, opts, transaction_type) do
Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do

#function to format the request for normal refund
defp normal_refund(amount, id, opts, transaction_type) do
element(:authenticateTestRequest, %{xmlns: @aut_net_namespace}, [
element(:createTransactionRequest, %{xmlns: @aut_net_namespace}, [
add_merchant_auth(opts[:config]),
add_order_id(opts),
add_refund_transaction_request(amount, id, opts, transaction_type),
Expand All @@ -344,7 +344,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do

#function to format the request for normal void operation
defp normal_void(id, opts, transaction_type) do
element(:authenticateTestRequest, %{xmlns: @aut_net_namespace}, [
element(:createTransactionRequest, %{xmlns: @aut_net_namespace}, [
add_merchant_auth(opts[:config]),
add_order_id(opts),
element(:transactionRequest, [
Expand Down Expand Up @@ -375,7 +375,7 @@ defmodule Gringotts.Gateways.AuthorizeNet do
element(:description, opts[:profile][:description]),
element(:email, opts[:profile][:description]),
element(:paymentProfiles, [
element(:customerType, opts[:customerType]),
element(:customerType, (if opts[:customerType], do: opts[:customerType], else: "individual")),
add_payment_source(card)
])
])
Expand Down Expand Up @@ -565,4 +565,13 @@ defmodule Gringotts.Gateways.AuthorizeNet do
defp join_string(list, symbol) do
Enum.join(list, symbol)
end

defp base_url(opts) do
cond do
opts[:config][:mode] == :prod -> @production_url
opts[:config][:mode] == :test -> @test_url
true -> @test_url
end
end

end
Loading

0 comments on commit 6b2d76f

Please sign in to comment.