Question

I need to override the method rescue_from_spree_gateway_error from the controller checkout_controller.rb

My gateway is returning this:

Gateway Error   --- !ruby/object:ActiveMerchant::Billing::Response params:   success: false   order_status:    message: Autoriza��o negada   amount:    order:    transaction:  message: Autoriza��o negada success: false test: false authorization:  fraud_review:  avs_result:   code:    message:    street_match:    postal_match:  cvv_result:   code:    message:


This is my override, at /app/controllers/checkout_controller_decorator.rb

module Spree
  CheckoutController.class_eval do
    def rescue_from_spree_gateway_error(error)
      puts "==========================================="
      flash[:error] = error.message
      render :edit
    end
  end
end


I have added the puts so I could see it on the console but it was not printed.


I also have added another puts above the method declaration and it prints out when I start the server:

=> Booting WEBrick
=> Rails 3.2.13 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
!!!!!THIS IS ABOVE THE METHOD!!!!! [2013-09-05 16:33:31] INFO  WEBrick 1.3.1 [2013-09-05 16:33:31] INFO  ruby 1.9.3 (2013-06-27) [x86_64-linux] [2013-09-05 16:33:31] INFO  WEBrick::HTTPServer#start: pid=21635 port=3000


So it shows me that the class is loaded but the method is never called like the error returned above by the ActiveMerchant::Billing::Response is not a Gateway Error.


The checkout_controller has this line calling the method when a GatewayError happens:

rescue_from Spree::Core::GatewayError, :with => :rescue_from_spree_gateway_error
Was it helpful?

Solution

The Spree version is 2.0.3.

To make this work I had to override two files, creating them at:

app/controllers/checkout_controller_decorator.rb

app/models/order_decorator.rb

The checkout_controller_decorator.rb code:

module Spree
  CheckoutController.class_eval do
    def update
      if @order.update_attributes(object_params)
        fire_event('spree.checkout.update')
        return if after_update_attributes
        unless @order.next
          flash[:error] = @order.errors[:base].join("\n")
          redirect_to checkout_state_path(@order.state) and return
        end
        if @order.completed?
          session[:order_id] = nil
          flash.notice = Spree.t(:order_processed_successfully)
          flash[:commerce_tracking] = "nothing special"
          redirect_to completion_route
        else
          redirect_to checkout_state_path(@order.state)
        end
      else
        render :edit
      end
    end
  end
end

And the order_decorator.rb code:

module Spree
  Order.class_eval do

    def process_payments!
      if pending_payments.empty?
        raise Core::GatewayError.new Spree.t(:no_pending_payments)
      else
        pending_payments.each do |payment|
          break if payment_total >= total

          payment.process!

          if payment.completed?
            self.payment_total += payment.amount
          end
        end
      end
      rescue Core::GatewayError => e
        result = !!Spree::Config[:allow_checkout_on_gateway_error]
        errors.add(:base, e.message) and return result
      end
    end
  end
end

The problem is with the version 2.0.3 so I had to update those two files with the ones from the version 2.0.4 and the problem is solved =)

OTHER TIPS

The exception you're trying to catch is being caught earlier. Almost certainly here:

https://github.com/spree/spree/blob/v2.0.4/core/app/models/spree/order.rb#L443

You'll need to override that function to add the logic that you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top