Subhashmohan's Blog

Implementing Bibit Payment for Rails Applications

Posted on: June 23, 2009

Every one in the Ruby / Rails world knows and implements different payment methodologies for their E-commerce shopping cart. We understand people know much about how to integrate with Paypal,…blah blah blah… however the biggest issue with all these payment gateways is that the customers have to be associated with multiple websites i.e. If I want to buy something from a cart which associates to Paypal gateway, I need to have an account. So if that is the case then for every different Ecommerce payment gateway I need to have a separate account and I need to remember the credentials. Also many times if I want to implement offline payments such as money orders or cheque, etc., we don’t have any implementation that could take care of this system.

However many time I wished that it would be good that these shopping companies support multiple payment gateways and also multiple methods which are country oriented. E.g. Paypal is not an acceptable method in India, where as it is a Hit in US… Same way In France JCB is well known and in UK, Diners club, switch, maestro cards are well known. So I always believe that I should be allowed to pick up only the ones I need to make the payments as most of the time I shall be associated to one of them rather than getting myself associated to every one and forget at the end of the day where what transaction took place.

Thankfully to my support came in Bibit, a payment service of Royal Bank of Scotland (RBS) which supports almost all the payment gateways available and provides you information of gateways based on the country of the shopper. And to my advantage, I was supposed to write a code for a shopping cart to which it supported. Bibit is very much well known in Europe. Bibit also support Multi-Lingual

Before I start telling you how to implement bibit, a small introduction about BIBIT

“The Bibit Redirect, or Select, service model is an integration method to the Bibit Payment Service suited for Internet shop environments, call centers or reservation centers, and multi-channel sales situations. It allows for real-time processing of payments and ensures a maximum number of up-to-date payment methods. The Redirect model is secure, provides Bibit with required information to perform active fraud risk assessment, and is the fastest way to get up and running with on-line payments.” – As available in bibit website.

More details can be found in . Also this post picks up much of the information from the bibit document and has been modified to suit Ruby on Rails users.

What bibit does is, it provides a standard methodology to the shopping cart websites who can register with Bibit and then run their code with bibit. Once the shopping websites provide bibit some information the control is transferred to Bibit and after completion just like paypal, you can take back the control.  So actually the shopping Cart websites integrate their systems with bibit and bibit provides the customers of the shopping cart websites with various predefined payment gateways based on the country and languages.

Now we know what Bibit is, Lets to see how we can integrate bibit to our shopping carts.

Bibit provides an exclusive method called XML Order Creation which can be utilized to send information to bibit’s website as a secured channel and they handle the rest. The data is sent as an XML with some key elements such as description, amount, orderContent, paymentMethodMask and shopper.

Some of the Elements of this XML are as follows

1. Document Type Declarations

As with any XML declaration we need to declare bibit as well to use the standards payment service dtd as follows

<?xml version=”1.0″?>

<!DOCTYPE paymentService PUBLIC “-//Bibit//DTD Bibit PaymentService v1//EN”


2. Merchant Information

This is the information that the shopping cart have to provide to bibit in order to authorize the input XML. The main information required is the merchant code.

<paymentService version=”1.4″ merchantCode=”MYMERCHANTCODE”>




The XML requires only the Merchant code, however in order to send the xml to the bibit website we need the merchant code as login and the xml password to send this XML. This is more explained in the Http Connectivity section

3.  Order Creation Details

While creating an order we have to follow few significant methods so that Bibit understands the order that has been sent. Some of them are

a.  Order Description – Requires Order Code, Description, Order value, Currency in which order is being placed and the decimal place. The below is a sample example of an order. The Order Code has to be very much unique from bibit’s perspective and hence it is advisable to create the ordercode with a Salt of your’s and a number

<order orderCode=”T0211011″>

<description>Some description of your product</description>

<amount value=”2600″ currencyCode=”USD” exponent=”2″/>


b.  Order Content – Has the complete details of the order and its line items. These details are wrapped inside a CDATA to allow bibit to confirm / show the user the order details. This is something like giving the details of what you would ideally put in a Confirm page before you place the order.


<![CDATA[content here]]>


Some of the information that is generally part of order content are order code, line items, item price, total amount, shipping and billing addresses, merchant contact details, etc., For more information you can refer to bibit’s documentation.

c.  Selecting Payment Gateways – In bibit, you can select which all payment gateways that you can allow to your client to login and support.


<include code=”ALL”/>

<include code=”VISA-SSL”/>

<exclude code=”AMEX-SSL”/>


d.  Email address – Finally we need to give the shopper’s email address as follows




4.  XML Validation

Finally it is a good practice to have a XML validation done on your xml. Bibit is quite strict about xml format and the transaction might fail for this activity.

So once you have the XML ready, it should look as below

<?xml version=”1.0″?>

<!DOCTYPE paymentService PUBLIC “-//Bibit/DTD Bibit PaymentService v1//EN”


<paymentService version=”1.4″ merchantCode=”MYMERCHANT”>


<order orderCode=”T0211011″>

<description>20 tulip bulbs from MYMERCHANT Webshops</description>

<amount value=”2600″ currencyCode=”EUR” exponent=”2″/>




<tr><td bgcolor=”#ffff00″>Your Internet Order:</td><td colspan=”2″

bgcolor=”#ffff00″ align=”right”>T0211011</td></tr>

<tr><td bgcolor=”#ffff00″>Description:</td><td>20 Tulip bulbs</td><td


<tr><td colspan=”2″>Subtotal:</td><td align=”right”>20,00</td></tr>

<tr><td colspan=”2″>VAT: 15%</td><td align=”right”>3,00</td></tr>

<tr><td colspan=”2″>Shipping and Handling:</td><td align=”right”>3,00</td></tr>

<tr><td colspan=”2″ bgcolor=”#c0c0c0″>Total cost:</td><td bgcolor=”#c0c0c0″

align=”right”>Euro 26,00</td></tr>

<tr><td colspan=”3″>&nbsp;</td></tr>

<tr><td bgcolor=”#ffff00″ colspan=”3″>Your billing address:</td></tr>

<tr><td colspan=”3″>Mr. J. Shopper,<br>11 Shopperstreet,<br>1234


<tr><td colspan=”3″>&nbsp;</td></tr>

<tr><td bgcolor=”#ffff00″ colspan=”3″>Your shipping address:</td></tr>

<tr><td colspan=”3″>Mr.J. Shopper,<br>11 Shopperstreet,<br>1234


<tr><td colspan=”3″>&nbsp;</td></tr>

<tr><td bgcolor=”#ffff00″ colspan=”3″>Our contact information:</td></tr>

<tr><td colspan=”3″>MYMERCHANT Webshops International,<br>461 Merchant

Street,<br>1255 Merchanttown,<br>Netherlands

<br><br><br>(111) 1235 456</td></tr>

<tr><td colspan=”3″>&nbsp;</td></tr>

<tr><td bgcolor=”#c0c0c0″ colspan=”3″>Billing notice:</td></tr>

<tr><td colspan=”3″>Your payment will be handled by Bibit Global Payments

Services<br>This name may appear on your bank






<include code=”VISA-SSL”/>

<include code=”AMEX-SSL”/>

<include code=”ECMC-SSL”/>









<street>11 Shopperstreet</street>










What Next, now lets do some Ruby coding to send this XML to bibit and do the processing. We shall utilize Net HTTP for the same. So in your controller i.e. where you have checkout processing being done you can add this code. I shall call my controller as CartController and the code shall be in cart_controller.rb

The first thing I require is to declare the required lib files. We need to declare http, https and uri classes. These are available as follows

require ‘net/https’

require ‘net/http’

require ‘uri’

Once you have your declaration ready all you have to do is write the code in the make_payment  method

def make_payment

#….write all the necessary do necessary bibit payments

Lets understand one by one on what has to be filled with in the make_payment method

First get the XML ready

xml_string = %{xml as above}

It is better to putin the xml with in the %{} as the content becomes a well formatted xml. If we try to do it as quotes or double quotes, lots of escape sequences have to be used which is a pain. We found out sing %{} is the best method.

Once your XML is ready, Start posting the XML throught NET HTTP

All you need for this to work, is connection to the bibit service, your merchant code and the merchant password that you have declared in using your login credentials.

url = URI.parse(””), url.port)

@http.use_ssl = true


req =


req[‘Content-Type’] = ‘text/xml’

req.body = xml_string # as seen above

response = http.request(req)

response_xml = response.body

return response_xml


The URL you see is for the bibit’s test environment, All you have to do is to change the url to

Handling the Response

Once you send the XML to the bibit as above, bibit shall send you back an response XML which gives you some information about bibit accepting your order. All we have to do is to extract the order code and the re-direction information from response xml and re-direct our application to bibit to do the complete gateway process

<?xml version=”1.0″?>

<!DOCTYPE paymentService PUBLIC “-//Bibit//DTD Bibit PaymentService v1//EN”


<paymentService merchantCode=”MYMERCHANTCODE” version=”1.4″>


<orderStatus orderCode=”T0211011″>

<reference id=”1234567″>






All that is important in this xml is

Order URL – This is the URL that has to be redirected to allow bibit to complete the payments. The url in the xml is


Reference ID – This ID is more necessary for Future references or offline payments so that you know the status of the product. The reference ID in this xml response is


Order Code – This is the code that we generated before sending it to the bibit payment. This has to be unique and can be used to check in the database for order  placement. The order code in the xml response is


In order to extract the information from the response xml we can use any parser such as ReXML parser / Atom Parser / CobraVsMongoose

Redirecting to Bibit

Once the URL has been acquired from the response XML, do a redirection to Bibit. Also you can do additional activities so that you can change the look and feel and information in the website.

a. Redirecting URLs

Additionally you can customize the URL to send your success / failure / pending URLs to Bibit to redirect to your website so that you can handle the particular order status i.e. When the Bibit completes the payment and if you want them to redirect back to your website (similar to returnurl of paypal), you can provide them as encoded text along with the bibit url (available in the response xml). The procedure could be as follows

&successURL=CGI::escape(url_for(:controller=>”cart”, :action=>”success”)

&pendingURL=CGI::escape(url_for(:controller=>”cart”, :action=>”pending”)

&failureURL=CGI::escape(url_for(:controller=>”cart”, :action=>”failure”)^T0211011&

You are at your disposable to implement any of these. Bibit shall redirect to your URLs along with the order code so that you can handle if you want to convert the cart into an order or not. A sample Success URL that bibit creates is as follows^MYMERCHANTCODE^T0211010&paymentStatus=AUTHORISED&paymentAmount=2600&paymentCurrency=USD&mac=0083c47880f0533d773c350ee0d51cfc

b. Setting Country and Language Codes

You can also set bibit the country code and the language code in order to allow bibit to show the payment methodologies for particular country and the bibit originating text based on language. This is again sent as part of the bibit url. Example URL is as follows^T0211011&country=GB&language=en

c. Setting User Interface of Bibit Payment Service

You can also send font / body attributes to allow bibit show information in the way you want. E.g.^T0211011&bodyAttr=bgcolor%3D%22black%22&fontAttr=face%3D%22arial%22+color%3D%22white%2

Hope this information helps you to start understanding and handle payments for your websites. We say three cheers to Bibit to help us solve a big problem.

Let me know your comments or if you need any support implementing Bibit Payments for your website.


5 Responses to "Implementing Bibit Payment for Rails Applications"

Nice Writing!! Will give a try… Thanks…

I am in the process of setting up bibit for my company and your blog is a very helpful one. I have some questions though. How do you implement dodirect paypment kinda thing. Where you take all Credit card details on your page itself and interaction with bibit only acts in background. Basically the customer stays on your site all the time.

And have you tried Active Merchant. Its the default payment library for Rails though it seems it doesn’t support bibit yet.

Hi Amit,

If you want the customer to stay on your site all the time. You can use an iframe and call the bibit url inside the iframe with a fixed height and width and no border. It will give the customer a feel that he’s still on your site.


could you please let me know what is the configuration we should do in apache to implement the ssl.

we have a website which is using bibit as a payment gateway, now the transaction between payment gateway and the site should be secured.

please let know thanks in advance

my email id us

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog Stats

  • 3,836 hits
June 2009
    Mar »



%d bloggers like this: