Recently I wanted to sell a small script for which I was getting some requests.I wanted the procedure to be as efficient as possible where the user was delivered the script though a secret link once the payment was made. After some thorough searching around the net , I found that though the Idea and procedure is so simple there exist No Free scripts
to do so ( Weired ). So in this tutorial we build a small script which can be used anywhere to sell digital goods online using Paypal IPN.They can be ebooks, shareware , software, images anything Bits and Bytes.
The script has the following features
- Complete automation of your online orders.
- Automatically emails your customers and provides them with a unique download link to your digital product.
- Automatically expiring links as required by the user.
- File Attachments in Email.
- Easy to test and debug.
- Easy to integrate into any existing website.
- Easy to install and configure. Only one settings file.
Downloads: 3,761 File Size: 25.2 KiB
IPN is what this system is designed around.Instant Payment
Notification (IPN) allows you to integrate PayPal payments with your
website’s back-end operations. IPN provides immediate notification and
confirmation of PayPal payments you receive. Here is how IPN works

- A customer payment or a refund triggers IPN. This payment can be
via Website Payments Standard FORMs or via the PayPal Web Services
API’s for Express Checkout, Mass Pay, or Refund Transaction. If the
payment has a “Pending” status, you receive another IPN when the
payment clears, fails, or is denied.
- PayPal posts HTML FORM variables to a program at a URL you specify.
You can specify this URL either in your Profile or with the notify_url
variable on each transaction. This post is the heart of IPN. Included
in the notification is the customer’s payment information (such as
customer name, payment amount). All possible variables in IPN posts are
detailed in this guide. When your server receives a notification, it
must process the incoming data.
- Your server must then validate the notification to ensure that it
is legitimate.Once this is verified you can go ahead and deliver the
goods with a link which expires after sometime to the payer.
To activate IPN, you can either change a setting in your PayPal
Profile or include the notify_url variable in the payment FORMs on your
website. We will us the notify_url method.
Also as we will not be using any encryption in the buttons we will
verify all the IPN details during the verification procedure.
Before you begin this tutorial please download the source and refer to it side by side for better understanding.
Step 1 : Setting up to receive and validate IPN
request . We are going to use one of the many Paypal Classes available
freely, I have taken one from micah carrick
.This class handles the request which Paypal send back to us.So the
validation code which will be called once the Paypal invokes our IPN.
validate_ipn() is the function which handles this part.
Step 2 : Now we want do define the products which
we want to sell , As my aim was to sell 1 - 2 odd scripts there was no
use of building a shopping cart. Also as i planned to skip the database
, and keep things simple as possible. So my products listing is a
simple array defined in the settings.php file. The array item index is
the item number which is used when creating a button.
// product[number] = array(’Name’ ,’Price’ , ‘Download Link’);
$products[2] = array(’My Script’,’32′,’downloads/myscript.zip’);
Step 3 : As we will be using non encrypted buttons
, users might try to fool the script by sending in fake IPN requests or
paying different amount and invoking the IPN script. To prevent such
kind of things from happening we put in a validation function which
checks various parameters according to our defined products. This
function is validate_product().
Step 4 : Now we need a Buy now button for our
product which defines our IPN callback, Item number , price etc. This
button is simple HTML and can be placed anywhere and all the user is
required is to click it and pay up , rest the script will take care of.
<form action=”https://www.sandbox.paypal.com/cgi-bin/webscr” method=”post”>
<input type=”hidden” name=”cmd” value=”_xclick”>
<input type=”hidden” name=”business” value=”mail@mybusiness.com”>
<input type=”hidden” name=”item_name” value=”My Ebook”>
<input type=”hidden” name=”item_number” value=”2″>
<input type=”hidden” name=”amount” value=”32.00″>
<input type=”hidden” name=”rm” value=”2″>
<input type=”hidden” name=”return” value=”http://www.mybusiness.com/thankyou.html”>
<input type=”hidden” name=”cancel_return” value=”http://www.mybusiness.com/”>
<input type=”hidden” name=”notify_url” value=”http://www.mybusiness.com/paypal/paypal.php”>
<input type=”image” src=”https://www.paypal.com/en_US/i/btn/x-click-but23.gif” border=”0″ name=”submit” alt=”Make
payments with PayPal - it’s fast, free and secure!”>
<img alt=”” border=”0″ src=”https://www.paypal.com/en_US/i/scr/pixel.gif” width=”1″ height=”1″>
</form>
Step 5 : Now if everything goes fine and user pays
up we need to send him a secret link which expires at our pre decided
time( say 24 hours ). Now this procedure will only takes place if the
IPN return a verified value. The secret link just contains a timestamp
and a product ID which is encrypted using RC4 encryption and then base
64 Encoded so that it can be used in a URL. Then a email is sent to the
user with the Download link.
// Client has successfully paid for the product
$product_id = $p->ipn_data[’item_number’];
$download = $product_id.’|’.time();
$download_link = $script_location.’download.php?file=’.base64_encode(RC4Crypt::encrypt($secret,$download));
Step 6 : Now we need a script which takes this link
and validates it and gives the user the download if everything goes
fine. download.php is the file which does this.
That’s it , you have a set of 2 scripts which will allow you to sell downloads online.
The package also includes a check.php which helps you check if Digisell is functioning properly. Run it after you have editing the settings file.