gringotts v1.0.2 Gringotts.Gateways.AuthorizeNet

A module for working with the Authorize.net payment gateway.

The module provides a set of functions to perform transactions via this gateway for a merchant.

AuthorizeNet API reference

The following set of functions for Authorize.Net have been provided:

ActionMethod
Authorize a Credit Cardauthorize/3
Capture a Previously Authorized Amountcapture/3
Charge a Credit Cardpurchase/3
Refund a transactionrefund/3
Void a Transactionvoid/2
Create Customer Profilestore/2
Create Customer Payment Profilestore/2
Delete Customer Profileunstore/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:

KeyRemarkStatus
currencynot Implemented
customerimplemented
invoiceimplemented
bill_toimplemented
ship_toimplemented
customer_ipimplemented
orderimplemented
lineitemsimplemented
ref_idimplemented
taximplemented
dutyimplemented
shippingimplemented
po_numberimplemented
customer_typeimplemented
customer_profile_idimplemented
profileimplemented

To know more about these keywords visit Request and Response key sections for each function.

To use this module you need to create an account with the Authorize.Net gateway which will provide you with a name and a transactionKey.

Configuring your AuthorizeNet account at Gringotts

Your Application config must include the name and transaction_key fields and would look something like this:

config :gringotts, Gringotts.Gateways.AuthorizeNet,
  adapter: Gringotts.Gateways.AuthorizeNet,
  name: "name_provided_by_authorize_net",
  transaction_key: "transactionKey_provided_by_authorize_net"

Scope of this module, and quirks

  • Although Authorize.Net supports payments from various sources, this library currently accepts payments by (supported) credit cards only.

Following the examples

  1. 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.
  2. Run an iex session with iex -S mix and add some variable bindings and aliases to it (to save some time):
iex> alias Gringotts.{Response, CreditCard, Gateways.AuthorizeNet}

Link to this section Summary

Functions

Authorize a credit card transaction

Capture a transaction

Charge a credit card

Refund amount for a settled transaction referenced by id

Store a customer payment profile

Remove a customer profile from the payment gateway

Validates the config dynamically depending on what is the value of required_config

To void a transaction

Link to this section Functions

Link to this function authorize(amount, payment, opts)
authorize(Float, Gringotts.CreditCard.t(), Keyword.t()) :: tuple()

Authorize a credit card transaction.

Function to authorize a transaction for the specified amount. It needs to be followed up with a capture/3 transaction to transfer the funds to merchant account.

For this transaction Authorize.Net returns a transId which can be use for:

Optional Fields

opts = [
  order: %{invoice_number: String, description: String},
  ref_id: String,
  lineitems: %{
    item_id: String, name: String, description: String,
    quantity: Integer, unit_price: Float
  },
  tax: %{amount: Float, name: String, description: String},
  duty: %{amount: String, name: String, description: String},
  shipping: %{amount: String, 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

iex> opts = [
  ref_id: "123456",
  order: %{invoice_number: "INV-12345", description: "Product Description"}, 
  lineitems: %{itemId: "1", name: "vase", description: "Cannes logo", quantity: "18", unit_price: "45.00"}
]
iex> card = %CreditCard{number: "5424000000000015", year: 2020, month: 12, verification_code: "999"}
iex> amount = 5
iex> result = Gringotts.authorize(Gringotts.Gateways.AuthorizeNet, amount, card, opts)
Link to this function capture(id, amount, opts)
capture(String.t(), Float, Keyword.t()) :: tuple()

Capture a transaction.

Function to capture an amount for an authorized transaction.

For this transaction Authorize.Net returns a transId which can be use to:

Quirks

  • If a capture transaction needs to void then it should be done before it is settled. For AuthorieNet all the transactions are settled after 24 hours.

  • AuthorizeNet supports partical capture of the authorized amount. But it is advisable to use one authorization code only once.

Optional Fields

opts = [
  order: %{invoice_number: String, description: String},
  ref_id: String
]

Example

iex> opts = [
  ref_id: "123456"
]
iex> amount = 5
iex> id = "123456"
iex> result = Gringotts.capture(Gringotts.Gateways.AuthorizeNet, id, amount, opts)
Link to this function purchase(amount, payment, opts)
purchase(Float, Gringotts.CreditCard.t(), Keyword.t()) :: tuple()

Charge a credit card.

Function to charge a user credit card for the specified amount. It performs authorize and capture at the same time. For this transaction Authorize.Net returns transId which can be used to:

Optional Fields

opts = [
  order: %{invoice_number: String, description: String},
  ref_id: String,
  lineitems: %{
    item_id: String, name: String, description: String,
    quantity: Integer, unit_price: Float
  },
  tax: %{amount: Float, name: String, description: String},
  duty: %{amount: String, name: String, description: String},
  shipping: %{amount: String, 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

iex> opts = [
  ref_id: "123456",
  order: %{invoice_number: "INV-12345", description: "Product Description"}, 
  lineitems: %{itemId: "1", name: "vase", description: "Cannes logo", quantity: "18", unit_price: "45.00"}
]
iex> card = %CreditCard{number: "5424000000000015", year: 2020, month: 12, verification_code: "999"}
iex> amount = 5
iex> result = Gringotts.purchase(Gringotts.Gateways.AuthorizeNet, amount, card, opts)
Link to this function refund(amount, id, opts)
refund(Float, String.t(), Keyword.t()) :: tuple()

Refund amount for a settled transaction referenced by id.

Use this method to refund a customer for a transaction that was already settled, requires transId of the transaction. The payment field in the opts is used to set the mode of payment. The card field inside payment needs the information of the credit card to be passed in the specified fields so as to refund to that particular card.

Required fields

opts = [
  payment: %{card: %{number: String, year: Integer, month: Integer}}
]

Optional fields

opts = [ref_id: String]

Example

iex> opts = [
  payment: %{card: %{number: "5424000000000015", year: 2020, month: 12}}
  ref_id: "123456"
]
iex> id = "123456"
iex> amount = 5
iex> result = Gringotts.refund(Gringotts.Gateways.AuthorizeNet, amount, id, opts)
Link to this function store(card, opts)
store(Gringotts.CreditCard.t(), Keyword.t()) :: tuple()

Store a customer payment profile.

Use this function to store the customer card information by creating a customer profile which also creates a payment profile if card inofrmation is provided, and in case the customer profile exists without a payment profile, the merchant can create customer payment profile by passing the customer_profile_id in the opts. The gateway also provide a provision for a validation mode, there are two modes liveMode and testMode, to know more about modes see.

Quirks

  • The current version of this library supports only credit card as the payment profile.
  • If a customer profile is created without the card info, then to create a payment profile card info needs to be passed alongwith cutomer_profile_id to create it.

Required Fields

opts = [
  profile: %{merchant_customer_id: String, description: String, email: String}
]

Optional Fields

opts = [
  validation_mode: String,
  billTo: %{
    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

iex> opts = [
  profile: %{merchant_customer_id: 123456, description: "test store", email: "test@gmail.com"},
  validation_mode: "testMode"
]
iex> card = %CreditCard{number: "5424000000000015", year: 2020, month: 12, verification_code: "999"}
iex> result = Gringotts.store(Gringotts.Gateways.AuthorizeNet, card, opts)
Link to this function unstore(customer_profile_id, opts)
unstore(String.t(), Keyword.t()) :: tuple()

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

iex> id = "123456"
iex> opts = []
iex> result = Gringotts.store(Gringotts.Gateways.AuthorizeNet, id, opts)
Link to this function validate_config(config)

Validates the config dynamically depending on what is the value of required_config

Link to this function void(id, opts)
void(String.t(), Keyword.t()) :: tuple()

To void a transaction

Use this method to cancel either an original transaction that is not settled or an entire order composed of more than one transaction. It can be submitted against ‘purchase’, authorize and capture. Requires the transId of a transaction.

Optional fields

opts = [ref_id: String]

Example

iex> opts = [
  ref_id: "123456"
]
iex> id = "123456"
iex> result = Gringotts.void(Gringotts.Gateways.AuthorizeNet, id, opts)