Skip to content

Commit

Permalink
Simplify callbacks logic: no more necessity to pass variable to lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKishenin committed Aug 12, 2014
1 parent 3a2235c commit c8e7ded
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Edge (not released)

* Simplify callbacks logic: no more necessity to pass `controller` variable to lambda. Thanks @BrainNya for active promotion.

## 0.3.2

* Add confugurable result callback [#14][]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ To define custom success/fail callbacks you can also use the initializer:

Rubykassa.configure do |config|
...
config.success_callback = -> (controller, notification) { controller.render text: 'success' }
config.fail_callback = -> (controller, notification) { controller.redirect_to controller.root_path }
config.result_callback = -> (controller, notification) { controller.render text: notification.success }
config.success_callback = -> (notification) { render text: 'success' }
config.fail_callback = -> (notification) { redirect_to root_path }
config.result_callback = -> (notification) { render text: notification.success }
end

Lambdas are called in RobokassaController so you can respond with [any kind that is supported by Rails](http://guides.rubyonrails.org/layouts_and_rendering.html#creating-responses) calling it on the `controller` variable.
Lambdas are called in RobokassaController so you can respond with [any kind that is supported by Rails](http://guides.rubyonrails.org/layouts_and_rendering.html#creating-responses).

NOTE: `result_callback` should always return `"OK#{ invoice_id }"` string. So, implement your custom logic above `render text: notification.success` line.

Expand Down
10 changes: 5 additions & 5 deletions app/controllers/robokassa_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ class RobokassaController < ApplicationController

def paid
if @notification.valid_result_signature?
Rubykassa.result_callback.call(self, @notification)
instance_exec @notification, &Rubykassa.result_callback
else
Rubykassa.fail_callback.call(self, @notification)
instance_exec @notification, &Rubykassa.fail_callback
end
end

def success
if @notification.valid_success_signature?
Rubykassa.success_callback.call(self, @notification)
instance_exec @notification, &Rubykassa.success_callback
else
Rubykassa.fail_callback.call(self, @notification)
instance_exec @notification, &Rubykassa.fail_callback
end
end

def fail
Rubykassa.fail_callback.call(self, @notification)
instance_exec @notification, &Rubykassa.fail_callback
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/generators/rubykassa/templates/rubykassa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

# Result callback is called in RobokassaController#paid action if valid signature
# was generated. It should always return "OK#{ invoice_id }" string, so implement
# your custom logic above `controller.render text: notification.success` line
# your custom logic above `render text: notification.success` line

config.result_callback = -> (controller, notification) { controller.render text: notification.success }
config.result_callback = -> (notification) { render text: notification.success }

# Define success or failure callbacks here like:

# config.success_callback = -> (controller, notification) { controller.render text: 'success' }
# config.fail_callback = -> (controller, notification) { controller.redirect_to controller.root_path }
# config.success_callback = -> (notification) { render text: 'success' }
# config.fail_callback = -> (notification) { redirect_to root_path }
end
22 changes: 8 additions & 14 deletions lib/rubykassa/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ class Configuration

def initialize
self.login = "your_login"
self.first_password = "first_password"
self.second_password = "second_password"
self.mode = :test
self.http_method = :get
self.xml_http_method = :get
self.success_callback = Proc.new do |controller, notification|
render text: 'success'
end
self.fail_callback = Proc.new do |controller, notification|
render text: 'fail'
end
self.result_callback = Proc.new do |controller, notification|
render text: notification.success
end
self.first_password = "first_password"
self.second_password = "second_password"
self.mode = :test
self.http_method = :get
self.xml_http_method = :get
self.success_callback = -> (notification) { render text: 'success' }
self.fail_callback = -> (notification) { render text: 'fail' }
self.result_callback = -> (notification) { render text: notification.success }
end
end
end

0 comments on commit c8e7ded

Please sign in to comment.