PayPal Hacks. 100 Industrial-Strength Tips and Tools Free Open Book

PayPal Hacks. 100 Industrial-Strength Tips and Tools

Previous Section  < Day Day Up >  Next Section

Hack 86 Synchronizing PDT and IPN

figs/expert.gif figs/hack86.gif

Ensure that your product is delivered, even when PDT fails and the return page never shows, by introducing redundancy with IPN.

PayPal's PDT system [Hack #85] automatically redirects your customers back to your web page after they pay and sends the transaction information along with them. While this is an effective way to deliver products and services to your customers without forcing them to wait for IPN to contact your server, it's certainly not infallible. If you care about record keeping, you'll want to use IPN to record payment details into a database [Hack #82] so that you don't miss any payments.

This hack shows how to coordinate PDT with IPN to ensure that every transaction is processed by your server. The potential problem here is that when using PDT, or even the return variable feature, your customer can be redirected back to your web site before the IPN system has finished processing. You can address this issue by checking your local database to see whether or not the transaction details have been inserted yet; this refreshes the return page until the order has been processed and the IPN data has been received.

The reason you still need to use the IPN system is that the PDT is intended to be used only for a one-time query when the transaction takes place. If that query fails, the data for that transaction is lost forever. The IPN system has a high level of redundancy; it continues to call your IPN processing script for up to four days until it processes successfully.


7.26.1 The Code

The following ASP code simply reads the transaction data passed from PDT and then checks your local database to see if the IPN has finished processing the transaction. If not, it repeatedly refreshes the page (every five seconds) until it finds the corresponding transaction in the database. Use this as your PDT return page:

<%

1. Response.AddHeader "Pragma","no-cache"

Response.Expires = 0

Response.buffer = true

Response.clear



2.  'Create transaction id variable

Dim txn_id

txn_id = Request("txn_id")



3.  'Check if IPN has been processed with database query and recordset

Dim rsOrderCheck

Set rsOrderCheck = Server.CreateObject("ADODB.Recordset")

rsOrderCheck.ActiveConnection = MM_connPayloadz_STRING

rsOrderCheck.Source = "SELECT tblOrderDetails.* FROM tblOrderDetails 

                WHERE tblOrderDetails.txn_id = '" & txn_id & "'"

rsOrderCheck.Open( )



4.  'Count how many times you refresh the browser

Dim vRCount

If Request("rcount") = "" Then 

        vRCount = 1

Else

        vRCount = cInt(Request("rcount")) + 1

End If

%>

<html>

<head>

<% If rsOrderCheck.EOF And rsOrderCheck.BOF Then 'ipn not processed yet %>

5.  <meta http-equiv="refresh" content="5;URL=

                http://paypalhacks.com/pdtpage.asp?txn_id=<%=Request("txn_id")%

                &rcount=<%=vRCount%>">

<% End If %>

</head>

<body>

<% If rsOrderCheck.EOF And rsOrderCheck.BOF Then 'ipn not processed yet %>

6.  Please wait while we locate your order.

This may take up to 30 seconds.

<% Else 'ipn has been processed %>

7.  IPN has been processed, insert content here.

<% End If %>

</body>

</html>

Line 1 tells the browser and server not to cache the page content, but rather to expire it immediately; this makes sure that new content appears when it is available. Then, line 2 initializes the transaction ID, and line 3 checks it against the database. Line 5 contains the meta refresh tag, which refreshes the page automatically if the recordset is empty (e.g., if IPN has not processed the order yet).

Place your own messages on lines 6 and 7 to inform the customer that the order is still being processed and that the order is ready, respectively.

This example illustrates synchronizing the PDT and IPN system, but you can also use the same technique presented here for your return page if you are not using the PDT system. For information on using the return page for order processing, see [Hack #85] .


7.26.2 Hacking the Hack

Normally, the IPN system contacts your server and completes the process in a matter of seconds after the customer pays. However, there are times when the IPN system can take longer (up to several minutes or even hours). This can be caused by load on the PayPal system, on your site, or any number of other possibilities. In the event of such a delay, the repeated refreshing of the page is likely to induce seizure in your customer or, at the very least, try his patience.

To address this issue, you might want to limit the number of times the browser is refreshed and display a message to the customer if that limit is reached (something to the effect that his order is still being processed and he should contact you or get a cup of coffee or something). Simply add the following snippet of code before the opening <html> tag:

<%

'Redirect customer to order search timeout page

If vRCount => 5 Then

 Response.Redirect("ordertimeout.asp")

End If

%>

This code simply checks the number of times the browser has been refreshed (vRCount, set in the original code) and interrupts the process after five unsuccessful tries (this means that at least 30 seconds have passed since the customer was first sent to your PDT page).

    Previous Section  < Day Day Up >  Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


         Main Menu
    PayPal Hacks
    Table of Contents
    Copyright
    Credits
    Preface
    Chapter 1. Account Management
    Chapter 2. Making Payments
    Chapter 3. Selling with PayPal
    Chapter 4. Payment Buttons
    Chapter 5. Storefronts and Shopping Carts
    Chapter 6. Managing Subscriptions
    Chapter 7. IPN and PDT
    Introduction: Hacks #65-86
    What IPN and PDT Are
    How IPN Works
    Advantages of PDT
    Hack 65 Receive Instant Payment Notifications
    Hack 66 Troubleshoot Instant Payment Notifications
    Hack 67 Send a Purchase Confirmation Email with IPN
    Day Day Up
    Hack 69 Use IPN with eBay Listings
    Hack 70 Track Your eBay Products with IPN
    Hack 71 Deliver Digital Goods with IPN
    Hack 72 Deliver Digital Goods with a Return Page
    Hack 73 Implement Price Checking with IPN
    Hack 74 Provide an Order Summary with IPN
    Hack 75 Upsell Your Customers
    Hack 76 Enable Multiple IPN Pages
    Hack 77 Use Mass Pay to Create an Affiliate System
    Hack 78 Manage Your Inventory with IPN
    Hack 79 Display Donation Goals on Your Web Site
    Hack 80 Display a Recent Donor List
    Hack 81 Capture Customer Information with IPN
    Hack 82 Insert Payment Details into a Database with IPN
    Hack 83 Insert Cart Details into a Database
    Hack 84 Track Google Referrals
    Hack 85 Process Payments like a Credit Card with PDT
    Hack 86 Synchronizing PDT and IPN
    Chapter 8. The PayPal Web Services API
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele