Rails and Paypal's Payment Data Transfer

When you use Paypal to get payments, you can configure a return URL to which the payer will be redirected. When you activate Payment Data Transfer, you get some information about the payment that redirected to your site. The paypal gem (more info at leetsoft, although the doc you'll find there is out of date) doesn't work with Payment Data Transfer, but only with Instant Payment Notification (notification made separately by Paypal making a post request to your server). I chose to go for PDT as it enables me to immediately give information to the user, as opposed to IPN which happens outside of the user's activity. I have written a bit of code, largely inspired by ( with chunks even simply copied from ) the IPN code (both because it was faster, but also because it makes its usage very similar), to treat PDT received from Paypal. It makes it as easy as working with the IPN. The main difference being the parameter passed to the constructor (the whole post for IPN, just the transaction id for PDT).
    #initialize by passing the transaction id we get from paypal
    pdt = Paypal::PaymentData.new(params["tx"])

    # equivalent to IPN usage
  if pdt.acknowledge
      account = Account.find pdt.item_number
      #as paypal advises, at least check
      #   * the transaction is about the right amount, and 
      #   * the payment's recipient is correct
      if pdt.complete? and  \\
               Money.euro(account.account_type.monthly_fee*100) == \\
               pdt.amount  and \\
        @message = 'payment_not_yet_received'
        #if we can't verify the transaction, notify admins
        SystemNotifier.deliver_paypal_problem_notification(request, pdt)
So, how do you use it? Get the file payment_data.rb and put it in the same directory as the notification.rb file of the Paypal library for rails. Edit the file and put your identity_token in the @@identity_token variable (line 9). you can find it in your paypal profile, under Website Payment Preferences (see the Payment Data Transfer section). I also had to require the file to make the code aboce work. Simply put this line, eg as the first line of the action paypal uses as return url:
    require 'payment_data'
The code is not yet inproduction but working fine with Paypal's developer sandboxes at developer.paypal.com. I decided to blog this following a discussion about Paypal on the rails Mailing list. More documentation about integration with Paypal is available at their integration center, where you can find the Order Management Integration Guide, which details PDT and IPN variables and mechanisms.