O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
PayPal Hacks
By Shannon Sofield, Dave Nielsen, Dave Burchell
September 2004
More Info

HACK
#54
Create a Dynamic Storefront
Produce a powerful storefront with a simple database and dynamic server scripting
[Discuss (4) | Link to this hack]

PayPal's Button Factory makes managing a small web store easy, provided that you have a small number of products. But if your store has hundreds or thousands of products, generating the necessary HTML code through the Button Factory (not to mention later changing that code) would be a daunting task. Therefore, you'll need a method to quickly generate generic shopping cart HTML button code for all your store's products.

This hack provides an ideal situation for a database-driven page that can use a single page as a template for an arbitrary number of products contained in a database. The example illustrates the techniques using Microsoft Active Server Pages written in VBScript with an Access database, though the principles described here can be applied to any server platform/database combination.

Building the Template

The second step in creating your storefront is to generate generic HTML Button Factory code to serve as your template for your database-driven store. Your button code should look something like this:

<form target="paypal" action=
                "https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="business" value="yyouremail@yyourisp.com">
<input type="hidden" name="item_name" value="Widget">
<input type="hidden" name="item_number" value="Wid-001">
<input type="hidden" name="amount" value="1.00">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src=
                "https://www.paypal.com/en_US/i/btn/x-click-but22.gif" border="0"
                name="submit">
<input type="hidden" name="add" value="1">
</form>

The storefront page displays all the items for sale by taking the information in your tblProducts database table and dynamically inserting it into the generic PayPal Button Factory code you just created. To get started, use a SQL query to retrieve the product information. Depending on the server platform, languages supported, and database technology used, the syntax to connect to the database and return the data will vary. The SQL query to create your recordset should look like this:

SELECT item_name, item_number, item_price, Id FROM tblProducts

TIP

See the "Database Coding and Platform Choices" section of the Preface for the additional information needed to put this SQL statement to work with this and the other hacks in this book.

Your database then returns all of the products in the table, which you'll need to place into a recordset called rsProducts.

Next, take the generic button code from the previous step and replace the field values with references to fields in your database. For instance, change this line:

<input type="hidden" name="item_name" value="Widget">

to this (assuming you're using VBScript for ASP, as discussed in the Preface):

<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">

Your final code should look something like this:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"
                method="post">
<input type="hidden" name="business" value="yyouremail@yyourisp.com">
<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">
<input type="hidden" name="item_number" value="<%=rsProducts
                ("item_number")%>">
<input type="hidden" name="amount" value="<%=rsProducts("item_price")%>">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif"
                border="0" name="submit">
<input type="hidden" name="add" value="1">
</form>

When this page is loaded into a web browser, your server executes the SQL query before it is presented to the customer. The code then pulls the first item from the recordset and generates the button code for the corresponding product dynamically. The next step is to generate a whole page of buttons, one for each item in your database:

'While recordset still has products, loop code
While NOT rsProducts.EOF

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr"
                method="post">
<input type="hidden" name="business" value="yyouremail@yyourisp.com">
<input type="hidden" name="item_name" value="<%=rsProducts("item_name")%>">
<input type="hidden" name="item_number" 
                value="<%=rsProducts("item_number")%>">
<input type="hidden" name="amount" value="<%=rsProducts("item_price")%>">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif"
                border="0" name="submit">
<input type="hidden" name="add" value="1">
</form>

'Move to next record
rsProducts.MoveNext( )
Wend

Figure 2 shows the finished product listing, complete with multiple dynamically generated payment buttons.

Figure 2. The finished web page, loaded into a browser


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.