After a lot of fiddling and help, the working solution was to search for an error
key within the response params hash and if an error was present add the message to the object errors. Not particularly elegant but it now does what i want.
ORDER MODEL
def purchase(basket)
response = GATEWAY.purchase(Product.total_basket_price(basket)*100, credit_card, purchase_options)
if response.params.key?('error')
self.errors.add :base, response.message
false
else
true
end
end
ORDERS CONTROLLER
Also switched the order of the if
statements in the controller so that def purchase(basket)
runs first before the order is saved, allowing the error message(s) from the response to be caught and displayed.
if @order.purchase(basket)
if @order.save
render "show"
else
render "new"
end
else
render "new"
end
VIEW
<%= if @order.errors.any?
@order.errors[:base].to_sentence
end%>