When you have a project written using Rails and you want to add payment system to it (e.g PayPal, Moneybookers, etc. or Robokassa in our case) the first gem you should think about is active_merchant
by Shopify:
ActiveMerchant is a simple payment abstraction library used in and sponsored by Shopify.
So when I had to add Robokassa payment system to our project I opened a list or the supported integrations and was kinda disappointed because active_merchant didn’t have it. Latter I found a fork that had Robokassa support but it was outdated and some tests failed ec801d3d4f8. So I decided to look at this code and fix it instead of writing all this stuff from scratch.
Actually to make that tests work I had to fix a small typo 07fb5494134. Yeah, it was easy. The next thing I decided to add to that implementation was different urls for sandbox and production environments (yeah, Robokassa has a rule that you should test your code in sandbox at first and when all your stuff work you can use production environment). You can see this code here - c2ec85d53cb
And then it’s time to add active_merchant to the project. Add it to your Gemfile
:
1
|
|
To use ActionView
helpers like payment_service_for
you should add this to activemerchant.rb
file and put in in the initializers
folder:
1 2 3 4 |
|
If you want to use production mode add this line to initializers too:
1
|
|
The next step is routes. Robokassa makes a push request to our application when transaction was made. So let’s add these routes:
1 2 3 4 5 6 |
|
To make this work we should create controller:
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 |
|
And the last thing we need is to add form to our page:
1 2 3 4 5 6 |
|
That’s it! Now if we have @payment
object and we submitting this form we will be redirected to the Robokassa site where we can make payment with amount of @payment.amount
.