SEJU
SEJU

Reputation: 1135

Rails: Paypal Express implementation fails with undefined method `checkout_thank_you_url'

I am working on the integration of Paypal Express payments. I am adapting a Standard checkout and I am using the active-merchant gem. Set up should be something like response=gateway.setup_purchase(price, details), where details are all the parameter which PayPal requires. The express gateway requires a return_url and cancel_return_url.

When I try to execute the payment through a submit button I get:

Order xx failed with error message undefined method `checkout_thank_you_url' for #<Order:0x1081e1bb8> 

In my order model I have the following parts:

#app/models/order.rb

def process
  process_with_active_merchant
  save!
  self.status == 'processed'
end

private
def process_with_active_merchant
  ActiveMerchant::Billing::Base.mode = :test
  gateway = ActiveMerchant::Billing::PaypalExpressGateway.new( 
    :login     => 'sandbox-account', 
    :password  => 'sandbox-password', 
    :signature => "sandbox-secret"

    params = { 
    :order_id => self.id, 
    :email => email, 
    :address => { :address1 => ship_to_address, 
                  :city => ship_to_city, 
                  :country => ship_to_country, 
                  :zip => ship_to_postal_code 
                } , 
    :description => 'Books', 
    :ip => customer_ip,
    :return_url => checkout_thank_you_url(@order), # return here if payment success
    :cancel_return_url => checkout_index_url(@order) # return here if payment failed
}
  response = gateway.setup_purchase((@order.total * 100).round(2), params)
  if response.success? 
    self.status = 'processed' 
  else 
    self.error_message = response.message 
    self.status = 'failed' 
  end 
end 

The method is called in the checkout controller

def place_order
  @order = Order.new(params[:order])
  @order.customer_ip = request.remote_ip 
  populate_order
  ...
  @order.process
  ...
end

def thank_you
 ...
end

How can I get this to work? Thank you in advance!

Update

I suppose I have to specify the controller and action, but when using:

:return_url => url_for(:controller => :checkout, :action => "thank_you")

I get:

Order 98 failed with error message undefined method `url_for' for #<Order:0x1065471d8>

Upvotes: 1

Views: 197

Answers (1)

Luke Griffiths
Luke Griffiths

Reputation: 899

In your Oder model, include the module that Rails uses to generate URLs.

Add this code inside your class definition for Order:

class Order  
  include Rails.application.routes.url_helpers  
  # ...
end

Those helpers are already included in Controller classes, so methods like checkout_thank_you_url are available in controllers (and hence view templates) by default.

You must include the module into any other classes where you want to use route methods.

Upvotes: 1

Related Questions