Integrate PayPal Digital Goods Express Checkout Into Website

On April 27, 2013, in Make Money Online, by James Liu

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)

Here is a video to show what PayPal Digital Goods is. Click Here.

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.

PayPal Digital Goods Flow

PayPal Digital Goods Flow

Here is an example 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 account and password to finish the payment. After you pay with sandbox account, you will get a download link for PayPal Digital Goods Integration Guide;


Username: pygy_1283buyer@hotmail.com
Password: 123456789

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 type='image' name='paypal_submit' id='paypal_submit'  src='https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif' border='0' align='top' alt='Pay with PayPal'/>
</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>


<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.
	});

</script>

checkout.php

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:

<?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&#91;&#93; = array('name' => 'PayPal Digital Goods Integration Guide', 'amt' => $paymentAmount, 'qty' => 1);

	//::ITEMS::
	
	// 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;
	}
}

?>

paypalfunctions.php

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.

PayPal API Access Page

PayPal API Access Page

orderconfirm.php

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:

<html>
<head>
<script>
alert("Payment Successful");
</script>
</head>
<body>
	<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>
</body>
</html>

cancel.php

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

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

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.

Tagged with:  

37 Responses to “Integrate PayPal Digital Goods Express Checkout Into Website”

  1. Craig Neubauer says:

    James,

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

    Please advise.

    Craig

  2. Craig Neubauer says:

    Thanks James for the Article and quick response. I now have the example code.

  3. easy_republik says:

    Hello,

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

    Kind regards.

    • James says:

      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.

      Thanks
      James

  4. Jim H says:

    Purchased source code, waiting for email. thanks.

  5. Jim Hewitt says:

    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?
    thanks,
    jim

    • James says:

      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).

      Thanks
      James

  6. WonderHackTools says:

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

    • James says:

      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
      James

  7. Jo Kooy says:

    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).

  8. Jo Kooy says:

    Hi James,

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

  9. Frank says:

    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?

    • James says:

      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
      James

  10. SoftwareSeller says:

    Hey James just made payment please send me te code.Transaction ID3HC03385BT044603G.Thanks!

  11. Toure says:

    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?

    • James says:

      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
      James

  12. Toure says:

    Just bought your product. waiting if you can provide me some answer. Thanks

    • James says:

      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
      James

  13. Toure says:

    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.

    Thanks.

  14. Toure says:

    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.

    • James says:

      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
      James

  15. Toure says:

    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?

    • James says:

      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
      James

  16. Toure says:

    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?
    `
    require_once(‘/Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-config.php’);
    // 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’])”);
    `

  17. Toure says:

    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.

    • James says:

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

  18. Toure says:

    I was looking on that file and I saw the note about saving the transaction to local database, but the place to put it is uncleared to me.

  19. Kyle says:

    Hi

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

    See this link for a description of the problem:

    http://www.linkedin.com/groups/PayPal-Digital-Goods-Express-Checkout-3988941.S.146761186

    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?

  20. gio says:

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

Leave a Reply

Weboy

WordPress Blog