Class: Robokassa::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/robokassa/interface.rb

Constant Summary collapse

@@notification_params_map =
{
    'OutSum'         => :amount,
    'InvId'          => :invoice_id,
    'SignatureValue' => :signature,
    'Culture'        => :language
}
@@params_map =
{
    'MrchLogin'      => :login,
    'OutSum'         => :amount,
    'InvId'          => :invoice_id,
    'Desc'           => :description,
    'Email'          => :email,
    'IncCurrLabel'   => :currency,
    'Culture'        => :language,
    'SignatureValue' => :signature
}.invert
@@service_params_map =
{
    'MerchantLogin'  => :login,
    'Language'       => :language,
    'IncCurrLabel'   => :currency,
    'OutSum'         => :amount
}.invert

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Interface

Returns a new instance of Interface.



31
32
33
# File 'lib/robokassa/interface.rb', line 31

def initialize(options)
  @options = options
end

Instance Method Details

#base_urlObject

returns test.robokassa.ru or merchant.roboxchange.com in order to current mode



170
171
172
# File 'lib/robokassa/interface.rb', line 170

def base_url
  test_mode? ? 'http://test.robokassa.ru' : 'https://merchant.roboxchange.com'
end

#fail(params, controller) ⇒ Object

Fail callback requiest handler It requires Robokassa::Interface.fail_implementation to be inmplemented by user



115
116
117
118
119
120
121
122
123
# 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_urlObject

returns url to redirect user to payment page



175
176
177
# File 'lib/robokassa/interface.rb', line 175

def init_payment_base_url
  "#{base_url}/Index.aspx"
end

#init_payment_options(invoice_id, amount, description, custom_options = {}, currency = '', language = 'ru', email = '') ⇒ Object

make hash of options for init_payment_url



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/robokassa/interface.rb', line 144

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]]}])
  map_params(options, @@params_map)
end

#init_payment_signature(invoice_id, amount, description, custom_options = {}) ⇒ Object

calculates md5 from result of :init_payment_signature_string



159
160
161
# File 'lib/robokassa/interface.rb', line 159

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 = {}) ⇒ Object

generates signature string to calculate ‘SignatureValue’ url parameter



164
165
166
167
# File 'lib/robokassa/interface.rb', line 164

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 = {}) ⇒ Object

Generates url for payment page

Example

<%= link_to “Pay with Robokassa”, interface.init_payment_url(order.id, order.amount, “Order #Robokassa::Interface.orderorder.id”, ”, ‘ru’, order.user.email) %>



131
132
133
134
# 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) ⇒ Object

Maps gem parameter names, to robokassa names



137
138
139
140
141
# File 'lib/robokassa/interface.rb', line 137

def map_params(params, map)
  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

#md5(str) ⇒ Object

:nodoc:



179
180
181
# File 'lib/robokassa/interface.rb', line 179

def md5(str) #:nodoc:
  Digest::MD5.hexdigest(str).downcase
end

#notify(params, controller) ⇒ Object

This method verificates request params recived from robocassa server



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

calculates signature to check params from Robokassa



53
54
55
# 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) ⇒ Object

build signature string



47
48
49
50
# 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) ⇒ Object



57
58
59
60
61
62
# 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) ⇒ Object

Handler for success api callback this method calls from RobokassaController It requires Robokassa::Interface.success_implementation to be inmplemented by user



102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

calculates signature to check params from Robokassa



71
72
73
# 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) ⇒ Object

build signature string



65
66
67
68
# 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) ⇒ Object



75
76
77
78
79
80
# 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?Boolean

Indicate if calling api in test mode

Returns

true or false

Returns:

  • (Boolean)


42
43
44
# File 'lib/robokassa/interface.rb', line 42

def test_mode?
  @options[:test_mode]
end

#tokenObject



35
36
37
# File 'lib/robokassa/interface.rb', line 35

def token
  @options[:token]
end