【问题标题】:Spree - Override checkout_controller methodsSpree - 覆盖 checkout_controller 方法
【发布时间】:2023-03-11 16:36:01
【问题描述】:

我需要从控制器checkout_controller.rb重写方法rescue_from_spree_gateway_error

我的网关返回这个:

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:


这是我的覆盖,/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


我已经添加了puts,所以我可以在控制台上看到它,但它没有被打印出来。


我还在方法声明上方添加了另一个puts,它会在我启动服务器时打印出来:

=> 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


所以它显示该类已加载,但该方法从未被调用,就像上面 ActiveMerchant::Billing::Response 返回的错误不是网关错误一样。


checkout_controller 有这一行在 GatewayError 发生时调用该方法:

rescue_from Spree::Core::GatewayError, :with => :rescue_from_spree_gateway_error

【问题讨论】:

  • 可能是因为核心中的方法没有参数,而你的方法有参数?
  • 我真的不知道,我在听邮件组用户的一个建议:groups.google.com/forum/#!topic/spree-user/rRcIoDs-9Dc
  • @zrl3dx 我刚刚试了一下,删除了参数并硬编码了消息flash[:error] = "Not authorized",什么也没发生=/
  • 尝试将其放入app/controllers/spree 而不是app/controllers,AFAIK 装饰器的功能与路径(以及文件名)严格相关。
  • @zrl3dx 没有成功 =/

标签: overriding checkout spree


【解决方案1】:

Spree 版本是 2.0.3。

为了完成这项工作,我必须覆盖两个文件,并在以下位置创建它们:

app/controllers/checkout_controller_decorator.rb

app/models/order_decorator.rb

checkout_controller_decorator.rb 代码:

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

还有order_decorator.rb 代码:

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

问题出在2.0.3 版本上,所以我不得不用2.0.4 版本中的文件更新这两个文件,问题就解决了=)

【讨论】:

    【解决方案2】:

    您试图捕获的异常被更早地捕获。几乎可以肯定:

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

    您需要重写该函数以添加您正在寻找的逻辑。

    【讨论】:

      猜你喜欢
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2014-03-31
      • 1970-01-01
      • 2015-02-04
      • 2015-08-23
      • 2014-06-18
      相关资源
      最近更新 更多