View Source Gringotts.Gateways.AuthorizeNet (gringotts v1.1.1)
A module for working with the Authorize.Net payment gateway.
Refer the official Authorize.Net API docs.
The following set of functions for Authorize.Net have been implemented:
Action | Method |
---|---|
Authorize a Credit Card | authorize/3 |
Capture a previously authorized amount | capture/3 |
Charge a Credit Card | purchase/3 |
Refund a transaction | refund/3 |
Void a transaction | void/2 |
Create Customer Profile | store/2 |
Create Customer Payment Profile | store/2 |
Delete Customer Profile | unstore/2 |
Most Gringotts
API calls accept an optional Keyword
list opts
to supply
optional arguments for transactions with the Authorize.Net gateway. The
following keys are supported:
Key |
---|
customer |
invoice |
bill_to |
ship_to |
customer_ip |
order |
lineitems |
ref_id |
tax |
duty |
shipping |
po_number |
customer_type |
customer_profile_id |
profile |
To know more about these keywords check the Request and Response tabs for each API method.
notes
Notes
- Though Authorize.Net supports multiple currencies however, multiple currencies in one account is not supported. A merchant would need multiple Authorize.Net accounts, one for each chosen currency. Please refer the section on "Supported acquirers and currencies" here.
- You, the merchant needs to be PCI-DSS Compliant if you wish to use this module. Your server will recieve sensitive card and customer information.
- The responses of this module include a non-standard field:
:cavv_result
.:cavv_result
is the "cardholder authentication verification response code". In case of Mastercard transactions, this field will always benil
. Please refer the "Response Format" section in the docs for more details.
configuring-your-authorizenet-account-at-gringotts
Configuring your AuthorizeNet account at Gringotts
To use this module you need to create an account with the
Authorize.Net gateway and obtain your login secrets: name
and
transactionKey
.
Your Application config must include the name
and transaction_key
fields and would look something like this:
config :gringotts, Gringotts.Gateways.AuthorizeNet,
name: "name_provided_by_authorize_net",
transaction_key: "transactionKey_provided_by_authorize_net"
scope-of-this-module
Scope of this module
Although Authorize.Net supports payments from various sources (check your dashboard), this library currently accepts payments by (supported) credit cards only.
following-the-examples
Following the examples
First, set up a sample application and configure it to work with Authorize.Net.
- You could do that from scratch by following our Getting Started guide.
- To save you time, we recommend cloning our example repo
that gives you a pre-configured sample app ready-to-go.
- You could use the same config or update it the with your "secrets" above.
To save a lot of time, create a
.iex.exs
file as shown in this gist to introduce a set of handy bindings and aliases.
We'll be using these bindings in the examples below.
Link to this section Summary
Functions
Authorize a credit card transaction.
Captures a pre-authorized amount
.
Transfers amount
from the customer to the merchant.
Refund amount
for a settled transaction referenced by id
.
Store a customer's profile and optionally associate it with a payment profile.
Remove a customer profile from the payment gateway.
Catches gateway configuration errors.
Voids the referenced payment.
Link to this section Functions
@spec authorize(Gringotts.Money.t(), Gringotts.CreditCard.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Authorize a credit card transaction.
The authorization validates the card
details with the banking network,
places a hold on the transaction amount
in the customer’s issuing bank and
also triggers risk management. Funds are not transferred.
To transfer the funds to merchant's account follow this up with a capture/3
.
Authorize.Net returns a transId
(available in the Response.id
field) which
can be used for:
optional-fields
Optional Fields
opts = [
order: %{invoice_number: String, description: String},
ref_id: String,
lineitems: %{
item_id: String, name: String, description: String,
quantity: Integer, unit_price: Gringotts.Money.t()
},
tax: %{amount: Gringotts.Money.t(), name: String, description: String},
duty: %{amount: Gringotts.Money.t(), name: String, description: String},
shipping: %{amount: Gringotts.Money.t(), name: String, description: String},
po_number: String,
customer: %{id: String},
bill_to: %{
first_name: String, last_name: String, company: String,
address: String, city: String, state: String, zip: String,
country: String
},
ship_to: %{
first_name: String, last_name: String, company: String, address: String,
city: String, state: String, zip: String, country: String
},
customer_ip: String
]
example
Example
iex> amount = Money.new(20, :USD)
iex> opts = [
ref_id: "123456",
order: %{invoice_number: "INV-12345", description: "Product Description"},
lineitems: %{item_id: "1", name: "vase", description: "Cannes logo", quantity: 1, unit_price: amount},
tax: %{name: "VAT", amount: Money.new("0.1", :EUR), description: "Value Added Tax"},
shipping: %{name: "SAME-DAY-DELIVERY", amount: Money.new("0.56", :EUR), description: "Zen Logistics"},
duty: %{name: "import_duty", amount: Money.new("0.25", :EUR), description: "Upon import of goods"}
]
iex> card = %CreditCard{number: "5424000000000015", year: 2099, month: 12, verification_code: "999"}
iex> result = Gringotts.authorize(Gringotts.Gateways.AuthorizeNet, amount, card, opts)
@spec capture(String.t(), Gringotts.Money.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Captures a pre-authorized amount
.
amount
is transferred to the merchant account by Authorize.Net when it is smaller or
equal to the amount used in the pre-authorization referenced by id
.
Authorize.Net returns a transId
(available in the Response.id
field) which
can be used to:
notes
Notes
- Authorize.Net automatically settles authorized transactions after 24
hours. Hence, unnecessary authorizations must be
void/2
ed within this period! - Though Authorize.Net supports partial capture of the authorized
amount
, it is advised not to do so.
optional-fields
Optional Fields
opts = [
order: %{invoice_number: String, description: String},
ref_id: String
]
example
Example
iex> opts = [
ref_id: "123456"
]
iex> amount = Money.new(20, :USD)
iex> id = "123456"
iex> result = Gringotts.capture(Gringotts.Gateways.AuthorizeNet, id, amount, opts)
@spec purchase(Gringotts.Money.t(), Gringotts.CreditCard.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Transfers amount
from the customer to the merchant.
Charges a credit card
for the specified amount
. It performs authorize
and capture
at the same time.
Authorize.Net returns transId
(available in the Response.id
field) which
can be used to:
optional-fields
Optional Fields
opts = [
order: %{invoice_number: String, description: String},
ref_id: String,
lineitems: %{
item_id: String, name: String, description: String,
quantity: Integer, unit_price: Gringotts.Money.t()
},
tax: %{amount: Gringotts.Money.t(), name: String, description: String},
duty: %{amount: Gringotts.Money.t(), name: String, description: String},
shipping: %{amount: Gringotts.Money.t(), name: String, description: String},
po_number: String,
customer: %{id: String},
bill_to: %{
first_name: String, last_name: String, company: String,
address: String, city: String, state: String, zip: String,
country: String
},
ship_to: %{
first_name: String, last_name: String, company: String, address: String,
city: String, state: String, zip: String, country: String
},
customer_ip: String
]
example
Example
iex> amount = Money.new(20, :USD)
iex> opts = [
ref_id: "123456",
order: %{invoice_number: "INV-12345", description: "Product Description"},
lineitems: %{item_id: "1", name: "vase", description: "Cannes logo", quantity: 1, unit_price: amount},
tax: %{name: "VAT", amount: Money.new("0.1", :EUR), description: "Value Added Tax"},
shipping: %{name: "SAME-DAY-DELIVERY", amount: Money.new("0.56", :EUR), description: "Zen Logistics"},
duty: %{name: "import_duty", amount: Money.new("0.25", :EUR), description: "Upon import of goods"}
]
iex> card = %CreditCard{number: "5424000000000015", year: 2099, month: 12, verification_code: "999"}
iex> result = Gringotts.purchase(Gringotts.Gateways.AuthorizeNet, amount, card, opts)
@spec refund(Gringotts.Money.t(), String.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Refund amount
for a settled transaction referenced by id
.
The payment
field in the opts
is used to set the instrument/mode of
payment, which could be different from the original one. Currently, we
support only refunds to cards, so put the card
details in the payment
.
required-fields
Required fields
opts = [
payment: %{card: %{number: String, year: Integer, month: Integer}}
]
optional-fields
Optional fields
opts = [ref_id: String]
example
Example
iex> opts = [
payment: %{card: %{number: "5424000000000015", year: 2099, month: 12}}
ref_id: "123456"
]
iex> id = "123456"
iex> amount = Money.new(20, :USD)
iex> result = Gringotts.refund(Gringotts.Gateways.AuthorizeNet, amount, id, opts)
@spec store(Gringotts.CreditCard.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Store a customer's profile and optionally associate it with a payment profile.
Authorize.Net separates a customer's profile from their payment profile. Thus a customer can have multiple payment profiles.
create-both-profiles
Create both profiles
Add :customer
details in opts
and also provide card
details. The response
will contain a :customer_profile_id
.
associate-payment-profile-with-existing-customer-profile
Associate payment profile with existing customer profile
Simply pass the :customer_profile_id
in the opts
. This will add the card
details to the profile referenced by the supplied :customer_profile_id
.
notes
Notes
- Currently only supports
credit card
in the payment profile. - The supplied
card
details can be validated by supplying a:validation_mode
, available options aretestMode
andliveMode
, the deafult istestMode
.
required-fields
Required Fields
opts = [
profile: %{merchant_customer_id: String, description: String, email: String}
]
optional-fields
Optional Fields
opts = [
validation_mode: String,
bill_to: %{
first_name: String, last_name: String, company: String, address: String,
city: String, state: String, zip: String, country: String
},
customer_type: String,
customer_profile_id: String
]
example
Example
iex> opts = [
profile: %{merchant_customer_id: 123456, description: "test store", email: "test@gmail.com"},
validation_mode: "testMode"
]
iex> card = %CreditCard{number: "5424000000000015", year: 2099, month: 12, verification_code: "999"}
iex> result = Gringotts.store(Gringotts.Gateways.AuthorizeNet, card, opts)
@spec unstore(String.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Remove a customer profile from the payment gateway.
Use this function to unstore the customer card information by deleting the customer profile present. Requires the customer profile id.
example
Example
iex> id = "123456"
iex> result = Gringotts.store(Gringotts.Gateways.AuthorizeNet, id)
Catches gateway configuration errors.
Raises a run-time ArgumentError
if any of the required_config
values
is not available or missing from the Application config.
@spec void(String.t(), Keyword.t()) :: {:ok | :error, Gringotts.Response.t()}
Voids the referenced payment.
This method attempts a reversal of the either a previous purchase/3
or
authorize/3
referenced by id
.
It can cancel either an original transaction that may not be settled or an entire order composed of more than one transaction.
optional-fields
Optional fields
opts = [ref_id: String]
example
Example
iex> opts = [
ref_id: "123456"
]
iex> id = "123456"
iex> result = Gringotts.void(Gringotts.Gateways.AuthorizeNet, id, opts)