Spacer
Spacer

NGCODERS.COM

[ NEXT GENERATION CODERS ]

Spacer


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.


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

  1. 1 Viper

    Nice script, However everything is working except for the download link part! Any thoughts of why?

  2. 2 admin

    Its working in both IE and FF , no idea why not working for you .

  3. 3 Gray

    First of all thank you so much for providing this script for free, it’s great.
    Is there a way to test this in sandbox? i.e. to test the purchase of downloads and sending the download link.

    Also is there anyway to intergrate the script to write the ipn to a database aswell?

    Thanks

    Gray

  4. 4 Viper

    Is there some directory that I need to create for the downloads! And if so where is the configeration setting for the name and location of the directory? Also what do I need to name the files that I want to be downloadable? Can you give me an example!

    Thank you again!

  5. 5 admin

    @ Gray - Yes you can test it just change paypal url in sandbox , in settings just set $sandbox = true;

    @viper - Please look at the settings.php included it should answer all your questions.

  6. 6 tim

    and from another great script..thank you, finally something for ipn downloadables that has a download link and can be setup and checked..althought for some reason i cant get the check.php to send a dummy email to my email..everything else has the green light..any thoughts?

  7. 7 Viper

    I checked the settings everything is correct but i don’t recieve an email with the download link. the only thing that I receive via email is a reciept from paypal?

    See Below!
    $products[1] = array(’noce’,'30′,’downloads/nocne.zip’);

    I ran the check and everything passed!

    my downloads are in the downloads folder. Um kinda lost on this!

  8. 8 Viper

    [08/23/2008 10:18 PM] - SUCCESS :Validating prodcut 1
    [08/23/2008 10:18 PM] - FAILURE :Invalid Reciver E-Mail :
    [08/23/2008 10:18 PM] - FAILURE :IPN prodcut validation failed.
    [08/23/2008 10:18 PM] - SUCCESS :Paypal class finished.

  9. 9 admin

    @tim - Please check email not landing in Junk , Can you send email using other scripts on ur hosting. Maybe php email not working.

    @Viper - According to error log validation is failing , is revciver ID And paypal ID same ? Also try sending email using check.php included … ull know if any problem is there with emailing system.

  10. 10 tim

    i dont really have any other php scripts to send from ..how can i check real quick? my mailto: links work

  11. 11 tim

    are you meaning port forwarding 25 as in the php.ini file?
    do i really need to do that? tried it and still got a failure ..
    if you are looking into it..rechecked all my email entry’s..
    still notta..

  12. 12 Nate

    Wow. Thank you! VERY much! This is exactly the kind of thing I was looking for! I have yet to try it, but looking over the code, its simple and concise, exactly what I needed!
    And I gotta agree…for something so simple, I’m surprised its not used elsewhere. Were I not so constrained on my own time, I’d have whipped something together eventually, but this is a HUGE help!

  13. 13 steve

    Hi,
    Thanks for providing us with something so useful!!

    To be fair, i havent tried it yet, i need to get home first, but i already noticed a few things that nobody seems to have commented:

    1) In step 4 you have:
    https://www.sandbox.paypal.com…...

    You should mention that this is for testing purposes! A few more newbies than me can miss that. Am i right?

    Related to it:
    for sandbox testing, do we need to do both changes:
    the above URL AND $sandbox = true; ??

    2) Your index.html is empty. Is this the “regular” sales page that should contain the button?

    3) Any complete solution that works to see in action?

    4) Can you add a Donations button here so people who use it can donate something for your work!

    Thanks!

  14. 14 admin

    @ tim : your issues are unrelated to the script . If you need consulting please contact in private. You can also set up sendmail/smtp using the script but its beyond the scope of article.

    @Steve : 1. Most people download the script which generates the buttons for them.
    2. the script usually goes into a directory so index.html is to prevent directory listing.
    3.I have it installed for a lot of clients , but no demo as such.
    4. Never had much luck with them , i prefer paid consulting :).

  15. 15 Gray

    Thanks ng coders i got sandbox working great. I am too having problems with download link not being sent. I get the comfirmation email and so does the user and all the check.php shows no problems. Also my host does allow php mail scripts. I’m completely stumped!

  16. 16 Gray

    yey sussed it. My smtp settings were wrong from my host. It works great now!

  17. 17 Gray

    thank you for the great script!

  18. 18 Digimanual

    Hello,

    It is a good news that this website came back :)

    The script is great. And my website have been running for 10 Months. I have rewrite some codes for my own aim.

    I suggest that new user should read all of the codes again and again untill you understand them.

    Thank you.

  19. 19 tim

    pffft . i was just using the settings thinking it would fill the rest in..
    filled in my host smtp mail settings and still doesnt dummy email me but im all over it..

  20. 20 digimanual

    tim:

    You could use phpmailer send e-mail from other mail server, such like googlemail. It much better then your own e-mail address. For me, some e-mail receiver always put my e-mail into junk folder :( It never happend after I use googlemail.

    ==code in paypal.php==

    // mail($p->ipn_data['payer_email'], $subject, $body, $headers);

    //Disable the PHP mail above
    //PHPmailer, sent an e-mail via smtp server

    require(”class.phpmailer.php”);

    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = “xxxxxx@gmail.com”; // SMTP username
    $mail->Password = “xxxxxx”; // SMTP password
    $name=$p->ipn_data['first_name'].’ ‘.$p->ipn_data['last_name']; // Recipient’s name
    $mail->From = “xxxxxx@googlemail.com”;
    $mail->FromName = “DigiManual”;
    $mail->AddAddress($p->ipn_data['payer_email'],$name);
    $mail->AddReplyTo($mail->From,$mail->FromName);
    $mail->WordWrap = 100; // set word wrap
    $mail->IsHTML(false); // send as HTML
    $mail->Subject = $subject;
    $mail->Body = $body;
    if(!$mail->Send())
    {

    // Use php mail as backup
    mail($p->ipn_data['payer_email'], $subject, $body, $headers);
    }

  21. 21 John

    I’ve tested it and have not gotten it to work (as far as actually receiving the downloaded file). I am hosted at godaddy and I know they have different mail setting, but I get the test emails just fine.

    What settings do I need to change to make it work with a different SMTP?

  22. 22 tim

    Hey John i noticed some product id stuff on the paypal section..i have 1and1 hosting (might be dumping that in the future) and still cant even get the emails to send..cant tell if i need to make the changes in the php.ini file also or just the script area. like i said i havent been getting any dummy emails to know for sure. dang this is hard for a noob.

  23. 23 Sergio

    Hello,

    Great work. You rock!

    I would like to deliver a webpage with the url for the client perform the download instead of sending him an e-mail. Can this be made and how?

    peace!

    Sergio

  24. 24 admin

    @tim : script was never meant for beginners , though i tried making it as simple as possible.

    @Sergio : Yes this can be done to , but changes to the code have to be made.

  25. 25 Sergio

    @admin: Great, I’ll have to figure out how that is done… not a easy task for a not even beginner. It’s always time to start.

    peace!

    Sergio

  26. 26 Enkriptid

    very cool. i am a beginner and i do not have that hard of a time understanding this at all.. good job!

  27. 27 tim

    thanks for the replies guys, i download hmailserver last night and got it working..it has its own little microsoft mysql database built in..myself ,i didnt want or need a full blown mysql database… i dont get the download link just yet in the dummy email– but, still chipping away at it ..im sure the email script does work i just dont have it setup right somewhere..may take another wack at it in the future.

  28. 28 tim

    ok update, im getting the download link and emails from sandbox..click on link to download, but it s file not found?? double checked its there and the script is pointing right at it?? did i miss something?? download url has the encoding garble to it..

  29. 29 tim

    got it! i shouldnt have been changing things in the download.php..
    im good to go…weeeee!

  30. 30 Clay

    Thanks so much–this script is fantastic and exactly what I was looking for!

  31. 31 blackst

    Thank you so much. I have connected it to my wimpy player database and is working like a charm. I had to make some adjustments, but by far the best tutorial i have seen in a while.
    If anyone needs it to work with their database contact me through my web site.

  32. 32 Andy

    Hello! I’m not sure, but what I understand is that the script is for one time purpose, I mean if you want to sell 1 item or more in 1 package. Is it possible to sell more items separately? I want to integrate this script on a wordpress blog, so in every post I have 1 button for selling 1 item. Is this a simple thing or several changes should be made?

  33. 33 George

    Hello. As far as I can tell this is a wonderful script. I read in one of your replies that this is not meant for beginners. I’m below a beginner, but I’m not a complete moron.

    If you will indulge me please.

    I have my movie
    Your download.php
    Your e-mail.php
    Your paypal.php
    Your check.php
    Your settings.php

    After I changed info in settings.php I did check.php and everything was successful. I received the e-mail.

    I now want to check in sandbox, so in settings I set it to true.

    What do I do next, to check it in sandbox? When I click on the button it brings me to paypal not sandbox paypal.

    What else do I need to do?

    Thank you

    One last thing, I notice in a few of the php it says something about thankyou.html, what is that? (Sorry I am a beginner)

  34. 34 George

    Hello: FORGET MY LAST POST.

    I am now able to check through Sandbox. But, I have some questions.

    1. When I buy through paypal I get no e-mails anywhere. But, after I purchase there’s a box that says RETURN TO MERCHANT. When I click on that it takes me to my THANKYOU.HTML and I am able to download the movie.

    However, since it’s at sandbox there is no customer e-mail to see if an e-mail is sent with the link. Do you know how I can check this?

    2. When I do check.php everything says successful except products check. Would you know why?

    Thanks again: George

  35. 35 Charlotte

    Hi, I have tested this script on my site and the pay button then paying works and i recieved the money except no email went to the payee’s address and I don’t really know why. Is there something I have to add or change in anything?
    Thanks for your help and this would be a really good script once I get it to work!

  36. 36 Stewart Waller

    Hi

    What a great script!

    I have been setting up the script and think i almost have it working however i am trying to set the currency to British Pounds, (GBP). I have changed the currency in the settings.php to GBP but it makes no difference.

    Thanks

    Stewart

  37. 37 Stewart Waller

    Hi

    Just to let you know got it working for GBP

    thanks again

    Stewart

  38. 38 river

    After reading some of the above posts I wonder if I’ve downloaded the right thing because I don’t see a php. ini file.

    You have indicated that this scipt is not for beginners but please don’t be exasperated by my questions. I’m a quick study. My questions are:

    !. Should the merchant have a Web Payment Standard or Web payment Pro account at PayPal? Or either one?

    2. You indicated above that an index.html file is included to protect the directory the script goes in. Does this mean the script is to go into a directory by itself? Or can it go in the main directory where the form is?

  39. 39 Idan

    I know why might people not get this script to work, especially you Viper

    It seems that if the default paypal account e-mail address is not the same as the payment address it will return an “Invalid Reciver E-Mail” error and won’t send the download link.

    I have simply commented out the whole part in paypal.php and it’s now working for me.

    // if( $this->ipn_data['receiver_email'] != $paypal_email )
    // {
    // $this->debug_log(’Invalid Reciver E-Mail : ‘.$this->ipn_data['reciver_email'],false);
    // return false;
    // }

    Hope this is useful for others!

  40. 40 swapnil

    any idea if something like this can be incorporated into google merchant accounts?

  41. 41 nero

    Hi and compliments for this work!
    Just a little info, wat is the url to set on Paypal admin about IPN payments?

    (paypal require URL for IPN)

    Please, let me know and thanks!

    massimo

  42. 42 admin

    @river - Standard paypal account should do ( should not be limited ). Both things can be done , but its better to keep it in separate directory.
    @idan - Someone can simulate a ipn request from paypal and itll work , though possibility is minimal .
    @swapnil - I dont htink so
    @nero - You dont need to set any paypal IPN url , just leave it empty/disabled.

  43. 43 Geert

    Hi there,
    Just wondering how I could use Euro as currency .. I changed the currency field in settings.php to “EUR” .. but this doesn’t do the trick. Do I need to change anything in the button code as well ?

    Cheers,
    Geert

  44. 44 Nils

    great code…. can’t wait to get it working…

    I have changed everything to what i think is correct, but am new to sandbox…. sandbox receives the order confirmation….. but no email is sent to the sandbox buyer concerning the download - i assmue because the email address doesn’t actually exist!

    I am also running WAMPserver

    Is there anyway i can tes download link without putting the site online?

  45. 45 Cassavetes

    Hi,
    Thank you for your scripts. I got a problem when I run the check.php:
    Checking product ID [2] is accessible :Failed
    I think it’s related to the button but I don’t know where I made a mistake. Can you tell me more about this issue?
    And when we try the ipn script with sandbox, how can we check if the emails are sent? (emails are fictive so…)
    Thanks in advance.

  46. 46 Cassavetes

    Hi,
    Forget it…Found my mistake for the check issue. thanks again.

  47. 47 Mevo

    Hi, I got it working but the only problem is that the email with the download link is not being sent after purchase. The debug says every is ok and i am able to use php’s mail function. Any ideas?

  48. 48 philg

    Does this script function with more than 1 item in a shopping cart?

    If not is there a script available which can handle >1 item_numbers in the IPN?

  49. 49 Pweck

    I would also like to know if it is possible to add more than one item to my downloads folder.

    Ive added another product to the array in my settings, but I noticed that the download file reads and dumps a link from product [2].

    My check shows that both product [2] and product [3] are found, and it generates two buttons for me.

    But will the download only point to product [2] based on what is in the download.php?

    Anyway, thanks so much for the script, real gem.

  50. 50 Pweck

    The reason why most of these download link emails are not being sent is because the settings.php has on line 31:

    // Send File as attachment

    $email_attachment = true;

    if you are trying to sell anything more than a few mb you probably wont ever see the download email.

    if you just set

    $email_attachment = false;

    the email should send fine with a download link.

    Also for selling more than one product all you need is another product array starting after product[2], so product[3] array(… and so on.

    Now I am having a problem though where I have a zip file which is 325mb which I am trying to download but my download link will always truncate the file at 141 mb and i am not sure why as I can manually download the file just fine.

    Any suggestions?

  51. 51 M K Mathur

    Hi,

    Thanx, Your script is wonder & very user friendly.
    But My requirement is some different. I hope you will solve.

    I need products details like price downloadlink from database not static.
    I tried to modify
    $products[1] = array(’noce’,’30′,’downloads/nocne.zip’);
    But did not get sucess. Pls. help me

Leave a Reply



About

You are currently browsing the NGCoders - Next Generation Coders weblog archives.

Categories

Subscribe