We love open source and we invest in continuous learning. We give back our knowledge to the community.

Sebastián Martínez

Active Merchant Recurring Billing

Comments

Working on a payment system we had to sort some difficulties when making recurring payments using active merchant. Why? Because we needed to use BeanStream gateway and active merchant does not support recurring billing for this gateway yet. So I just added the functionality to it.

You can check out my fork of Active Merchant that supports recurring billing for this merchant on http://github.com/wyeworks/active_merchant.

Here I leave you an example on how to use it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'rubygems'
require 'active_merchant'

 ActiveMerchant::Billing::Base.mode = :test

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

  # 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               => '2012',
                  :verification_value => '123'
                )

  # Validating the card automatically detects the card type
  if credit_card.valid?

    # Create a gateway object for the BeansTream service
    gateway = ActiveMerchant::Billing::BeanstreamGateway.new(
                :login => 'TestMerchant',
                :password => 'password',
                :pass_code => 'pass_code'
              )

    # Make recurring payment for the amount
    response = gateway.recurring(amount, credit_card, {
      :recurring_billing => {
         :end_of_month => '1',
         :tax1 => 0,
         :interval => {
            :unit => :months,
            :length => '1'
          },
          :duration => {
            :start_date => Date.today,
            :occurrences => 5
          }
        })

    if response.success?
      puts "Successfully charged $#{sprintf("%.2f", amount / 100)} to the credit card #{credit_card.display_number}. The Account number is #{response.params['rbAccountId']}"
    else
      raise StandardError, response.message
    end
  end

And that’s it, you have now created a recurring billing payment.

Now, what if you want to cancel the same? Heres is how to do that:

1
 gateway.cancel_recurring({ :account_id => account_id })

Pretty easy…the account_id is the number the gateway responses with when making a recurrent payment, and the passcode is the way of authenticating in order to make this kind of transactions, and you can find it at the beanstream administrative console.

So try it out and have fun!

Comments