class Robokassa::Interface

Public Class Methods

new(options) click to toggle source
# File lib/robokassa/interface.rb, line 31
def initialize(options)
  @options = options
end

Public Instance Methods

base_url() click to toggle source

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(params, controller) click to toggle source

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
init_payment_base_url() click to toggle source

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
init_payment_options(invoice_id, amount, description, custom_options = {}, currency='', language='ru', email='') click to toggle source

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
init_payment_signature(invoice_id, amount, description, custom_options={}) click to toggle source

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
init_payment_signature_string(invoice_id, amount, description, custom_options={}) click to toggle source

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
init_payment_url(invoice_id, amount, description, currency='', language='ru', email='', custom_options={}) click to toggle source

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
map_params(params, map) click to toggle source

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
notify(params, controller) click to toggle source

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
notify_response_signature(parsed_params) click to toggle source

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
notify_response_signature_string(parsed_params) click to toggle source

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
notify_validate_signature(params) click to toggle source
# 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
success(params, controller) click to toggle source

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
success_response_signature(parsed_params) click to toggle source

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
success_response_signature_string(parsed_params) click to toggle source

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
success_validate_signature(params) click to toggle source
# 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
test_mode?() click to toggle source

Indicate if calling api in test mode

Returns

true or false

# File lib/robokassa/interface.rb, line 42
def test_mode?
  @options[:test_mode]
end
token() click to toggle source
# File lib/robokassa/interface.rb, line 35
def token
  @options[:token]
end