Once Apple introduce the in-app-purchase, purchase digital goods inside app becomes one acceptable way for users accessing videos, audios, eBooks, virtual goods easily. Therefore, in-website-purchase should be the correct way for user to purchase digital goods inside Website. PayPal released a new payment way which enable us to purchase digital goods smoothly in website.

The first time I was using the Paypal is 4 years ago. At that time, my friend and I want to create a social site using PHP and integrate the Paypal as our main payment gateway. Then, in the second time, I implemented the Paypal in my CMS site, which is an simple donation button. I think it is time to use Paypal again, because it provide a new payment way, PayPal for Digital Goods.

What’s PayPal Digital Goods (Express Checkout)

What’s the benefit from PayPal Digital Goods

Cheaper transition fee is the most attractive benefit. When we sell our product in a small amount money, PayPal Digital Goods is the best choice, especially when people are familiar to pay small dollar like $0.99, $2.99 for digital goods.

Website Payment Standard Regular Fee:2.9% + $0.30 USD to 3.9% + $0.30 USD
PayPal Digital Goods (Express Checkout) Fee (for U.S. Business Account):5.5% + $0.05 USD
PayPal Digital Goods (Express Checkout) Fee (for non U.S. Business Account):6% + $0.05 USD

For example, if we sell one digital product in our website at $2.00, the PayPal regular fee will be around $0.35 to $0.37. However, the PayPal for digital goods fee is only $0.15 to $0.16.

Which PayPal payment solution is better for you?

The Paypal website payment standard regular fee is start from 2.9% to 3.9% plus $0.30 transition fee. If we use the minimum fee ratio 2.9% to calculate, it’s better to choose PayPal digital goods when we sell our products under $8.00. If we use the maximum fee ratio 3.9% to calculate, Paypal digital goods payment solution is preferred when we sell our products under $12.00. By the way, all my calculation is according my PayPal fee. Paypal account in different country has different transition fee.

PayPal Digital Goods Express Checkout Example

With PayPal digital goods, we can integrate this payment gateway smoothly. It will enable the payment without redirecting user leaving current page. When users click the pay button, it will show up an payment window in current page. After users finish the payment, we can close the window and show the digital content immediately.

Here is a demo online PDF store to show you how PayPal Digital Goods payment solution works in a web page. In this PayPal express checkout example, I am using PayPal sandbox testing account to receive and pay. Hence, when you click the buy button, please using following test account and password to finish the payment. After you pay with sandbox account, you will get a download link.

Integrate PayPal Digital Goods Express Checkout in Website

To implement this example, we need to put the PayPal form and a piece of JavaScript in the html page.

<form action="checkout.php" method="POST">
<input id="paypal_submit" alt="Pay with PayPal" name="paypal_submit" src="https://jmsliu.com/digitalgoods/img/buttonCheckoutPaypal.png" type="image" align="top" /></form>
<!-- Add Digital goods in-context experience. Ensure that this script is added before the closing of html body tag -->

<script src="https://www.paypalobjects.com/js/external/dg.js" type="text/javascript"></script>

	var dg = new PAYPAL.apps.DGFlow({
		trigger: 'paypal_submit',
		expType: 'instant'
//PayPal will decide the experience type for the buyer based on his/her 'Remember me on your computer' option.


As you can see, the form action is pointed to checkout.php which is hosted on our server. In the checkout.pnp, we need to specify the several sale information such as price, amount, returnURL and cancelURL. Here is the list:

  • paymentAmount, the total price of the order. It is $0.99 in my example.
  • currencyCodeType, the currency. It is USD
  • paymentType, the type of payment. In my case, it is “Sale”
  • returnURL, the return url when payment is successful.
  • cancelURL, the cancel url when payment is cancelled.
  • items, it is an array, which will store all items which user will buy. In my example, it has only one item, “PayPal Digital Goods Integration Guide”. The name will show in the Paypal payment page.

Here is the checkout.php:


require_once ("paypalfunctions.php");

$PaymentOption = "PayPal";
if ( $PaymentOption == "PayPal")
	$paymentAmount = "0.99";
	$currencyCodeType = "USD";
	$paymentType = "Sale";
	$returnURL = "orderconfirm.php";
	$cancelURL = "cancel.php";
	$items = array();
	$items[] = array(
        'name' => 'PayPal Digital Goods Integration Guide',
        'amt' => $paymentAmount, 
        'qty' => 1);


    // to add anothe item, uncomment the lines below and comment the line above
    // $items[] = array('name' => 'Item Name1', 'amt' => $itemAmount1, 'qty' => 1);
    // $items[] = array('name' => 'Item Name2', 'amt' => $itemAmount2, 'qty' => 1);
    // $paymentAmount = $itemAmount1 + $itemAmount2;

    // assign corresponding item amounts to "$itemAmount1" and "$itemAmount2"
    // NOTE : sum of all the item amounts should be equal to payment amount

    $resArray = SetExpressCheckoutDG( $paymentAmount, $currencyCodeType, $paymentType,
    $returnURL, $cancelURL, $items );

    $ack = strtoupper($resArray["ACK"]);
    if($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
        $token = urldecode($resArray["TOKEN"]);
        RedirectToPayPalDG( $token );
    } else {
        //Display a user friendly Error on the page using any of the following error information returned by PayPal
        $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
        $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
        $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
        $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);

        echo "SetExpressCheckout API call failed. ";
        echo "Detailed Error Message: " . $ErrorLongMsg;
        echo "Short Error Message: " . $ErrorShortMsg;
        echo "Error Code: " . $ErrorCode;
        echo "Error Severity Code: " . $ErrorSeverityCode;



In the checkout.php, it includes another php file, paypalfunctions.php. This file specify my paypal account information and all pre-defined PayPal Express Checkout functions. To use the latest REST PayPal API, we need to provide following information:

  • API_UserName
  • API_Password
  • API_Signature

API_UserName is not the PayPal login email account. Neither API_Password is. We can get all these information in our PayPal account. In PayPal Selling Tools section, find the “API access” and click update. In the next page, please choose “Option 2 – Request API credentials to create your own API username and password.” After that, you will see your API username, password and signature. You can find the detail instructions in PayPal document.


orderconfirm.php is called after user finishes the payment successfully. For digital goods purchase, we can give user the download link or close the payment window. In my example, I will show the download link for user to download what he purchased. Here is the code example:

<script><br />
alert("Payment Successful");

<a href="https://merchant.paypal.com/cms_content/CA/en_US/files/merchant/PP_ExpressCheckout_IntegrationGuide_DGEnt.pdf">Start Download PayPal Digital Goods Integration Guide</a>


This file will be called if user cancels the payment. So I just close the payment windows. It is quite straightforward.

alert("Payment Cancelled");
 // add relevant message above or remove the line if not required
window.onload = function() {
	 if (window.opener) {
	} else {
		if (top.dg.isOpen() == true) {
            return true;

Get PayPal Digital Goods Example Source Code at $9.99

You will get the full PayPal Digital Goods example source code. With the source code, you can implement your own PayPal digital goods in your website. To use this example source code, please copy the whole digitalgoodsexample folder into your host root folder, and use sandbox PayPal to try the example purchase. If you don’t have, you can use mine in above example.

In this example source code, it includes:

  • example.html, Digital Goods Store Page
  • paypalfunctions.php, PayPal Express Checkout Function File
  • checkout.php, PHP Script which will send request to PayPal
  • orderconfirm.php, PHP Script which will show user the downloading link after user finishes the purchase successfully
  • cancel.php, PHP Script which will close the payment window when user cancels the payment

Premium Project: Digital Goods Ecommerce Platform

If you are interested in build your own payment system and deploy on your website, I suggest you take a look at my premium payment system. It is based on PHP with Bootstrap, you can easily sell and manage your digital goods with your website. It is designed to work with all the website CMS systems such as WordPress, Joompla, Drupal, etc. For more information and demonstration, please check PayPal Digital Goods Delivery System.

Previous PostNext Post


  1. James,

    I purchased the example source code, but received nothing and wasn’t redirected to a download or anything.

    Please advise.


  2. Hello,

    I am waiting for your source code, I paid it and wasn’t redirected me to a download link.

    Kind regards.

    1. Hi,

      The source code is sent to your email account. My website is old, so it take time to implement PayPal Digital Goods in my website. But I will do it when I am free enough.

      I think Paypal Digital Goods is really helpful like my case. Hopefully, the source code will be helpful to you as well.


  3. I am using your code samples that I purchased. In the sandbox, I get to the point where I can authorize payment to the sandbox buyer account, but then it goes to “Loading” and hangs on sandbox.paypal.com/webapps/checkout/webflow/Sparta/expresscheckoutvalidatedataflow?execution=e1S2

    Any idea of where to go from here?
    I have a return url set, and am using my sandbox api credentials.

    Maybe you ran into this before?

    1. Hi Jim,

      Sometimes the Paypal sandbox server will hang. I met the problem several times. Usually, it will recover after several hours. However, I still suggest to clear the cache and cookie before you try it again.

      My code handles sending request and verifying the purchase after users finish the payment (orderconfirm.php and cancel.php).


  4. Hello James please tell me can I integrate this code in my blogspot website?
    I want to buy this code.

    1. For blogspot, I am not very sure. If the blogspot allow you to put javascript in your post, then the answer is YES.

      Best Regards

  5. Hi,

    I need to integrate PayPal in my multi-langual website.
    Is your source code still for sale?

    Is it possible to open the PayPal window in the language from the country from where the visitor lives?

    Can I sent a parameter with the payment-request (f.e. id-number of customer) and get the parameter back after the PayPal window closes? (I want to get the id-number after payment and store it in database with order-information).

    1. Hi Jo,

      Yes, my source code is always ready for sale.
      About multi-language, I think it is possible. But it may take some time for me to figure it out.
      For sending parameter with payment-request, you can send whatever you want and they will be returned after the payment is done. You will get the id-number after payment and store it in database.

      Best Regards

  6. Hi James,

    I tried ordering your source code but keep getting a message from your website saying “payment failed”.

  7. Hi, I’m interested and it looks like this might be a good solution for the purchase but I need for customers to be able to listen to a sample mp3 so they know what song they are getting. Is that possible?

    1. Hi Frank

      It is quite simple. You can prepare a sample mp3 file for your customer to preview. Then, if your customers make the purchase, you show them the real one. Are you running your business on Joomla website? If so, there is a extension for you, much easy and simple.

      Best Regards

  8. I am wondering if I can incorporate this in my wordpress site for selling digital files (pdf). I have been using custom post type, but my issue was to redirect user after purchase to the right product.
    Can I use your codes with wordpress custom post type? It’s not expensive at all, but could I get any further help from you to make it work with wordpress?

    1. Dear Toure,

      My code is an example to implement PayPal digital goods payment part. And it works perfectly with WordPress. I don’t understand what you mean about “custom post type”. If you want to put “buy button” in post, just insert a piece of html/javascript code.

      Best Regards

    1. Thank you for your purchase. I think you already get a good experience about the purchase procedure. PayPal digital goods works perfect for downloadable product, like your pdf files. But there are some constrain. First of all, you must have a Business PayPal Account. Second, you must setup your PayPal payment method as “PayPal Digital Goods (Express Checkout)”. For more information, you can check https://www.paypal.com/us/webapps/mpp/digital-goods

      Best Regards

  9. Thank you for your response.
    I have the following concerns, I am planning to sell multiple products and I don’t want the client to have to do anything at all.
    I have created a wp custom post type called downloads in order for the client to add new download products. every product will have its own price and own download link. Now in your checkout.php I believe I could replace the following line based on my custom post type meta box:

    07. “0.99” width “‘ .get_post_meta($post->ID, ‘product_info_price’, true). ‘”;
    13. ‘name’ => get_the_title($ID);
    to dynamically get the current product information (price and name);

    I do have an archive product page and a single product page.
    I would like to give an option to directly add product to cart from the archive page for multiple purchase or add a product from the single product page.
    I will be glade if I can send you my codes so you can have a much better idea.
    I know this might be outside of your offer here, but I will appreciate any help on at least knowing if it possible with your codes.


  10. I see that on checkout.php on the section checkout module, I will need to insert a loop so it can through all my products dynamically because it will be impossible to manually update the code when adding new products to the list of downloads. I could not also find out where I could point to the digital download link. Is there a way to have the link to my digital downloads on my server and automatically point to them? I will not be able to actually know what product customer will decide to purchase so I can have all the download in a zip. My best best will be product individual link to each product purchase from the cart.

    Please respond to me. At least, I want to know if it will be worth for me to continue my project based on your codes.

    Again thank you.

    1. Dear Toure,

      For a short answer, if you want to implement with a shipping cart, it is hard to do with my code, but still possible.

      You have purchased this example, I have built a product manager system to manage my digital products, but only for one purchase – one download.

      If you are interested in my product manager system, let’s talk about it. It will be easier for you to add new products and sell them on your pages. But you still have to manually add the “buy button” code in your wordpress post.

      Best Regards

  11. Thanks for your reply.
    Yes I will be interested on your product manager system. I want to get the best out of the digital download cart. What do I need to do at this point? Is your product manager for sale or is there a way to guide me on building upon the codes I purchased?

    1. Dear Toure,

      I have a plan to sell the whole system as a premium project. But I am still working on a demo site and product site.

      Best Regards

  12. Hi,

    Could you tell me at what point or section in your script I can incorporate the option to have the sale transaction recorded on my database?

    So where should I put this in order to capture payment transactions into my wordpress database?
    // change to your own directory if necessary
    global $wpdb;
    $table = $wpdb->prefix.”main_table”
    $wpdb->query(“INSERT INTO $table (album, artist) VALUES ($_POST[‘album’], $_POST[‘artist’])”);

  13. I will definitely update my data based on paypal express payment details. Your other plugin is going into a right direction, but it’s not really what I am looking for. If I have some indication about where to insert my database request and where/how to create product details under arrays instead of hard coding their name, price, etc… I guess I will be just fine.

    1. If you want to update your database according to the successful transaction, you can insert your code in orderconfirm.php

  14. Hi

    Does this solve the problem of paypal digital goods not working on IPADS?

    See this link for a description of the problem:


    I am having issues with my paypal digital goods shopping cart being blocked by default on IPADS. Its been my experience, and the experience of many others, that paypal digital goods does not work on ipads and safari due to PPDG launching a popup to complete the payment process.

    Does this solve that problem?

  15. Hi, I am in a hosted in a windows server,does it works in that configuration or it needs a linux server?

  16. Hello James,
    A long time has passed since you wrote this post and I’m not sure if you are available to help.
    I’m in the process of developing a freelance writing website where payments will be made through PayPal. However, since the price of questions being posted by clients vary, I may need a different solution from what you gave above.
    In my case, the client should set the price he/she is willing to pay on the freelance website . After clicking next on the site, a PayPal payment page should pop up with the price the client placed.
    I have been in contact with PayPal to guide me in an API that can do this but so far has been unsuccessful. What PayPal is offering is a Pay Now button where price is set or can be manipulated on the PayPal Payment Page.

    Kindly advice
    Thank you

    1. Hello Benson,

      Please don’t worry about the age of the post. The post is old, but PayPal doesn’t change its way to collect money. The way I show here is still working. The demo is still working.

Leave a Reply

Your email address will not be published. Required fields are marked *