title: Rails - handling the PayPal IPN callback using the paypal-recurring gem
body: <p>I am using the <a href="https://github.com/fnando/paypal-recurring">paypal-recurring gem</a> to handle recurring payments in a Rails app. The majority of my code is from this excellent <a href="http://railscasts.com/episodes/289-paypal-recurring-billing">Railscast</a> but I also want to add a payment_notification model to accept the IPN callback and store any relevant data. This <a href="http://railscasts.com/episodes/142-paypal-notifications">Railscast</a> goes over how to set up notifications.  However, I am having a hard time figuring out how to send the paypal-recurring gem IPN callback to my PaymentNotification model.</p>
  
  <p>How can I set the :ipn_url to correctly write the IPN callback to my PaymentNotification model.  I tried the following so far:</p>
  
  <p>1) Adding <code>ipn_url: "http://my-app-name.com/payment_notifications"</code> to the process method (under options) or payment_notifications_url</p>
  
  <p>2) Trying the solution suggested at the bottom of this <a href="https://github.com/fnando/paypal-recurring/issues/10">GitHub issue page</a> </p>
  
  <p>3) Using Paypal's Instant Payment Notification (IPN) simulator to send to "http://my-app-name.com/payment_notifications", but I get an error: <em>IPN delivery failed. HTTP error code 401: Unauthorized</em> </p>
  
  <p><strong>EDIT</strong> </p>
  
  <p><strong>I have been able to successfully simulate the delivery of the IPN to my payments_notifications_url. Now I just need to figure out how to point the recurring gem to send the ipn to there.</strong></p>
  
  <p>Any pointers would be greatly appreciated. Below is some of my current code. If I am forgetting any relevant parts, please let me know.</p>
  
  <p><strong>PaypalPayment Model</strong></p>
  
  <pre><code> class PaypalPayment
     def initialize(subscription)
       @subscription = subscription
     end
  
     def checkout_details
       process :checkout_details
     end
  
     def checkout_url(options)
       process(:checkout, options).checkout_url
     end
  
     def make_recurring
       process :request_payment
       process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.zone.now
     end
  
     def cancel_recurring
       process :cancel
     end
  
   private
  
     def process(action, options = {})
       options = options.reverse_merge(
         token: @subscription.paypal_payment_token,
         payer_id: @subscription.paypal_customer_token,
         description: @subscription.plan.name,
         amount: @subscription.plan.monthly_price,
         currency: "JPY"
       )
       response = PayPal::Recurring.new(options).send(action)
       raise response.errors.inspect if response.errors.present?
       response
     end
   end
  </code></pre>
  
  <p><strong>PaymentNotifications Controller</strong></p>
  
  <pre><code> class PaymentNotificationsController &lt; ApplicationController
     protect_from_forgery :except =&gt; [:create]
  
     def create
       PaymentNotification.create!(:params =&gt; params, :status =&gt; params[:payment_status], :transaction_id =&gt; params[:txn_id])
       render :nothing =&gt; true
     end
   end
  </code></pre>