Convert Figma logo to code with AI

activemerchant logoactive_merchant

Active Merchant is a simple payment abstraction library extracted from Shopify. The aim of the project is to feel natural to Ruby users and to abstract as many parts as possible away from the user to offer a consistent interface across all supported gateways.

4,597
2,487
4,597
6

Top Related Projects

ShopifyAPI is a lightweight gem for accessing the Shopify admin REST and GraphQL web services.

Ruby library for the Stripe API.

Quick Overview

Active Merchant is a Ruby library for processing credit card transactions. It provides a consistent interface to numerous payment gateways, simplifying the integration of payment processing into Ruby applications. The library supports a wide range of payment providers and offers a unified API for handling various payment-related operations.

Pros

  • Supports a large number of payment gateways, providing flexibility in choosing payment providers
  • Offers a consistent API across different gateways, simplifying integration and maintenance
  • Actively maintained with regular updates and improvements
  • Extensive documentation and community support

Cons

  • Primarily focused on Ruby, which may limit its use in non-Ruby projects
  • Some less common or region-specific payment gateways may have limited support
  • Learning curve can be steep for developers new to payment processing concepts

Code Examples

  1. Creating a new credit card object:
credit_card = ActiveMerchant::Billing::CreditCard.new(
  number: '4111111111111111',
  month: '8',
  year: '2024',
  first_name: 'John',
  last_name: 'Doe',
  verification_value: '123'
)
  1. Initializing a payment gateway:
gateway = ActiveMerchant::Billing::StripeGateway.new(
  login: 'sk_test_123456789'
)
  1. Processing a payment:
response = gateway.purchase(1000, credit_card, {
  description: 'Purchase of goods',
  currency: 'USD'
})

if response.success?
  puts "Payment processed successfully"
else
  puts "Payment failed: #{response.message}"
end

Getting Started

To use Active Merchant in your Ruby project:

  1. Add the gem to your Gemfile:

    gem 'activemerchant'
    
  2. Run bundle install to install the gem.

  3. Require the library in your code:

    require 'active_merchant'
    
  4. Configure the payment mode (test or production):

    ActiveMerchant::Billing::Base.mode = :test # Use :production for live transactions
    
  5. Initialize a gateway and start processing payments using the examples provided above.

Competitor Comparisons

ShopifyAPI is a lightweight gem for accessing the Shopify admin REST and GraphQL web services.

Pros of shopify-api-ruby

  • Specifically designed for Shopify's API, offering more tailored functionality
  • More actively maintained with frequent updates and improvements
  • Better documentation and examples for Shopify-specific use cases

Cons of shopify-api-ruby

  • Limited to Shopify's ecosystem, less versatile for other e-commerce platforms
  • Steeper learning curve for developers not familiar with Shopify's API
  • Potentially more complex setup for simple payment processing tasks

Code Comparison

active_merchant:

require 'active_merchant'

gateway = ActiveMerchant::Billing::StripeGateway.new(
  :login => 'YOUR-SECRET-KEY'
)

response = gateway.purchase(1000, credit_card, :ip => '127.0.0.1')

shopify-api-ruby:

require 'shopify_api'

session = ShopifyAPI::Auth::Session.new(shop: "your-shop.myshopify.com", access_token: "your-access-token")
ShopifyAPI::Context.activate_session(session)

order = ShopifyAPI::Order.new
order.line_items = [{ title: "Test Product", price: 10.00, quantity: 1 }]
order.save

The code examples highlight the difference in focus between the two libraries. active_merchant is more generic and centered around payment processing, while shopify-api-ruby is tailored for interacting with Shopify's broader API, including order management and other e-commerce functionalities.

Ruby library for the Stripe API.

Pros of stripe-ruby

  • Focused specifically on Stripe integration, offering more comprehensive Stripe-specific features
  • Regularly updated with new Stripe API features and improvements
  • Simpler setup and configuration for Stripe-only implementations

Cons of stripe-ruby

  • Limited to Stripe payment processing, lacking support for other payment gateways
  • May require additional libraries for features beyond Stripe's core functionality
  • Less flexibility for multi-gateway implementations compared to Active Merchant

Code Comparison

stripe-ruby:

require 'stripe'
Stripe.api_key = 'sk_test_...'

charge = Stripe::Charge.create({
  amount: 2000,
  currency: 'usd',
  source: 'tok_visa',
  description: 'My First Test Charge (created for API docs)',
})

Active Merchant:

require 'active_merchant'

gateway = ActiveMerchant::Billing::StripeGateway.new(login: 'sk_test_...')

credit_card = ActiveMerchant::Billing::CreditCard.new(
  number: '4242424242424242',
  month: '8',
  year: '2024',
  verification_value: '123'
)

response = gateway.purchase(2000, credit_card)

Both libraries offer Ruby-based solutions for payment processing, but stripe-ruby is tailored specifically for Stripe integration, while Active Merchant provides a more versatile approach supporting multiple payment gateways. The code examples demonstrate the difference in setup and usage between the two libraries.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Active Merchant

Build Status Code Climate

Active Merchant is an extraction from the ecommerce system Shopify. Shopify's requirements for a simple and unified API to access dozens of different payment gateways with very different internal APIs was the chief principle in designing the library.

It was developed for usage in Ruby on Rails web applications and integrates seamlessly as a Rails plugin, but it also works excellently as a stand alone Ruby library.

Active Merchant has been in production use since June 2006 and is now used in most modern Ruby applications which deal with financial transactions. It is maintained by the Shopify and Spreedly teams, with much help from an ever-growing set of contributors.

See GettingStarted.md if you want to learn more about using Active Merchant in your applications.

If you'd like to contribute to Active Merchant, please start with our Contribution Guide.

Installation

From Git

You can check out the latest source from git:

git clone git://github.com/activemerchant/active_merchant.git

From RubyGems

Installation from RubyGems:

gem install activemerchant

Or, if you're using Bundler, just add the following to your Gemfile:

gem 'activemerchant'

Usage

This simple example demonstrates how a purchase can be made using a person's credit card details.

require 'active_merchant'

# Use the TrustCommerce test servers
ActiveMerchant::Billing::Base.mode = :test

gateway = ActiveMerchant::Billing::TrustCommerceGateway.new(
            :login => 'TestMerchant',
            :password => 'password')

# ActiveMerchant accepts all amounts as Integer values in cents
amount = 1000  # $10.00

# The card verification value is also known as CVV2, CVC2, or CID
credit_card = ActiveMerchant::Billing::CreditCard.new(
                :first_name         => 'Bob',
                :last_name          => 'Bobsen',
                :number             => '4242424242424242',
                :month              => '8',
                :year               => Time.now.year+1,
                :verification_value => '000')

# Validating the card automatically detects the card type
if credit_card.validate.empty?
  # Capture $10 from the credit card
  response = gateway.purchase(amount, credit_card)

  if response.success?
    puts "Successfully charged $#{sprintf("%.2f", amount / 100)} to the credit card #{credit_card.display_number}"
  else
    raise StandardError, response.message
  end
end

Contributing

For more in-depth documentation and tutorials, see GettingStarted.md and the API documentation.

Emerging ActiveMerchant 3DS conventions are documented in the Contributing guide and Standardized 3DS Fields guide of the wiki.

Supported Payment Gateways

The ActiveMerchant Wiki contains a table of features supported by each gateway.

API stability policy

Functionality or APIs that are deprecated will be marked as such. Deprecated functionality is removed on major version changes - for example, deprecations from 2.x are removed in 3.x.

Ruby and Rails compatibility policies

Because Active Merchant is a payment library, it needs to take security seriously. For this reason, Active Merchant guarantees compatibility only with actively supported versions of Ruby and Rails. At the time of this writing, that means that Ruby 2.5+ and Rails 5.0+ are supported.