diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c5825e..cfa14fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][] diff --git a/README.md b/README.md index a8d0f07..e0e1930 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/controllers/robokassa_controller.rb b/app/controllers/robokassa_controller.rb index 4e0f3e8..72e9d6b 100644 --- a/app/controllers/robokassa_controller.rb +++ b/app/controllers/robokassa_controller.rb @@ -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 diff --git a/lib/generators/rubykassa/templates/rubykassa.rb b/lib/generators/rubykassa/templates/rubykassa.rb index d6ca1ce..eaef2f3 100644 --- a/lib/generators/rubykassa/templates/rubykassa.rb +++ b/lib/generators/rubykassa/templates/rubykassa.rb @@ -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 diff --git a/lib/rubykassa/configuration.rb b/lib/rubykassa/configuration.rb index 8d6272f..0e7be96 100644 --- a/lib/rubykassa/configuration.rb +++ b/lib/rubykassa/configuration.rb @@ -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 \ No newline at end of file