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, Authorize.net…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 http://www.bibit.com . 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”

“http://dtd.bibit.com/paymentService_v1.dtd”>

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

<submit>

</submit>

</paymentService>

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″/>

</order>

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.

<orderContent>

<![CDATA[content here]]>

</orderContent>

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.

<paymentMethodMask>

<include code=”ALL”/>

<include code=”VISA-SSL”/>

<exclude code=”AMEX-SSL”/>

</paymentMethodMask>

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

<shopper>

<shopperEmailAddress>shopper@myshopper.com</shopperEmailAddress>

</shopper>

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”

“http://dtd.bibit.com/paymentService_v1.dtd”>

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

<submit>

<order orderCode=”T0211011″>

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

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

<orderContent>

<![CDATA[

<center><table>

<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

align=”right”>1,00</td></tr>

<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

Shoppercity,<br>Netherlands</td></tr>

<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

Shoppercity,<br>Netherlands</td></tr>

<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>mymerchant@webshops.int<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

statement<br>http://www.bibit.com</td></tr&gt;

</table></center>

]]>

</orderContent>

<paymentMethodMask>

<include code=”VISA-SSL”/>

<include code=”AMEX-SSL”/>

<include code=”ECMC-SSL”/>

</paymentMethodMask>

<shopper>

<shopperEmailAddress>jshopper@myprovider.int</shopperEmailAddress>

</shopper>

<shippingAddress>

<address>

<firstName>John</firstName>

<lastName>Shopper</lastName>

<street>11 Shopperstreet</street>

<postalCode>1234</postalCode>

<city>Shoppercity</city>

<countryCode>NL</countryCode>

<telephoneNumber>0123456789</telephoneNumber>

</address>

</shippingAddress>

</order>

</submit>

</paymentService>

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 code.to do necessary bibit payments
end

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 bibit.com using your login credentials.

url = URI.parse(”https://secure-test.bibit.com/jsp/merchant/xml/paymentService.jsp”)

@http=Net::HTTP.new(url.host, url.port)

@http.use_ssl = true

@http.start(){|http|

req = Net::HTTP::Post.new(url.path)

req.basic_auth MYMERCHANTCODE, MYXMLPASSWORD

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 https://secure-test.bibit.com/jsp/merchant/xml/paymentService.jsp.

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”

“http://dtd.bibit.com/paymentService_v1.dtd”>

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

<reply>

<orderStatus orderCode=”T0211011″>

<reference id=”1234567″>

https://secure-test.bibit.com/jsp/shopper/SelectPaymentMethod.jsp?orderKey=

MYMERCHANT^T0211011

</reference>

</orderStatus>

</reply>

</paymentService>

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

https://secure.bibit.com/jsp/shoppe/SelectPaymentMethod.jsp?orderKey=

MYMERCHANT^T0211011

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

1234567

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

T0211011

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”)

https://secure.bibit.com/jsp/shopper/SelectPaymentMethod.jsp?orderKey=MYMERCHANTCODE^T0211011&successURL=http%3A%2F%2Fyour_domain.com%2Fcart%2Fsuccess&failureURL=http%3A%2F%2Fyour_domain.com%2Fcart%2Ffailure&pendingURL=http%3A%2F%2Fyour_domain.com%2Fcart%2Fpending

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

http://www.your_domain.com/cart/success?orderKey=MYADMINCODE^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
https://secure.bibit.com/jsp/shopper/SelectPaymentMethod.jsp?orderKey=MYMERCHANTCODE^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.

https://secure.bibit.com/jsp/shopper/SelectPaymentMethod.jsp?orderKey=MYMERCHANTCODE^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.

Advertisements

5 Responses to "Implementing Bibit Payment for Rails Applications"

Nice Writing!! Will give a try… Thanks…

Hi,
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.

hi,

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 debnathjoy@rediffmail.com

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Blog Stats

  • 3,807 hits
June 2009
M T W T F S S
    Mar »
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

Pages

%d bloggers like this: