PayPal notifications - Rails 3.

Hello!

I'm trying to integrate PayPal into my (first) Rails app. I've been
looking at RailsCast #142, http://railscasts.com/episodes/142-paypal-notifications.
But I don't get it to work. My app isn't a normal e-commerce site,
it's a buy and sell site. Every ad has a 'published' boolean, which I
want to set to true if the user completes the payment process. My code
looks like this.

Order model:

class Order < ActiveRecord::Base
  belongs_to :ad

  def paypal_url(return_url, notify_url)
      values = {
        :business => 'anders_1291665108_biz@my_mail.se',
        :cmd => '_xclick',
        :currency_code => "SEK",
        :upload => 1,
        :return => return_url,
        :invoice => id,
        :amount => 15,
        :item_name => "Ad",
        :quantity => 1,
        :lc => "SE",
        :notify_url => notify_url
      }
      "https://www.sandbox.paypal.com/cgi-bin/webscr?" +
values.to_query
    end
end

My Payment Notifications Controller:

class PaymentNotificationsController < ApplicationController
  protect_from_forgery :except => [:create]

  def create
    PaymentNotification.create!(:params => params, :ad_id =>
params[:invoice], :status => params[:payment_status],
                                :transaction_id => params[:txn_id])
    render :nothing => true
  end
end

And my Payment Notification model (where I think the problem is):

class PaymentNotification < ActiveRecord::Base
  belongs_to :ad
  serialize :params
  after_create :mark_ad_as_purchased

  def mark_ad_as_purchased
    if status == "Completed"
      ad.update_attribute(:published => true)
    end
  end
end

I get an error that says:

NoMethodError in Payment notificationsController#create

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.update_attribute

Any help or tips would be appreciated!

Thanks
Anders

Hello!

I'm trying to integrate PayPal into my (first) Rails app. I've been
looking at RailsCast #142, http://railscasts.com/episodes/142-paypal-notifications.
But I don't get it to work. My app isn't a normal e-commerce site,
it's a buy and sell site. Every ad has a 'published' boolean, which I
want to set to true if the user completes the payment process. My code
looks like this.

Order model:

class Order < ActiveRecord::Base
belongs_to :ad

def paypal_url(return_url, notify_url)
values = {
:business => 'anders_1291665108_biz@my_mail.se',
:cmd => '_xclick',
:currency_code => "SEK",
:upload => 1,
:return => return_url,
:invoice => id,
:amount => 15,
:item_name => "Ad",
:quantity => 1,
:lc => "SE",
:notify_url => notify_url
}
"https://www.sandbox.paypal.com/cgi-bin/webscr?" +
values.to_query
end
end

My Payment Notifications Controller:

class PaymentNotificationsController < ApplicationController
protect_from_forgery :except => [:create]

def create
PaymentNotification.create!(:params => params, :ad_id =>
params[:invoice], :status => params[:payment_status],
:transaction_id => params[:txn_id])
render :nothing => true
end
end

And my Payment Notification model (where I think the problem is):

class PaymentNotification < ActiveRecord::Base
belongs_to :ad
serialize :params
after_create :mark_ad_as_purchased

def mark_ad_as_purchased
if status == "Completed"
ad.update_attribute(:published => true)

Assuming this is where it is failing, you are updating the attribute
of ad, but the error says ad is nil. I assume this is supposed to be
setup from params[:invoice] above. I suggest you use ruby-debug to
break into your code here and see what is going on. If you do not
know about ruby-debug have a look at the Rails Guide on debugging.
Also you could look in the log to see the query creating the
notification record to check that ad_id is set correctly.

Colin

Thanks Colin!

You found the root of my problem, the invoice/id was wrong. And thanks
for the tips regarding Ruby-debug. Still has a lot to learn. :smiley:

// Anders

Anders_P wrote in post #967140:

Thanks Colin!

You found the root of my problem, the invoice/id was wrong. And thanks
for the tips regarding Ruby-debug. Still has a lot to learn. :smiley:

// Anders

Hi Anders, I'm having this same problem. Could you tell me what exactly
was wrong with your invoice/id setup? Thanks, -Max