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.

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


  • Hi, how can I send two or more different email with diffrent topics depending on the products? should i create more thatn one check.php and setting folder?

    for example: check2.php and setting2.php for the second process?

  • Hello,
    This script was working great but all of a sudden when the customer clicks on their download link only about half of it downloads. It then says the folder is empty. Any ideas?

    • The half download must have to do with php timeout on your server , Please check.

      • The thing is using the link the script provides only half the file downloads but if I use a direct link to the file from the www. it downloads fine. Your suggestion might be right because of the PHP script?

  • Hi, im just about to give this a go. Ive just wasted all day tring to get google checkout to work. I am concerned that the price is in the source code. Is this a problem? I mean will paypal process a payment for less than the set amount. Thanks.

  • sorry just started and found my answer.

  • Hi,

    I’ve got my script up and running, the check is 100% successful with 5 products and counting. The IPN script is working correctly and I’m recieving payments from customers.

    However, my automatic download link is not sending and I’m having to resort to manually sending a link to my customers. The first 2-3 tests I ran were fine, with an automatically generated link being sent to the test e-mail, but now that the shopfront is up and running customers are not recieving the download links.

    The Paypal IPN history suggests all IPN calls are being sent successfully, but the automatic mail isn’t sending out.

    Any ideas?

    Thanks.

  • Hi

    yeah, the dummy email from check.php arrives in an instant. I’ve checked with my host and there seems to be no problem with my account accepting incoming mail from a PHP mailer either.

    Any help would be greatly appreciated!

    Thanks

  • Oh Man….i find it difficult to believe that i stumbled onto your piece of writing so easily…LOL…..It was just what i was loking for….Thanks so much

  • Is there a limit to the size of the zipped folder being downloaded? I have a 6.6MB zipped folder that causes the script to not run fully. Thanks!

  • Hi,

    I would like just to ask if it’s possible to use other emails attached to my Paypal other than the primary email.

    My primary email is customerservice@mydomain.com but I want to use payments@mydomain.com.

    Is there any type of setting I should do inside my Paypal account? Is this possible?

    Thanks so much.

  • Will this work with add to cart with multiple items?

    Thanks
    Bob

  • I’m having the same issue Rob (above) is having.

    Everything is working fine, payment is recieved, IPN is returning as verified.

    The email to the customers, with the download link, is not being sent.

    Any help would be greatly appreciated!

  • Yes, the test email sends without a problem.

    The email to the Seller also gets sent, the only email that does not go through is the email to the Buyer (with the download link). ?

    I’m confused. If the seller email is being sent, why isn’t the buyer one working?

  • I got it to work – apparently it had an issue with the email attachment. I changed this to “false” and the email went through.

    I’d still like to get the attachment to work. Any thoughts as to why it wouldn’t work with the attachment. The file is a PDF.

  • Great script and easy to implement. One question: can I use it to send the user a custom generated serial number to activate the downloaded kit he just received in the link?
    We sell mobile software and we need the customer to insert his/her IMEI and a desired password in the order form, then after successful payment, he will get delivered the download link and a serial number. Can we use your script to do this? How? Thanks!

  • Great tutorial ! It works perfectly for me !
    Thanks

Leave a Reply