The process flow and purpose of this script can be summarized with a flow chart, shown below:
The important thing to note is that the moment the customer lands on the download page in your website, the IPN has already been successfully processed by the IPN handler script (ipnhandler.php) and inserted into the customer records table. All the download script needs to do is retrieve and validate the user input invoice number and IP address records, and then compare that with the IPN data saved in the customer records database table to make sure it belongs to a valid, paid-up customer. The project's complete source code will be available for downloading at the end of this tutorial series (in part six, the next part). It covers all of the scripts discussed in parts one through five. It also illustrates in detail how to implement this project on your own test server, using your own server-specific information and PayPal sandbox test accounts. The PHP script: customerdownload.php Below is the complete, working script for customerdownload.php. It will be able to execute the process as stated in the flow chart. <?php //Check if the customer download form is submitted if (!isset($_POST['invoicenumber'])) //Form is not submitted, show the web form to the customer ?> <!--Retrieve the user IP address, then assign it to the visitor IP field--> <input type="hidden" name="visitorip" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>"> <!--Show recaptcha to the user--> <?php //You need a recaptcha account to have your own public and private keys require_once('recaptchalib.php'); //Web form submitted, validate the recaptcha first. require_once('recaptchalib.php'); //Recaptcha validation succeeded, process the customer request $invoicenumber = trim($_POST['invoicenumber']); //Retrieve the posted IP address from the web form $visitoripaddress = trim($_POST['visitorip']); //Initialize errors array, this will contain any errors found during the validation. //Validate the invoice number submitted if (empty($invoicenumber)) { //Check if the invoice number is alpha-numeric. if (!(ctype_alnum($invoicenumber))){ //Check if the IP address is valid if (!(filter_var($visitoripaddress, FILTER_VALIDATE_IP))){ //If there are any errors found during the validation, return it to the user. if (sizeof($errors) > 0) //There are no errors found in the validation, connect to MySQL database include '/home/www/php-developer.org/paypal_ipn_demo/connect.php'; //Sanitize the invoice number variable in preparation for a MySQL query. $invoicenumber = mysql_real_escape_string($invoicenumber); //check if the invoice number exists in the customer records database table if ($fetch = mysql_fetch_array( mysql_query("SELECT `InvoiceNumber` FROM `customerrecords` WHERE `InvoiceNumber`='$invoicenumber'"))) { //Invoice number records found, check if the customer has already downloaded the ebook //You should only allow downloading for customers who have not yet claimed their digital product purchase. //Retrieve the download status of this transaction based on the given invoice number. //Check also if the payment status is COMPLETED. This is very important; you should not allow downloading to an incomplete/unpaid transaction. //Retrieve the payment status associated with the invoice number in the customer records table. $result2 = mysql_query("SELECT `PaymentStatus` FROM `customerrecords` WHERE `InvoiceNumber`='$invoicenumber'") //Check if the IP address of the customer downloading is an IP address of a valid customer. $result3 = mysql_query("SELECT `IPAddress` FROM `customerrecords` WHERE `InvoiceNumber`='$invoicenumber'") //Finally, once all of the important data is retrieved, check if the download status is incomplete, payment status is complete and the IP address is valid. if (($downloadstatus=='incomplete') && ($paymentstatus=='Completed') && ($validipaddress==$visitoripaddress)) { //The transaction is valid.
//Now retrieve the product filename associated with that digital product $productpurchased = mysql_real_escape_string($productpurchased); //Force download the ebook to the customer header("Content-type:application/pdf"); //Now that the customer has completed the downloading, update its records in the customer records table. $updateddownloadstatus='completed'; //Invalid transaction because of one of the reasons stated below: echo 'Sorry but the downloading process are denied because your transaction happens to be:<br />'; //Invalid transaction because the invoice number submitted by the user is not found in the database. echo 'Sorry but the Invoice Number is an invalid Paypal Transaction.';
blog comments powered by Disqus |
|
|
|
|
|
|
|