class Robokassa::Interface
Public Class Methods
# File lib/robokassa/interface.rb, line 31 def initialize(options) @options = options end
Public Instance Methods
returns auth.robokassa.ru or merchant.roboxchange.com in order to current mode
# File lib/robokassa/interface.rb, line 174 def base_url test_mode? ? 'http://auth.robokassa.ru/Merchant' : 'https://merchant.roboxchange.com' end
Fail callback requiest handler It requires Robokassa::Interface.fail_implementation to be inmplemented by user
# File lib/robokassa/interface.rb, line 115 def fail(params, controller) parsed_params = map_params(params, @@notification_params_map) fail_implementation( parsed_params[:invoice_id], parsed_params[:amount], parsed_params[:language], parsed_params[:custom_options], controller) end
returns url to redirect user to payment page
# File lib/robokassa/interface.rb, line 179 def init_payment_base_url "#{base_url}/Index.aspx" end
make hash of options for init_payment_url
# File lib/robokassa/interface.rb, line 145 def init_payment_options(invoice_id, amount, description, custom_options = {}, currency='', language='ru', email='') options = { :login => @options[:login], :amount => amount.to_s, :invoice_id => invoice_id, :description => description[0, 100], :signature => init_payment_signature(invoice_id, amount, description, custom_options), :currency => currency, :email => email, :language => language }.merge(Hash[custom_options.sort.map{|x| ["shp#{x[0]}", x[1]]}]) if @options[:test_mode] options[:isTest] = 1 end map_params(options, @@params_map) end
calculates md5 from result of :init_payment_signature_string
# File lib/robokassa/interface.rb, line 163 def init_payment_signature(invoice_id, amount, description, custom_options={}) md5 init_payment_signature_string(invoice_id, amount, description, custom_options) end
generates signature string to calculate 'SignatureValue' url parameter
# File lib/robokassa/interface.rb, line 168 def init_payment_signature_string(invoice_id, amount, description, custom_options={}) custom_options_fmt = custom_options.sort.map{|x|"shp#{x[0]}=#{x[1]}"}.join(":") "#{@options[:login]}:#{amount}:#{invoice_id}:#{@options[:password1]}#{custom_options_fmt.blank? ? "" : ":" + custom_options_fmt}" end
Generates url for payment page
Example¶ ↑
<%= link_to “Pay with Robokassa”, interface.init_payment_url(order.id, order.amount, “Order #{order.id}”, '', 'ru', order.user.email) %>
# File lib/robokassa/interface.rb, line 131 def init_payment_url(invoice_id, amount, description, currency='', language='ru', email='', custom_options={}) url_options = init_payment_options(invoice_id, amount, description, custom_options, currency, language, email) "#{init_payment_base_url}?" + url_options.map do |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" end.join('&') end
Maps gem parameter names, to robokassa names
# File lib/robokassa/interface.rb, line 137 def map_params(params, map) params = params.is_a?(Hash) ? params : params.permit!.to_hash parsed_params = Hash[params.map do|key, value| [(map[key] || map[key.to_sym] || key), value] end] parsed_params[:custom_options] = Hash[params.select{ |k,v| k.respond_to?(:starts_with?) && k.starts_with?('shp') }.sort.map{|k, v| [k[3, k.size].to_sym, v]}] parsed_params end
This method verificates request params recived from robocassa server
# File lib/robokassa/interface.rb, line 84 def notify(params, controller) begin notify_validate_signature(params) parsed_params = map_params(params, @@notification_params_map) notify_implementation( parsed_params[:invoice_id], parsed_params[:amount], parsed_params[:custom_options], controller) "OK#{parsed_params[:invoice_id]}" rescue Robokassa::InvalidSignature "signature_error" end end
calculates signature to check params from Robokassa
# File lib/robokassa/interface.rb, line 53 def notify_response_signature(parsed_params) md5 notify_response_signature_string(parsed_params) end
build signature string
# File lib/robokassa/interface.rb, line 47 def notify_response_signature_string(parsed_params) custom_options_fmt = parsed_params[:custom_options].sort.map{|x|"shp#{x[0]}=#{x[1]}"}.join(":") "#{parsed_params[:amount]}:#{parsed_params[:invoice_id]}:#{@options[:password2]}#{custom_options_fmt.blank? ? "" : ":" + custom_options_fmt}" end
# File lib/robokassa/interface.rb, line 57 def notify_validate_signature(params) parsed_params = map_params(params, @@notification_params_map) if notify_response_signature(parsed_params) != parsed_params[:signature].downcase raise Robokassa::InvalidSignature.new end end
Handler for success api callback this method calls from RobokassaController It requires Robokassa::Interface.success_implementation to be inmplemented by user
# File lib/robokassa/interface.rb, line 102 def success(params, controller) success_validate_signature(params) parsed_params = map_params(params, @@notification_params_map) success_implementation( parsed_params[:invoice_id], parsed_params[:amount], parsed_params[:language], parsed_params[:custom_options], controller) end
calculates signature to check params from Robokassa
# File lib/robokassa/interface.rb, line 71 def success_response_signature(parsed_params) md5 success_response_signature_string(parsed_params) end
build signature string
# File lib/robokassa/interface.rb, line 65 def success_response_signature_string(parsed_params) custom_options_fmt = parsed_params[:custom_options].sort.map{|x|"shp#{x[0]}=#{x[1]}"}.join(":") "#{parsed_params[:amount]}:#{parsed_params[:invoice_id]}:#{@options[:password1]}#{custom_options_fmt.blank? ? "" : ":" + custom_options_fmt}" end
# File lib/robokassa/interface.rb, line 75 def success_validate_signature(params) parsed_params = map_params(params, @@notification_params_map) if success_response_signature(parsed_params) != parsed_params[:signature].downcase raise Robokassa::InvalidSignature.new end end
# File lib/robokassa/interface.rb, line 35 def token @options[:token] end