📜 ⬆️ ⬇️

Integration of Robokassa into ActiveMerchant

Note the translator - the post about integration of Robokassa and Rails already was , but the method provided there, I believe, will not suit many.

When you have an application written in Ruby on Rails and you plan to add some payment system (for example, PayPal, Moneybookers or Robokassa, as in our case), the first gem you should think about is the active_merchant from Shopify.

ActiveMerchant is a simple abstract payment library used and sponsored by Shopify.
')
So when I needed to add payments through Robokassa to our project, I opened the list of supported payment systems and was a bit disappointed because Robokassa was not included there. A little later, I found a fork that added support for it, but it was already out of date, so some tests fell on ec801d3d4f8 . So I decided to look at this code and correct it, and not to write everything from scratch.

In fact, in order to fix the tests, I had to just fix a small typo 07fb5494134 ( zbs - approx. Lane ). Yes, it was easy. Then I decided to add to the previous solution different urls for the test environment and production (Robokassa recommends that you first test your code in the sandbox, and only then, when everything works, use it in live mode). Look at this code here - c2ec85d53cb .

Now it's time to add active_merchant to the project. Add it to the gemfile:

gem 'activemerchant', :require => 'active_merchant' 

To use ActionView helpers (such as payment_service_for) you need to put the activemerchant.rb file in the initializers folder:

 require 'active_merchant' require 'active_merchant/billing/integrations/action_view_helper' ActionView::Base.send(:include, ActiveMerchant::Billing::Integrations::ActionViewHelper) 

To use the production mode in the initializer you need to add another line:

 ActiveMerchant::Billing::Base.integration_mode = :production # :test for sandbox 

The next step is the routes. Robokassa makes a push request to the application when the transaction is completed, so you need to add the following lines to routes.rb:

 scope 'robokassa' do match 'paid' => 'robokassa#paid', :as => :robokassa_paid # to handle Robokassa push request match 'success' => 'robokassa#success', :as => :robokassa_success # to handle Robokassa success redirect match 'fail' => 'robokassa#fail', :as => :robokassa_fail # to handle Robokassa fail redirect end 

Now create a controller to make it all work:

 class RobokassaController < ApplicationController include ActiveMerchant::Billing::Integrations skip_before_filter :verify_authenticity_token # skip before filter if you chosen POST request for callbacks before_filter :create_notification before_filter :find_payment # Robokassa call this action after transaction def paid if @notification.acknowledge # check if it's genuine Robokassa request @payment.approve! # project-specific code render :text => @notification.success_response else head :bad_request end end # Robokassa redirect user to this action if it's all ok def success if !@payment.approved? && @notification.acknowledge @payment.approve! end redirect_to @payment, :notice => I18n.t("notice.robokassa.success") end # Robokassa redirect user to this action if it's not def fail redirect_to @payment, :notice => I18n.t("notice.robokassa.fail") end private def create_notification @notification = Robokassa::Notification.new(request.raw_post, :secret => AppConfig.robokassa_secret) end def find_payment @payment = Payment.find(@notification.item_id) end end 

Finally, let's add a form to the page:

 <%= payment_service_for @payment.id, AppConfig.robokassa_login, :amount => @payment.amount, :service => :robokassa, :secret => AppConfig.robokassa_secret do |s| %> <%= submit_tag "Submit" %> <% end %> 

That's all! Now, if an @payment object @payment , then after submitting the form, a redirect will occur to the Robokassa website, where you can make a payment for the amount specified in @payment.amount .

Source: https://habr.com/ru/post/147435/


All Articles