Hack 69 Use IPN with eBay Listings

figs/moderate.gif figs/hack69.gif

Include additional variables with auction payments to help fortify the connection between eBay and your PayPal transaction history.

When the IPN system is activated for auction payments, your IPN script receives a form post with the transaction information. If you have your IPN profile preferences set to On, your processing script always gets hit when a payment is made, even for auction payments.

In order to process posts for auctions, your IPN script needs to be able to recognize that the payment is being made for an auction and adjust accordingly. In some cases, you might want to process only certain sections of code for auctions, or you might want to omit certain sections of your IPN page in the case of auction payments. There are five additional variables you need to watch for while dealing with IPN pages that can potentially receive posts for auction payments: item_number, auction_buyer_id, auction_closing_date, and auction_multi_item, and for_auction.

The item_number variable, which is normally populated with your user-defined unique ID-tracking value, is sent populated with the auction number. If these new variables are not accounted for, or you do not have a way of dealing with the item_number value as passed back by auctions, you might have a problem with your entire system.

7.9.1 The Code

This hack shows you how to set up your IPN script to look for an auction payment, process one block of code for an auction payment, and process another section of code for nonauction payments. This example illustrates how to insert the auction buyer ID, the auction number, the auction closing date, and the multi-item counter variable for the auction into the separate database table tblAuctions.

'Process payment

If Request.Form(for_auction) = "true" then 'Auction payment received

 'Insert into tblAuctions table

 'Create and populate auction variables

 Dim auction_id, auction_buyer_id, auction_closing_date, auction_multi_item

 auction_id = Request.Form("item_number")

 auction_buyer_id = Request.Form("auction_buyer_id")

 auction_closing_date = Request.Form("auction_closing_date")

 auction_multi_item = Request.Form("auction_multi_item")

 'Database connection info here

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

 cInsAuction.ActiveConnection = "DRIVER={Microsoft Access Driver (*.mdb)};


 cInsAuction.CommandText = "INSERT INTO tblAuctions (auction_id, 

                auction_buyer_id, auction_closing_date, auction_multi_item) VALUES 

                ('" & auction_id & "', '" & auction_buyer_id & "', '" & 

                auction_closing_date & "', '" & auction_multi_item & "')"

 cInsAuction.CommandType = 1

 cInsAuction.CommandTimeout = 0

 cInsAuction.Prepared = true

 cInsAuction.Execute( )

End If

If for_auction <> "true" Then 'Normal payment

 'Create and populate normal variables

 Dim item_number

 Item_number = Request.Form("item_number")

 'Normal payment code here

End If

7.9.2 The Results

When you place the code in your IPN processing page, it enables your script to handle payment calls for both auctions and normal Web Accept payments. If you did not build this type of functionality into your script, your system might not function properly, because the item_number variable is populated with different information in the case of an auction.

The first section uses an If/Then statement to determine whether the post is being made for an auction. The for_auction variable lets your page determine whether this is the case. If the variable has a value of 1, the payment is for an auction, and the code uses the aforementioned additional variables (including the modified item_number variable) to make a database insertion into a table created to track auction payments. If the payment is not for an auction, the for_auction variable has a value of 0 and the section of code is not activated.

The second block of code does the exact opposite of the first section. It checks to see if the for_auction variable has a value other than 1 (true). If it has a value other than 1, the code block that handles the processing of your normal payments is activated. You should place all of your normal transaction processing code in this section.