This post explains the steps needed to setup ‘TwoCheckout’ payment gateway with rails. Its pretty easy since there is a gem made to integrate twocheckout into rails. I am writing this post to make it easier for those who want to integrate them. I met with some problems when I tried to integrate it with my rails app.
I am not going to explain all the things. I expect that you know the basics of rails to setup products prices and so on. Its pretty easy to do it in a cart as well. But I am not going to explain it for a cart. I will set it up for a single order.
The app has a Product, User and an Orders model. I have used devise to setup the user. The orders model has the user_id, product_id and status. It belongs to both user and product with many to one relation. With these we are going to set up twocheckout.
First add gem twocheckout to Gemfile and run bundle install.
Gemfile
Now that twocheckout is installed, the first thing we need to do is to require it in the environment.rb in config.
config/environment.rb
The products page has a buy button which on click creates an order and redirect it to confirm page.
This makes all the methods available to the entire app. Now the rest have to be done in the confirm payment page. I set it up in the products controller. I also defined a route for it in the routes as confirm.
# config/routes.rb
Now we setup the controller.
# app/controllers/products_controller.rb
Then in the view we just have to call html_safe on the form.
# app/views/products/confirm.html.erb
In confirm page you will see a Checkout button. If you want to change the style of twocheckout button you will have to add a class using JS to that element. I am not going to explain that.
Now we have a button that will be redirected to Twocheckout payment page. If you want to setup inline twocheckout payment (twocheckout as an overlay) you will have to copy paste a JS from here. You will also have to populate all the fields including address, card_holder_name and so on in the params
.
Next we have to set up a callback page from twocheckout. Lets call that return. We will setup the return page.
# config/routes.rb
---
Next we setup the return in products controller. You have to setup callback url from your twocheckout account.
# app/controllers/products_controller.rb
And that is the end of twocheckout setup.