Paypal IPN Script
I have already discussed the issues of Paypal Security and why you need an IPN script if you are selling items that you want to make available for your visitors to download (after they pay). On that page I mentioned how I have created an IPN script that allows me to have the purchased script automatically emailed to the buyer once their payment is accepted.
Well, now you can purchase a copy of that script to secure your own Paypal sales.
Will it run on my server?
The script uses code from my Advanced Form to Email Script to send the purchased item so your server will need to support the use of that script. The free version of my email script has the same requirements so if my free email script will run on your site then the Paypal IPN script will as well.
Configuration
This script consists of nine files called paypal1.php (or paypal.php), paypal2.php, paypal3.php, payconf.php, payinc.php, payprod.php, paycust, ptop.php, and pbottom.php. The first three of these call the fourth which defines how you want the script to work. To configure the script to work on your site you just update the values in payconf.php to suit your requirements and customize the ptop.php and pbottom.php files to give the thankyou page a similar appearance to the other pages on your site. There is no need to alter the first three files or payinc.php and payprod (which are used by the first three files to apply any valid discounts). You may modify paycust.php as required to add your own custom processing. An example paycust that logs purchases to a database is also included.
The payconf.php file defines the following fields which control how your IPN script will work:
- $ipt determines if the ip address is to be tested for one known to be used by Paypal.
- $debug set to 1 to send you an email when the IPN script and thank you page are accessed letting you know what values were passed to those pages so that you can check that the script is working correctly. Set this to 0 to stop the debugging emails being sent once you are happy with the functioning of the script.
- $sandbox can be set to 1 to make use of the Paypal sandbox to test your custom processing in paycust.php or 0 to run live.
- $qmail set to 1 if you are using qmail (or another mail program that uses \n instead of the standard \r\n on the end of header lines) otherwise set to 0 to get standard header separators on the emails.
- $site the address of your web site
- $premail the email address that you use for your paypal account
- $pa[n] array contains one entry for each product that you are selling. Each entry contains a comma separated list of the following:
- product code
- currency (usually USD)
- minimum acceptable price
- relative location of the file to be emailed (leave blank for items to be physically shipped)
- Description of the product being purchased
- Page to display if purchase is cancelled
- $ds[n] an optional array containing discounts offered based on codewords entered. Each entry contains a comma separated list of the following:
- code word
- product code
- discount amount (to be deducted from min price)
- start date (in ccyymmdd format)
- end date (in ccyymmdd format)
If you don't intend to offer discounts based on codewords then you don't need to set up this array. - $sub subject to use on the emails sent by the script
- $template a template for the text of the email that will be sent with the file attached.
- $template1 a template for the text of the email that will be sent if there is no file to be attached.
- template2 and template3 are the equivalent shopping cart texts.
- $pc[0] - $pc[10] are templates for the eleven alternate texts that can be displayed on the thank you page. These cover
- Payment accepted, product sent as email attachment
- Payment accepted, product to be physically shipped
- Insufficient funds received, the purchaser has reduced the amount to be paid to below the minimum price that you will accept for purchase of the product
- Donation received with thanks
- Donation received for specific product
- Payment received by eCheck, product will be emailed once payment clears
- Problem with payment (other than underpay)
- Payment accepted, sent as attachment (cart)
- Payment accepted, physically shipped (cart)
- Insufficient funds (cart)
- Payment received by echeck (cart)
The paycust.php file defines the following four functions that you can use to add your own custom processing at the indicated places in the processing sequence.
- startProcess() is called by paypal1.php (or paypal.php) and the Paypal shopping cart is not being used.
- pendProcess() is called by paypal2.php (the IPN script itself) whenever a payment is made using echeck (which therefore has a pending status). If using the shopping cart it is called once for each item in the cart.
- validProcess() is called by paypal2.php when a valid payment has been received and the product has been emailed (or is ready to be physically shipped). If using the shopping cart it is called once for each item in the cart.
- endProcess() is always called by paypal3.php. If using the shopping cart it is called once for each item in the cart. Note that this may be called prior to validProcess - eg. when payment is by echeck.
To use the script
First customize the ptop.php and pbottom.php files to give the thankyou page a similar appearance to the other pages on your site. What you place in these two files will "wrap" around the custom message produced for the thankyou page that will advise the purchaser of the status of their purchase (as defined in the $pc array). Update the payconf.php and paycust.php files to configure for your requirements (as described above) and then upload all of the supplied php files to your site.
Next you need to logon to your Paypal Account and update your paypal configuration to use the paypal2.php file as an IPN script. To do this for a Business or Premier Account go to My Account and select the Profile tab. The Instant Payment Notification Preferences link is in the Selling Preferences column. Alternatively, go to the Merchant Tools page and scroll down to the Other Paypal Tools section where you will find Instant Payment Notification as the first option. (this way works for any type of account). Set IPN on and supply the URL of the paypal2.php file on your site. You should also go into the Website Payment Preferences and make sure that Auto Return is turned off.
Finally you need to change all of your calls to paypal from your web pages to replace them with calls to paypal1.php?prod=product (where product identifies what is being purchased). If the product code is in the $pa array then the appropriate parameters will be generated from the table to generate a call to Paypal to purchase that product for the specified price. If the product is not in the table then appropriate parameters will be passed to Paypal to allow a donation for the specified product to be made.
Alternative codes for offering discounts and for calling the Paypal Shopping Cart are provided in buy.htm. You just need to copy the required version and paste it code into your page and change "product1" to the product code defined in the arrays for this product.
You can offer a discount price (eg. $1.00 off) in specific advertising by supplying a special discount code that readers of that advertising can enter into the form in order to obtain the discount. Anyone not seeing the ad will not know the discount code and will therefore have to pay the full price for the product. Discount codes are date sensitive so that you don't have to keep updating the discount array in payconf.php every time that you want to end a discount offer.
How it works
When your visitor selects the link on your page that calls paypal1.php (or paypal.php) the passed product code is looked up in the $pa array and the parameters for the Paypal call are generated. If a discount array is supplied and a discount code is entered then that code and product code are also looked up in the $ds array to see if there is a valid discount that is applicable on the current date, if so then the discount amount will be deducted from the price. The parameters will display in the address bar of the Paypal screen giving paypal3.php as the thank you page.
After the visitor enters their payment details in Paypal the paypal2.php script will be called to validate the payment. If the page is called from somewhere other than Paypal or if the receiver_email doesn't match then the payment is invalid. If the payment amount is below that specified then insufficient payment has been made. If sufficient valid payment has been made and cleared and a file is specified in the $pa array for this product then that file will be emailed to the purchaser.
Finally Paypal will call the paypal3.php page to display an appropriate thank you page depending on what type of payment was being made and whether it was successful or not. (If this page doesn't look the way that you want, you can customize it completely by updating ptop.php, pbottom.php and the $pc array in payconf.php).
If at any time during the process the purchase is cancelled then the page specified as the last field in the $pa entry will be displayed or if there is no $pa entry for this purchase then your home page will display.
How to obtain the script
Unfortunately, I have to try to fund the running of this web site somehow and so I have decided to charge a nominal amount for server side scripts such as this one. Use the following form to purchase this script for just $34.80 USD (Australian residents price is $41.00 AUD including GST). All purchases include 12 months free support including all updates to the script released during that time. If you have a valid discount code then enter it to purchase this script at an even lower price otherwise just leave the discount field blank, select the currency to make your payment and then press the buy now button. The script will be automatically emailed to you as soon as Paypal verifies your payment
a Paid Basic Membership to elistSecret.
This is a members only web site that deals specifically with building your own mailing list of potential customers. The site also includes reseller rights to a number of products that you can sell to those customers with your new Paypal script. This membership normally sells for $97.00.
See the bonus.txt file included in your purchase for details on how to claim your free gift.
Before you purchase this script you should reread the above information to ensure that the script will work on your server. If you cancel at any point during the payment process, you will be returned to this page.
Please make sure that the email account that you supply to Paypal will accept emails with attached zip files from addresses at felgall.com as otherwise your spam filtering may delete your purchase.
The liability of Felgall Pty Ltd for any theft of files by anyone managing to successfully bypass this script is limited to the original purchase price of the script. As purchases are emailed to the purchaser (with a blind copy to you) in the unlikely event that someone manages to successfully bypass the security of this script you will receive this blind copy that lets you know that your file has been stolen as well as providing you with the email address of the thief allowing you to take appropriate action against them.
Felgall Pty Ltd accept no responsibility for any theft of files not directly related to this script.


