Spacer
Spacer

NGCODERS.COM

[ NEXT GENERATION CODERS ]

Spacer

Selling Digital Goods/Ebooks With Paypal IPN and PHP

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.

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

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.

275 Responses to “Selling Digital Goods/Ebooks With Paypal IPN and PHP”


  • Upload the check.php then go to the url http://www.yourdomain/check.php
    that’s all

    the check.php will also give you the right button code to paste on your “index.php” for example.

  • hello i am looking to use this script but i want to use it in conjuction with a database would you be able to help me to do this.

  • the script sends me (seller) an email but it won’t send the buyer an email.

    what could be the cause of that?

  • Ardyle, you are lucky, i don’t receive any mail except the one from paypal…..

    Can someone send me their working files with the SMTP method by email. Or post here their scirpt, i would really appreciate.

    You can contact me to contact@e-ziris.com
    Thx.

    (I’m still trying to figured out what’s wrong)

  • Update : The only way i receive email notification is when i choose http://www.mydomain.com/payal.php as my return page and not notify_url….

    If i do that, after the payment i click on “return” and get stuck to a blank page : paypal.php ( which send emails notification but nothing else)

    What’s wrong ?

  • Thank you, Octavio. In the settings.php file, I changed the notification statement to

    $notify_email = “{payer_email}”;

    then I ran a test and it worked. The buyer’s email received an email with a timed download link to my digital information product.

  • Little Update :

    For those who don’t receive email notification you can do like this :

    - setup the paypal.php file as the return page in paypal.com config (do not turn on automatic return on paypal or it won’t works)

    - in the paypal.php : make a forward to your thankyoupage.php right after the send mail function.

    this way, when the user click : return to your website after a payment, it will load paypal.php for 0.1sec then forward to the thank you page.

    (This is the only way for me to receive email notification on paypal live)

    Also I have receive a paypal IPN mail this morning about : “check IPN server status”…i don’t know if paypal has some trouble with their IPN atm…that would explain why it never load/use the notify_url ……

  • I have got it working now, but when i go to the return to website button on paypal instead of taking me to my thankyou page i get this message:

    PAGE CANNOT BE DISPLAYED ETC.

    HTTP Error 405 – The HTTP verb used to access this page is not allowed.
    Internet Information Services (IIS)

    Other than that the script is working, im receiving the download link fine. Could this be related to your problem rakhim? (by the way thanks for the reply further up mate)

  • Sorry, just realised my mistake with above post, had it as thankyou.htm not thankyou.php

    Sorted :)

  • Hey! Where did the script go? I get an error when I try to download it.

    Thanks

  • Great job with the script! Thank you for providing it for free!

  • Just want to say THANK YOU!!!!!!!!!!!! I can’t say it enough, this tut is a life saver!!!!

  • Hey,

    I have downloaded the ZIP.

    What should I do now?

  • Hi Michael,

    Create a new directory on your site, you can name it paypal or whatever you like. Extract the contents of the zip and paste/upload to new directory. Brian

  • @Idan – Commenting out that check worked perfectly to help me to use a non-default email with PayPal

    @Pweck – Setting the attachment to false worked for me.

    What a great script.

    As a note the emails all arrive fine for me but not from the test.php

  • How do I use the script for multiple products. I see in the script this line

    $products[2] = array(‘My Script’,'32.00′,’downloads/myscript.zip’);

    Do I simply add a new line under it like this?

    $products[3] = array(‘My Script 2′,’32.00′,’downloads/myscriptb.zip’);

    any help would be great

  • Sorry for wasting your time .. that is how its done ..

    thanks for the great script!!

  • I get no attachment and no download link after payment.. Whats wrong?

    Is there something else that I have to do, except for activating the IPN in my Paypal profile, and changing the variables in the ’settings.php’ file?

    Anyone who can help me will really save my day!! Thanx!!

  • Great Script .. wroks a treat..

    Ive been looking for a script like this for ages and now i get one FREE which is my favourite price!!

  • Hi Henrik, Have you used check.php and digisell_debug.log to look for errors? Brian

  • I’m trying to modify this script to update an entry in a database just before it mails the successful purchase message. However no change to the database occurs.. I’ve checked the check.php and get this message.

    Checking product ID [2] is accessible :Failed

    Where did I go wrong?

    Thanks,
    David

  • How would i get the download link to display on the thankyou page?

    When people have paid and are sent back to my site, instead of them having to look at an email (which sometimes for some reason dosnt work) I would like a link on the return page.

    any help any one ?

  • Hi,

    How can i input more than one custom field. For example $operating_system , $database , $office_automation , $multimedia.

  • Awesome script. Thanks so much!

  • I’m not getting the (customer) email either. The only time I can get the download link to send is when I have $notify_email set to an actual email addy. I tried setting it to ‘{payer_email}’ but that doesn’t work. Any thoughts?

  • Can someone advise on folder permissions. If I make the folder permission 755 everything works great, however the download directory is at risk and if someone is clever they will find it and download everything for free. The paypal directory and downloads are open to public just by adding http://www.mydomain.com/paypal to url. If I change permissions to 750 or 754 to hide the directory, the paypal notification works however no download link is provided to buyer.

    HELP! thanks and this script is by far one of the safest and best have used to date…

  • I am a bit of a noob when it comes to this stuff…can anyone help me get this scripts up and going? I’ll give whoever helps a free copy of my new e-book “A-Z Symptoms – Gone Forever Herbal List! 27 dollar value not yet released. ( That is why I need this skript) haah Thanks guys,

    Joel

  • Hey Joel, I’ll give you a hand.

    • 100kwatt,

      So far I have created a new directory on my website (PayPal) and uploaded the extracted zip files. I am a novice, so I really could use a step by step how to, both on my website and working with PayPal.

      Thanks,
      Joseph

  • This is the first time I commented here and I must say you share us genuine, and quality information for other bloggers! Good job.
    p.s. You have an awesome template . Where did you find it?

  • Hi, thank you at first for the script.
    I have done all the changes in the settings.php
    but when I then check the check.php I get this message:

    Checking product ID [2] is accessible :Failed

    I don’t know what I did wrong. Have looked so often and can’t find what I did wrong.

    Any help would be so appreciated. Please help when you have a moment.
    Thank you.

  • Hi, forget it.
    It was such a silly silly mistake. I am upset with myself.

    Where I need to write the download link there was a mistake.
    I wrote the whole path instead of only ‘downloads/….’.

  • I am again:
    The check.php works perfect. All is standing on “success” and I am getting a dummy email.

    I have included the button on my page and my husband bought my product, because I wanted to have a really thing to test.
    I am getting payment notification and my husband gets notification about payment from Paypal. That is all.

    But NO download link is delivered. I tried to change the notifiy-email to payer-mail as suggested, but this also didn’t work. I also set the attachment to “false” and this also doesn’t work. No email to my customer nor to me.

    I really could need some help, because I don’t know what the problem could be and how to resolve it.
    I so hope that someone can help me.
    Thank you.
    Doris

  • When doing debug_log I am getting this error:

    [03/16/2009 10:15 PM] – FAILURE :IPN prodcut validation failed.

    [03/16/2009 10:15 PM] – SUCCESS :P aypal class finished.

    What did I wrong?
    Doris

  • Hi Doris, Have you used paypal’s sandbox?

    https://developer.paypal.com/

    change settings.php sandbox=true

  • Hi 100kwatt, thank you. I used it and it worked. But still didn’t work in real life.
    I then figured out that I needed to give my Primary PayPal Address. I also have others, but only with the Primary Paypal Address it works.
    Hope this will help others too.

    It works fantastic. What a wonderful script. Thanks to Ngcoders!!!
    Doris

  • I have a problem with 1 server and it’s strange because the exact same files on another server works properly.

    here my problem: I’ve received like 15 times the same e-mail with the download, but i only bought it once. (server and mail server are on 1and1, i’m using SMTP to send the email with the download link.

    Again, on the other server with others e-mail adress and paypal account, this thing never happened.

  • Thank you for this wonderful resource, but I am encountering a problem when running ‘check.php’. Everything passes, with the exception of sending the dummy email, which persistently fails.

    I have carefully gone through the ’settings.php’ file, looking out for any mistakes, but everything appears to be in order.

    I have even tried replacing “{payer_email}” to actual working email addresses – but all to no avail?

  • Teentop links directory, link exchange, add url, add link

  • I am new to PayPal and php, and was wondering if when the customer bought several files on one order, there would be multiple download links in the email.

    Thank you for this script and thank you for your time!

    • Silly me. The script would generate multiple emails.

      Thank you for this script. I almost have it configured and ready to go. Also, right now I use express checkout. Could I use this same script with Website Payments Standard?

  • I just tried to test adding the item to the cart, but realized the button code generated forces you to buy just that item – no more, no less (unlike storing the item in a cart). How do I alter the button code to add the item to an Express Checkout cart? Do I use PayPal’s code and just add some of the tags (e.g. the rm tag?) This script is still wonderful and was able to send the dummy email from check.php. THANK YOU!!

  • Hello, I have read and reread every response. I’ve gotten some good ideas and tips. But can’t get Success on a product…

    Here’s what I’ve done so far:

    1) downloaded script files
    2) put them into paypal folder
    3) changed url for script information
    4) created a test product in paypal for .01 cents
    5) copied button code onto product page
    6) took the Number from the button and applied it to this section // product[number]

    7) filled in = array(‘Name’ ,’Price’ , ‘Download Link’);

    Not sure what array (‘Name’) should be? and not sure if this needs to match something from the Paypal button code.

    8) I’ve run check.php

    I can not get success for my product. Everything else shows successful.

    The log document comes up blank always. changed it to True

    I did not touch the email, download, Paypal .php files – should I????

    This is obviously great script, and I obviously don’t have a clue what I’m doing. This is soooo over my head. I’ve been doing it for about 4 hours now and haven’t gotten past the product section.

    Thanks,
    Cookie

  • Check out brantonsoftware(dot)com for a working example of this software. The test is only one dollar. In the settings.php file, I changed the notification statement to
    $notify_email = “{payer_email}”;

  • I’m a noob. We’re all noobs. Some people just have a higher noob index than others. It’s no big deal.

    Some people may not know this little program convention. but when you see // at the start of a line of code, it basically tells the “machine” to ignore what comes after the // in that line. The content after // is only there for you, to explain what is going on or how something should work.

    Hence // product[number] = array(’Name’ ,’Price’ , ‘Download Link’);

    tells you that the product number is equal to three variables called Name, Price and Download Link.

    Conversely, the three variables called Name, Price and Download Link equal the product number.

    $products[2] = array(’My File 2’,’2.00′,’downloads/file2.zip’);
    $products[3] = array(’My File 3’,’3.00′,’downloads/file3.zip’);
    $products[4] = array(’My File 4’,’4.00′,’downloads/file4.zip’);

    So the buttons for products 2,3 and 4 need to match up.

    For example, button 2 should have these variables in it:

    and so forth, and so on..

  • Hi

    Thanks for this script which was a breeze to install and does exactly what it says on the box!

    I have been trying to allow the product array in settings.php to be echo-ed in from a database value. I have created an include file which echos the array exactly as it appears in settings.php

    But when I run check.php it doesn’t like it! Any hints as to why this might be happening?

    Many thanks

  • Hello,

    Everybody who replied to my question. Thank you. I got it figured out with bits and pieces of every ones logged replies. My issue was that I was including too much of the Download Link address, paypal/downloads/mylink.jpg, which I found reference to on some older comments. Once I deleted paypal/ it worked.

    To Branton… Too funny, I didn’t mean I was filling out between the // comments // I meant I filled out the correct section below that for ’Name’ ,’Price’ , ’Download Link’. Sorry, if that didn’t read well… Thank you for taking the time with such a great explanation.

    I certainly appreciate your help and wisdom.

    And the GREAT script!

    Sincerely,
    Cookie

  • Hello,

    I would like to ask if and how i can replace the 24 hour timer with a maximum download counter ?

    so a file can only be downloaded 3 times.

    Best Regards

  • Any news on how to get this to work on a shopping cart… Can it work? I’m a musician with 3 CD’s that have 12 songs each and I want to offer the songs individually for $0.99 similar to itunes… Can I do it with this?

  • It won’t work if you are trying to use this script as a shopping cart. It’s not a shopping cart script; however, that doesn’t mean you can’t figure out a way to make it work. There is no reason why you can’t license-for-personal-use copies of the songs individually and deliver the product to your customer.

  • How do I protect the download directory? I noticed I can just type the full url path and it downloads the file. Not cool. Help is appreciated.

  • Hi am so Frustrated with this.
    Im now to PHP & Scripting but have webdesign knowlage from 1997 but his has me beat.
    I now downloaded this script from 2 different places and it is vertually the same code.

    I can get nothing or have had nothing working and I have been at it for over a week now most nights until 1am and to be honest I cant say anything good about it other than its a good idea.

    1. I can send the Email and Pay for it through Paypal, No Problem but the customer doesnt get their download attachment.
    a. Is it possible to limit the number of downloads rather than the time delay.

    2. The Security of the Downloads folder – The only way I could get the file to download was to put in the URL of the downloadable file.

    What am i doing wrong please can anyone help.
    Robert

Leave a Reply