Skip to content
This repository

A quick overview of adding a new payment gateway to ActiveMerchant

Introduction

This is a brief guide to contributing new payment gateways to Active Merchant. This guide includes instructions on checking out the source code, developing the new gateway, and finally contributing the new gateway back to the project.

Developing the Gateway

Following is information on coding the new payment gateway. Also included are some of the common patterns and data structures used by gateways in Active Merchant.

Please note that while some payment gateways use SOAP as the format for communication, we do not accept any gateways that have a requirement on soap4r. We find that the overhead required in using the soap4r library is greater than just creating the SOAP document by hand using Builder.

It should also be noted that when there is a choice between the implementation of a Name-Value pair API and an XML API that the Name-Value pair API code will usually be faster to develop, more elegant, and smaller in size than equivalent implementation of the XML API.

Getting Started

Download the Source


git clone git://github.com/Shopify/active_merchant.git
cd ./active_merchant
bundle install

Generating the Gateway Skeleton

Then, from the root directory of Active Merchant, run (replace CardStream with the name of your gateway):


> script/generate gateway card_stream
Writing file lib/active_merchant/billing/gateways/card_stream.rb
Writing file test/unit/gateways/card_stream_test.rb
Writing file test/remote_tests/remote_card_stream_test.rb

At this point it is a good idea to look at other gateways in lib/active_merchant/billing/gateways/ and the remote tests in test/remote_tests for examples.

The first step is usually getting a successful `purchase()` with the minimum number of required parameters. Most gateways support some or all of the following operations:

  • purchase(money, creditcard, options = {})
  • authorize(money, creditcard, options = {})
  • capture(money, identification, options = {})
  • void(identification, options = {})
  • credit(money, identification, options = {})

money is an Integer value in cents. The currency of the transaction is passed in using the :currency option in the options hash.

Base Gateway Class

The base Gateway class provides a lot of helpful methods that will save you effort when implementing your gateway.

Setting the Money Format

There are two different styles for formatting amounts in use:

  • :dollars – The amount is formatted as a float dollar amount with two decimal places (Default)
  • :cents – The amount is formatted as an integer value in cents

You set the money format using the class accessor money_format. For example:

class ProtxGateway < Gateway
  self.money_format = :cents
end

Setting the money format allows you to use the amount method defined on the base gateway class. Simply pass in the amount as an Integer value in cents and the amount will be formatted appropriately. Please note that passing a Money object is deprecated and will be removed from a future release.

Setting the Default Currency

You need to set the default currency of the gateway if the gateway supports multiple currencies. This is done by using the class accessor default_currency. For example:

class ProtxGateway < Gateway
  self.default_currency = 'GBP'
end

Setting the Gateway’s Metadata

Each gateway contains metadata, which are available as class accessors. The metadata is needed for generating documentation, and for intelligent display within applications such as Shopify.

  • Gateway.supported_countries – sets the countries of merchants the gateway supports.
  • Gateway.supported_cardtypes – sets the card types supported by the gateway.
  • Gateway.homepage_url – sets the homepage URL of the payment gateway.
  • Gateway.display_name – sets the name of the gateway for display purposes, such as generating documentation.
  • Gateway.application_id – This is the application making calls to the gateway. This is useful for things like the Paypal build notation (BN) id fields.

Credit Card Types

  • :visa – Visa
  • :master – MasterCard
  • :discover – Discover Card
  • :american_express – American Express
  • :diners_club – Diners Club
  • :jcbJCB
  • :switch – UK Maestro, formerly Switch
  • :solo – Solo
  • :dankort – Dankort
  • :maestro – International Maestro
  • :forbrugsforeningen – Forbrugsforeningen
  • :laser – Laser

The Visa Electron, and Visa Delta cards are not in the list because payment gateways accept the cards as the :visa card type.

Example usage:

class AuthorizeNetGateway < Gateway
  self.supported_countries = ['US']
  self.supported_cardtypes = [:visa, :master, :american_express, :discover]
  self.homepage_url = 'http://www.authorize.net/'
  self.display_name = 'Authorize.net'
end

Options Hash

You shouldn’t be inventing new options for the options hash that gets passed into the public methods. Try to look at the other gateways for the hash keys they use as options and copy those. This allows the easy substitution of one gateway for another.

The options hash can include the following keys:

  • :order_id
  • :ip
  • :customer
  • :invoice
  • :merchant
  • :description
  • :email
  • :currency

The reason that the option has a :currency option is because we are phasing out support for the Money object. This means that in the future the money amount will always be an Integer in cents, and the currency will be set by the :currency option.

The current pattern to use when setting the currency is:

options[:currency] || currency(money)

The currency() method is defined in the base Gateway class, and will use the currency of the money, if it has one, or return the default currency for the gateway.

There are 3 different addresses you can use. There are :billing_address, :shipping_address, or you can just pass in :address
and it will be used for both.

  • :billing_address
  • :shipping_address
  • :address

This is the common pattern to use for the address:

billing_address = options[:billing_address] || options[:address]
shipping_address = options[:shipping_address]

Address Hash

The address is a hash with the following keys:

  • :name
  • :company
  • :address1
  • :address2
  • :city
  • :state
  • :country
  • :zip
  • :phone

Example

A complete options hash might be:

options = {
   :order_id => '1',
   :ip => '10.0.0.1',
   :customer => 'Cody Fauser',
   :description => '200 Web 2.0 M&Ms',
   :email => 'codyfauser@gmail.com',
   :address => {
     :name => 'Cody Fauser',
     ..
     ..
     :zip => '90210'
   }
}

Note that the Address-Hash does not include the email-address – you have to supply that with the normal Options-Hash!

Testing

There are two types of unit tests for each gateway. The first are the normal unit tests, which test the normal functionality of the gateway, and use Mocha to stub out any communications with live servers.

The second type are the remote unit tests. These use real test accounts, if available, and communicate with the test servers of the payments gateway. These are critical to having confidence in the implementation of the gateway. If the gateway doesn’t have a global public test account then you should remove your private test account credentials from the file before submitting your patch. We highly discourage submitting patches that don’t include these remote tests.

To run tests :


bundle install
bundle exec rake test:units   #Should all be passing
bundle exec rake test:remote  #Will not pass without updating test/fixtures.yml with credentials

To run a test individually :

bundle exec rake test:units TEST=test/unit/gateways/nab_transact_test.rb
bundle exec rake test:remote TEST=test/remote/gateways/remote_nab_transact_test.rb

Contributing Your Gateway

Now that your gateway is coded and tested you need to submit it back to us for inclusion in the project.

Creating the Pull Request

Once you’ve finished the development of your gateway you need to create a pull request and contribute it back for permanent addition to the project.

First create a branch for your changes with Git:


git checkout -b add_new_gateway

Next add any of the related files by doing:


git add [filename]

Then commit the changes to your local cloned repository with:


git commit -m "Your short but descriptive patch information"

Once all new files are added to Git you can push the branch to github:


git push origin add_new_gateway

This will push the branch with your changes to github.

Then you just have to log into github, switch to your branch and click the Pull Request button. This will let us know about your changes so that we can apply your code to the project. For more information on pull requests with github check out the help page on the subject.

Something went wrong with that request. Please try again.