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 83 Insert Cart Details into a Database

figs/moderate.gif figs/hack83.gif

Record the contents of customers' Shopping Carts into a database to build a complete order-tracking subsystem.

This hack records a list of products a customer has purchased, in addition to the corresponding payment and customer information. There are two situations in which you'll record purchase information: purchases of a single item (with the Buy Now button) and Shopping Cart transactions.

The first is fairly straightforward and serves as a primer for the more complex Shopping Cart insertion into your database. This hack is necessary for many merchants, because the PayPal history does not keep track of the individual items purchased in Shopping Cart transactions. For Shopping Cart purchases, the history provides only transaction information without any product detail. However, the PayPal IPN system does POST the individual cart values back to us in real time, so we can use that information to create our own payment history with full details.

7.23.1 The Database Table

Create a new database table to hold only the product detail information, as shown in Table 7-6.

Table 7-6. A database table that stores a customer's purchases

item_number

item_name

txn_id

6001

Vitamins

349857340958734958

6002

Sulfuric Acid

459384579348754343

6004

Calculator

345312023123246896

7001

Imitation Gruel

234982134201309323


This table will be used later with the transaction table to give a complete view of any specific transaction.

You will not record any of the payment information in this table, because you have already captured it in [Hack #82] .


You can join the tables using the transaction ID as the key; it will be the same in both tables for any one transaction. The minimal information you'll capture for each product purchased will be the product's name and item number, so create two fields named item_name and item_number with text data types.

The PayPal system does not provide individual product price information via IPN. To overcome this limitation, you must query an item's price from another table [Hack #73] and calculate the price for the item based on the item_number passed by the IPN system.


Name the new table tblOrderDetails and save the database. It is now ready to have information inserted into it by your IPN script.

7.23.2 Single-Item Purchases IPN Page

Because you're looking for the item_name and item_number variables, you need to create two new temporary variables to hold these values. Also, you need to capture the transaction ID so that you can query your database later for the information regarding a specific transaction. Create and populate the variables with the following code:

Dim item_name, item_number, txn_id

Item_name = Request.Form("item_name")

Item_Number = Request.Form("item_number")

Txn_id = Request.Form("txn_id")

Next, execute this SQL statement to insert these values into the database:

INSERT INTO tblOrderDetails (item_name, item_number, txn_id) VALUES 

                ('" & item_name & "', '" & item_number & "', '" & txn_id & "')

Once the script is activated, the values passed back for any transaction are inserted into your tblOrderDetails database table.

7.23.3 A Shopping Cart IPN

Since Shopping Carts pass one or more products for any single transaction, you need to check the IPN data for the item name and number of each product. First, use the num_cart_items variable to find out how many items the customer purchased. Create a local variable to hold the number of cart items and populate it with the following code:

'Get number of cart items purchased

Dim num_cart_items

Num_car_items = Request.Form("num_cart_items")

For Shopping Cart transactions, the item_name and item_number variables are appended with their corresponding cart item count. To get the value of the first item in the cart, examine item_name1; the name of the third item in the cart (if it exists) is stored in item_name3. Using the item_namei or item_numberi format, where the i is the cart item count, you can get the values for all the items in the cart.

Use a For loop in your IPN script to iterate through all the products your customer purchased, inserting the information about each into your database as you go.

'Get number of cart items purchased

Dim num_cart_items

Num_car_items = Request.Form("num_cart_items")



'Create new count variable

Dim i



For i=1 to num_cart_items



  set cInsDetails = Server.CreateObject("ADODB.Command")

  cInsDetails.ActiveConnection = "DRIVER={Microsoft Access Driver 

                (*.mdb)};DBQ="C:/InetPub/wwwroot/database/dbPayPal.mdb")

  cInsDetails.CommandText = "INSERT INTO tblOrderDetails (item_name, 

                item_number, txn_id) VALUES ('" & Request.Form(item_name & i) & 

                "', '" & Request.Form(item_number & i) & "', '" & txn_id & "')"

  cInsDetails.CommandType = 1

  cInsDetails.CommandTimeout = 0

  cInsDetails.Prepared = true

  cInsDetails.Execute( )



Next

Note that the transaction ID variable remains the same, regardless of what cart item you are on, because all the items were purchased as part of the same transaction.

    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