Paulo Daniel Gonzalez edited this page Nov 10, 2018 · 43 revisions


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 Nokogiri

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://
cd ./active_merchant
bundle install

Generating the Gateway Skeleton

All new gateways should use the gateway generator to create their implementation. 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

This will generate an adapter with default implementations for the following methods:

  • purchase(money, creditcard, options = {})
  • authorize(money, creditcard, options = {})
  • capture(money, tx_reference, options = {})
  • void(tx_reference, options = {})
  • refund(money, tx_reference, options = {})

These are the primary transactions that most gateways support (though there are other standard transactions described below, as well).

The first step is usually getting a successful `purchase()` with the minimum number of required parameters. You can use the remote tests (see below) as a test harness.

Other methods

There are also several other transactional methods part of an adapter’s public API that should be implemented, where possible. These include:

  • verify(creditcard, options = {}) which verifies that a credit card is valid. This lets you verify that a payment method is valid before trying to run an actual transaction (which might be done at some later date). Some gateways support a verify-like call directly, but for ones that don’t the adapter should run and void a small-amount auth transaction (which is the default implementation).
  • credit(money, creditcard, options = {}) which is like `refund`, but allows you to add money back to a card without having to reference a previous transaction.
  • store(creditcard, options = {}) adds/copies the payment method information to the gateway. Some gateways offer a vault-like capability where a card can be stored for future use outside any one specific transaction.

There are also a few non-transactional methods part of an adapter’s public API that should always be implemented:

  • scrub(transcript) which, given a text representation of the HTTP request/response to the gateway, replaces any sensitive values like the PAN, CVV, and gateway credentials with `[FILTERED]`.
  • supports_scrubbing? returns true if scrub is implemented.

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

The money value used in all methods is an Integer value in cents (even if the gateway API uses decimal 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
  • :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 = :dollars

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 (which is what all AM public methods accept, independent of the underlying gateway requirements) and the amount will be formatted appropriately.

# if self.money_format = :dollars, will set as 1.01
post[:amount] = amount(101)

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'

Transactions can specify a currency override by using the :currency option in the options hash.

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. Please determine these values from the gateway documentation or by contacting their support directly.

  • Gateway.supported_countries – sets the countries of merchants the gateway supports in ISO 3166-1-alpha-2 code
  • 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 = ''
  self.display_name = ''

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


A complete options hash might be:

options = {
   :order_id => '1',
   :ip => '',
   :customer => 'Cody Fauser',
   :description => '200 Web 2.0 M&Ms',
   :email => '',
   :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!


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.

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 suite 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

To run a specific test case use the `-n` flag:

$ ruby -Itest test/remote/gateways/remote_nab_transact_test.rb -n test_successful_purchase

It is useful to work on remote tests first, both because they’re less complex (no mocking/stubbing) and because you can capture the request/response easily which can then be copied to the unit tests. To capture the actual HTTP request sent and response received, use the `DEBUG_ACTIVE_MERCHANT` environment variable.

$ DEBUG_ACTIVE_MERCHANT=true ruby -Itest test/remote/gateways/remote_nab_transact_test.rb -n test_successful_purchase
<- "POST /test/xmlapi/payment...
<- "<?xml version=\"1.0\" ..."
-> "HTTP/1.1 200 OK\r\n"
-> "Content-Type: text/xml;charset=ISO-8859-1\r\n"
-> "Content-Length: 954\r\n"

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.


All submissions should:

- Include `test/fixtures.yml` if the gateway’s test credentials are shareable (recommended)
- Must have unit tests and functional remote tests
- Remote tests for a gateway should cover all supported transaction methods (auth, purchase, capture, refund, void, verify, store and scrub) and validate critical response formats such as charge amounts
- Your code should support all the Ruby versions and ActiveSupport versions we have enabled on Travis CI
- No new gem dependencies will be accepted.
- XML: use Nokogiri for generating and parsing XML.
- JSON: use `JSON` in the standard library to parse and generate JSON.
- Do not update the CHANGELOG, or the `ActiveMerchant::VERSION` constant.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.