Pages - Menu

Sunday, September 8, 2013

Making spree 2.0.3 show your gateway error messages

Hi there, happy?

I have developed an active merchant gateway, called 2Pay and I configured my Spree to use it.

It was working great until I got warned that when something goes wrong with the client's credit card, spree is always showing a default error message.

Well, that is terrible because the client will have no idea of what is going on among all possibilities.

PAUSE: I have this on StackOverflow if you feel more comfortable there :)

I had to do one thing, check the spree_core source code using

bundle open spree_core
So I noticed how the checkout_controller was calling a default message when something goes wrong with the Gateway object so I had to update it but it didn't work 100%.

Searching a bit more I found that the order.rb model rescue block wasn't adding the error to the errors hash.

Anyway, these are the two files you need to override and to do that you need to create checkout_controller.rb as checkout_controller_decorator.rb on /controller and the order.rb as order_decorator.rb on /model.

I know the paths are obvious but some user on #spree channel told me to save them on different folder and that was making me more confused.

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


I really hope it helps you my friend.

Now, be a nice person and say something nice to who's with you :)

No comments:

Post a Comment