Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






Hacking PayPal
Pages: 1, 2, 3

ModerateHack #66

Troubleshoot Instant Payment Notifications

Effectively diagnose processing problems and overcome some of IPN's stumbling blocks.

The IPN system is one of the most powerful features of the PayPal system. Deploying it requires a certain level of programming skill, but even with perfect programming, there can be issues that arise in deploying any new system for the first time. In the case of implementing IPN, there are several things you can do to help diagnose any issues that arise.

The first step in testing your IPN system is to make a live purchase on the system so that the script gets called by PayPal [Hack #65] .

Adding Email to IPN

A good way to help diagnose problems is to have your IPN processing page send you all the variables and their values as they were posted to the PayPal site. You can do this by inserting a server mail component function that emails you the complete form post from PayPal when your IPN page is called. You can add the code to send an email and also to include a switch to turn this function on and off with the following code, written in VBScript for Active Server Pages:

Dim vTesting
vTesting = 1 'Uncomment for test mode on
'vTesting = 0 'Uncomment for test mode off
If vTesting = 1 Then 'Send test email
 Dim TestCDO
 Set TestCDO = Server.CreateObject("CDONTS.NewMail")
 TestCDO.From = "youremail@yourisp.com"
 TestCDO.To = "youremail@yourisp.com"
 TestCDO.Subject = "IPN Variables"
 TestCDO.Body = Request.Form( )
 TestCDO.Send( )
 Set TestCDO = Nothing
End If

With this code added to the basic IPN processing code [Hack #65], the IPN page sends you an email with all the transaction data as posted by PayPal. This can help you determine whether the problems are with the data being passed back.

Using a Return URL

The next way to test your IPN script is to check to see if your IPN page is throwing any errors. You can do this easily by redirecting to your IPN page after payment (using the return variable) and having the IPN information sent when you hit the page. This provides the same functionality that normally occurs behind the scenes, except you are able to see it firsthand. You need to add the following code to your test purchase button to accomplish this:

<input type="hidden" name="rm" value="2">
<input type="hidden" name="return" value="http://yoursite.com/ipn.asp">

When this code is added to your purchase button, PayPal redirects you back to your IPN script after payment and a form post is sent that allows you to see if the page has an error on it.

TIP: If you're using Internet Explorer, you should also configure your browser to show descriptive server errors by disabling the "Show friendly HTTP error messages" option, found in Tools→Internet Options→Advanced. Now, when a page with an error is loaded, you'll get a descriptive message regarding the error and the line on which it occurred.

Capturing Errors

One way to find out if your IPN script is causing an error is to insert error-capturing code within your IPN page. When a page error occurs, you can get an email letting you know that an error has occurred and what the error was. This example uses ASP written in VBScript. First, you have to add the following piece of code to the top of your IPN page:

<% On Error Resume Next %>

That line makes sure that the page continues to process if an error is detected. Then, at the bottom of your IPN page, insert the following:

<% 
ErrorCheck( ) 
Function ErrorCheck( )
        If Err.Number <> 0 then  'if there is an error then the html table will be
                written out
  Dim ErrorCDO
  Set ErrorCDO = Server.CreateObject("CDONTS.NewMail")
  ErrorCDO.From = "youremail@yourisp.com"
  ErrorCDO.To = "youremail@yourisp.com"
  ErrorCDO.Subject = "IPN Error"
  ErrorCDO.Body = "Error: " & Err.Number & " " & VbCrLf & "Description: " &
                Err.Description & ""
  ErrorCDO.Send( )
  Set ErrorCDO = Nothing
 End If
End Function
%>

Once you add this code to your IPN script, you'll be notified via email when an error has occurred.

WARNING Since the page uses an On Error Resume Next statement, it assumes that the post worked properly and does not send an error back to the PayPal system or try again. Without this statement, PayPal would continue to repost the information back to your IPN script until it was successful. Therefore, you should use this technique only during testing phases and not in a live implementation.

Using a Third-Party Testing Script

Another easy way to test your IPN page is to use a third-party testing script that simulates a PayPal purchase to your IPN script without having to make an actual purchase. The best third-party testing script is located at http://www.eliteweaver.co.uk/testing/ipntest.php. Test your script by simply entering your IPN page's web address. You also have to change the following line in your IPN page (the postback line) so your script does not try to send the posted data back to PayPal as it causes an Invalid response from their system:

objHttp.open "POST", "https://www.paypal.com/cgi-bin/webscr", false

Change it like so:

objHttp.open "POST", "http://www.eliteweaver.co.uk/cgi-bin/webscr", false

Then, you can fill in the script form with any information you like and submit it to simulate the post to your IPN script. You can find a list of all available testing scripts at http://www.paypal.com/cgi-bin/webscr?cmd=p/pdn/3p-solutions-ipntools-outside.

Shannon Sofield began working with Macromedia development tools when Macromedia acquired Future Splash.

Dave Nielsen is the co-founder of CloudCamp, and principal consultant at Platform D, a developer programs consulting company. He is also coauthor of PayPal Hacks.

Dave Burchell got his start with computers by programming the Radio Shack TRS-80 in BASIC and the Commodore 64 in 6510 assembly. A fervent proponent of XML, Dave enjoys solving content-management problems with markup and open source software.


View catalog information for PayPal Hacks

Return to the Web DevCenter.