Beruflich Dokumente
Kultur Dokumente
0
Release Date: 2/14/2005
with PHP, MySQL, and PayPal
By Phillip Perkins
Takeaway
Adding e-commerce capabilities to your existing Web site does not necessarily require extensive
development time. This download shows you one way to add commercial functionality without a major
allocation of scarce resources.
This article was originally published on Builder.com in the February 3, 2004, Web Development Zone
newsletter, and was written by Phillip Perkins. Keep your developer skills sharp by automatically signing up
for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.
Table of Contents
CREATE A SITE FOR E-COMMERCE..........................................................................................................................................................2
SETTING UP THE SITE .........................................................................................................................................................................................2
GETTING DONATIONS (PAYMENTS) VIA PAYPAL................................................................................................................................................2
CREATING THE SITE ...........................................................................................................................................................................................2
Listing A .......................................................................................................................................................................................................3
Listing B .......................................................................................................................................................................................................4
DESCRIPTION .....................................................................................................................................................................................................7
VERSION HISTORY .............................................................................................................................................................................................7
1.0............................................................................................................................................................................................................7
o Bug fixes .............................................................................................................................................................................................7
o Feature changes .................................................................................................................................................................................7
TECHREPUBLIC SITE FEATURES .........................................................................................................................................................................7
The first page will collect the user information (i.e., first names, last names, e-mail addresses, and the
amount they plan to donate, along with optional fields for addresses and phone numbers) so that I can
store it on MySQL.
The next page will store the user information into a MySQL table and create another page that will forward
them to PayPal. This page will provide a text <input> for the donation amount. The necessary information
is then forwarded to PayPal. (Listing A)
Listing A
<html>
<body>
<?php
$connection = mysql_connect("localhost", " dbuser", "dbuserpass");
mysql_select_db("PRIMARY");
$result = mysql_query("SELECT * FROM GUEST_LIST WHERE email = '" .
$_POST["email"] . "'");
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
echo "This email address has already been registered.<br>";
} else {
$sql = "INSERT INTO GUEST_LIST (first_name, last_name, email) VALUES ('" .
$_POST["fname"] . "', '" . $_POST["lname"] . "', '" . $_POST["email"] . "')";
$result = mysql_query($sql) or die("Cannot do this query: " .
mysql_error());
if (mysql_affected_rows() > 0) {
$result = mysql_query("SELECT LAST_INSERT_ID()");
$row = mysql_fetch_array($result);
$guest_id = $row[0];
?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="myemail@mysite.com">
<input type="hidden" name="custom" value="<?= $guest_id ?>">
<input type="hidden" name="return" value="http://www.mysite.com/thankyou.php">
<input type="hidden" name="item_name" value="Bill's Party">
Donation Amount:
$<input type="text" name="amount">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cancel_return"
value="http://www.mysite.com/cancel.php">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="tax" value="0">
<input type="image" src=https://www.paypal.com/en_US/i/btn/x-click-but04.gif
name="submit">
</form>
<?php
} else {
echo "There was an error writing to the database.";
}
}
mysql_close($connection);
?>
</body>
</html>
First, the e-mail address is checked to see if it's already registered. If it is, the page simply outputs that it's
been registered. If not, the data is stored, the auto ID generated by the table is returned, and a form is
produced that points to PayPal. The form values that PayPal expects are all included for the donation. The
custom form element is used to store the auto ID so that when PayPal returns the donation information,
this value can be used to coordinate the donation amounts. PayPal will return the information to the
thankyou.php page as POST data. This is denoted by the rm form element set to 2.
The user enters the donation amount and is taken to PayPal, where he logs in and signs up (if necessary).
After a successful payment, the user is directed to thankyou.php. You can use this page to update the
payment information on the local database but, since I'm going to be using PayPal's Instant Payment
Notification (IPN), I'll just present the user with a simple thank you and a link back to the home page. This
is the same for the cancel page.
When a payment is made successfully, PayPal's IPN will send a notification to another page called
paypalipn.php, which is set up in the Profile area of your PayPal account. And when a payment notification
is received, I want to update my database to confirm the donation amount. Creating the code doesn't take
much work because PayPal's site provides sample code. I've taken the sample code and added a little of
my own custom code as you can see in Listing B.
Listing B
<?php
// read the post from PayPal system and add 'cmd'
set_error_handler("errorHandler");
$header = "";
$connection = mysql_connect("localhost", "dbuser", "dbuserpass");
mysql_select_db ("PRIMARY");
$req = 'cmd=_notify-validate';
error_log($d['mon']."/".$d['mday']."/".$d['year']."T".$d['hours'].":".$d['minutes'].":".$d['seconds']."
Error:
$errno in $errfile\nLine:$errline $errstr \n\n", 3, realpath("errlog.log"));
mail("phillip@phillipweb.com", "Error In $errfile", "Error: $errno\n$errstr\nin
$errfile\nLine:$errline");
exit;
}
?>
Also, when I receive the payment notification, I must send all the information back to PayPal to verify the
payment as a security measure. PayPal will respond with either VERIFIED or INVALID. If the payment is
verified, you can trust that the payment notification you received is authentic. Once the payment is verified,
I place an entry in my PAYPAL table for the guest_id. (Any database errors are written to a paypal.log log
file.) I can then use the amount information to display a total amount donated on another Web page.
If you'd like more information on the tools PayPal offers, then visit the site and look under the Merchant
Tools tab. (For some functionality, such as the button wizard, you need to log in.)
Description
The techniques outlined in this download show how to implement the basic principles of commerce using a
distinct method and programming language. The method has advantages and disadvantages, but it will
achieve the basic goal of recording a transaction. Using the lessons learned in this download will provide
you a platform from which you can launch your Web-based e-commerce application development project.
Version history
1.0
o Bug fixes
o Feature changes
TechRepublic communities engage IT professionals in the ultimate peer-to-peer experience, providing
actionable information, tools, and services to help members get their jobs done. TechRepublic serves the
needs of the professionals representing all segments of the IT industry, offering information and tools for IT
decision support and professional advice by job function.