1. Code
  2. PHP
  3. CodeIgniter

How to Sell Digital Goods with CodeIgniter

Scroll to top
33 min read

In today's tutorial, you'll learn how to create a small web app to sell digital items (eg. eBooks) securely and accept payments from PayPal.
Please note that this tutorial requires PHP 5. Please upgrade your PHP installation if you're still running version 4.


Getting Started

Download the latest CodeIgniter release to your server, and set up your MySQL database (I named it digitalgoods) with the following SQL queries:

1
 
2
CREATE TABLE `ci_sessions` ( 
3
  `session_id` varchar(40) NOT NULL DEFAULT '0', 
4
  `ip_address` varchar(16) NOT NULL DEFAULT '0', 
5
  `user_agent` varchar(50) NOT NULL, 
6
  `last_activity` int(10) unsigned NOT NULL DEFAULT '0', 
7
  `user_data` text NOT NULL, 
8
  PRIMARY KEY (`session_id`) 
9
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
10
 
11
CREATE TABLE `downloads` ( 
12
  `id` int(11) NOT NULL AUTO_INCREMENT, 
13
  `item_id` int(11) DEFAULT NULL, 
14
  `purchase_id` int(11) DEFAULT NULL, 
15
  `download_at` int(11) DEFAULT NULL, 
16
  `ip_address` varchar(15) DEFAULT NULL, 
17
  `user_agent` varchar(255) DEFAULT NULL, 
18
  PRIMARY KEY (`id`) 
19
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
20
 
21
CREATE TABLE `items` ( 
22
  `id` int(11) NOT NULL AUTO_INCREMENT, 
23
  `name` varchar(255) DEFAULT NULL, 
24
  `description` text, 
25
  `price` decimal(10,2) DEFAULT NULL, 
26
  `file_name` varchar(255) DEFAULT NULL, 
27
  PRIMARY KEY (`id`) 
28
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
29
 
30
CREATE TABLE `purchases` ( 
31
  `id` int(11) NOT NULL AUTO_INCREMENT, 
32
  `item_id` int(11) DEFAULT NULL, 
33
  `key` varchar(255) DEFAULT NULL, 
34
  `email` varchar(127) DEFAULT NULL, 
35
  `active` tinyint(1) DEFAULT NULL, 
36
  `purchased_at` int(11) DEFAULT NULL, 
37
  `paypal_email` varchar(127) DEFAULT NULL, 
38
  `paypal_txn_id` varchar(255) DEFAULT NULL, 
39
  PRIMARY KEY (`id`) 
40
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
41
 
42
INSERT INTO `items` (`id`,`name`,`description`,`price`,`file_name`) 
43
VALUES 
44
  (1, 'Unix and CHMOD', 'Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum.\n\nIn condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum. Etiam scelerisque, nunc ac egestas consequat, odio nibh euismod nulla, eget auctor orci nibh vel nisi.\n\nDonec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum. Etiam scelerisque, nunc. Morbi malesuada nulla nec purus convallis consequat. Vivamus id mollis quam. Morbi ac commodo nulla.', 19.99, 'UNIX and CHMOD.txt'), 
45
  (2, 'Intro to 8086 Programming', 'Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum.\n\nMorbi malesuada nulla nec purus convallis consequat. Vivamus id mollis quam. Morbi ac commodo nulla.', 4.95, 'Intro to 8086 Programming.txt');

The first query creates CodeIgniter's default user sessions table. We then create a table to log file downloads, one to store the items and another to store purchase details. Finally, we insert a couple of items into the table.

We've inserted two items into the database, so we have to create those files on the server. In the root directory for your application (the same folder as CodeIgniter's system directory), create a new directory named files:


In that directory, create two text files, named UNIX and CHMOD.txt and Intro to 8086 Programming.txt. The capital letters are important on most web servers. These are the file names we set in the database for our two items. Enter some content in the files so we can be sure the files are being downloaded correctly.

CodeIgniter Configuration

In the system/application/config/database.php file, set your database settings in the fields provided:

1
 
2
$db['default']['hostname'] = "localhost"; 
3
$db['default']['username'] = "root"; 
4
$db['default']['password'] = ""; 
5
$db['default']['database'] = "digitalgoods";

In config/autoload.php set the following libraries and helpers:

1
 
2
$autoload['libraries'] = array( 'database', 'session', 'form_validation', 'email' ); 
3
... 
4
$autoload['helper'] = array( 'url', 'form', 'download', 'file' );

In config/config.php set the base_url:

1
 
2
$config['base_url']	= "http://localhost/digitalgoods/";

In the same file, paste the following to create our own configuration settings:

1
 
2
/* 

3
|-------------------------------------------------------------------------- 

4
| Website Name 

5
|-------------------------------------------------------------------------- 

6
| 

7
| Will be used on page title bars, in emails etc. 

8
| 

9
*/ 
10
$config['site_name'] = "Digital Goods"; 
11
 
12
/* 

13
|-------------------------------------------------------------------------- 

14
| Admin Email 

15
|-------------------------------------------------------------------------- 

16
| 

17
| Used to send confirmations of purchases 

18
| 

19
*/ 
20
$config['admin_email'] = "dan@example.com"; 
21
 
22
/* 

23
|-------------------------------------------------------------------------- 

24
| No-Reply Email 

25
|-------------------------------------------------------------------------- 

26
| 

27
| The 'No-Reply' address used to send out file downloads 

28
| 

29
*/ 
30
$config['no_reply_email'] = "noreply@example.com"; 
31
 
32
/* 

33
|-------------------------------------------------------------------------- 

34
| PayPal Account 

35
|-------------------------------------------------------------------------- 

36
| 

37
| The email address PayPal payments should be made to 

38
| 

39
*/ 
40
$config['paypal_email'] = "paypal@example.com"; 
41
 
42
/* 

43
|-------------------------------------------------------------------------- 

44
| Download Limit 

45
|-------------------------------------------------------------------------- 

46
| 

47
| How many times can an item be downloaded within a certain time frame? 

48
|   eg. 4 downloads within 7 days 

49
| 

50
*/ 
51
$config['download_limit'] = array( 
52
	'enable'	=> false, 
53
	'downloads'	=> '4', 
54
	'days'		=> '7' 
55
);

Set each of the new config options to your desired settings, but keep 'Download Limit' disabled for now.

Finally, under config/routes.php set the default controller:

1
 
2
$route['default_controller'] = "items";

View All Items

Now to create the main controller for the site, create a file in the controllers directory named items.php, and inside enter:

1
 
2
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
3
 
4
class Items extends Controller { 
5
 
6
    function Items() { 
7
        parent::Controller(); 
8
        $this->load->model( 'items_model', 'Item' ); 
9
        $data['site_name'] = $this->config->item( 'site_name' ); 
10
        $this->load->vars( $data ); 
11
    } 
12
 
13
    function index() { 
14
        echo 'Hello, World!'; 
15
    } 
16
 
17
}

In the constructor we've loaded in the 'items_model' model (which we've named 'Item') which we'll create next, and placed the 'site_name' configuration setting into a variable which we can access in the views. For the index method, we've just set a simple 'Hello, World!' message for now.

Also, create a new model by creating new file in the models directory named items_model.php and inside enter:

1
 
2
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
3
 
4
class Items_model extends Model { 
5
 
6
    function Items_model() { 
7
        parent::Model(); 
8
    } 
9
 
10
}

If you take a look at the site in your browser now, you should see a 'Hello, World!' message which is being served from the index() method in the controller.

Now that we have no errors, let's get all the items from the database and display them.

In your Items controller, edit the index() function to use the following lines:

1
 
2
$data['page_title'] = 'All Items'; 
3
$data['items'] = $this->Item->get_all(); 
4
$this->load->view( 'header', $data ); 
5
$this->load->view( 'items/list', $data ); 
6
$this->load->view( 'footer', $data );

On the first line, we set a the title for the page to 'All Items'. As usual, everything in the $data array will be extracted when it's passed to the view - so $data['page_title'] can be accessed simply as $page_title.

Following that, we call a get_all() method from the Item model (which we haven't yet created) and place the result in what will become the $items variable.

On the three following lines, we load three view files.

The View

Inside the application/system/views/ directory, create the following file/folder structure: (header.php, footer.php, and an items/ directory with index.php inside).


header.php and footer.php are universal in our app and will be used on every page, and for each controller we will create a separate views folder (items/) and each method will have a view file named after it (index.php).

Inside header.php enter the following HTML5:

1
 
2
<!DOCTYPE html> 
3
<html lang="en">  
4
<head>  
5
  <meta charset="UTF-8" /> 
6
  <link rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" type="text/css" /> 
7
  <title><?php echo $page_title . ' | ' . $site_name; ?></title> 
8
</head>  
9
<body> 
10
 
11
<div id="wrap"> 
12
 
13
  <header> 
14
    <h1><?php echo anchor( '', $site_name ); ?></h1> 
15
  </header> 
16
 
17
  <section>

On line 5 we're including a CSS file we'll create later which will be located at the root of our site's file structure.

On line 6 we use both the $page_title and $site_title variables to create a relevant title for the page.

Finally, on line 13 we use CodeIgniter's anchor() method (from the URL Helper) to create a link to the site's homepage.

Inside footer.php enter:

1
 
2
  </section> 
3
   
4
  <footer> 
5
    <?php $copyright = ( date( 'Y' > 2010 ) ) ? '2010&ndash;' . date( 'Y' ) : '2010'; ?> 
6
    <p><small> 
7
      Copyright &copy; <?php echo anchor( '', $site_name ) . ' ' . $copyright; ?>. 
8
    </small></p> 
9
  </footer> 
10
 
11
</div><!-- /wrap --> 
12
</body> 
13
</html>

Line 4 sets the dates for the copyright notice. If the current year is 2010 then only the year 2010 will display in the notice. If the current year is later than 2010, say 2013, the date in the copyright notice would display as "2010–2013".

Retrieving and Displaying Items

In the controller, you will recall we called a get_all() method from the Items model. So inside models/items_model.php type:

1
 
2
function get_all() { 
3
  return $this->db->get( 'items' )->result(); 
4
}

This one line of code will retrieve all entries from the 'items' table in the database as PHP objects and return them in an array.

Now, to display the items, enter the following in views/items/index.php:

1
 
2
<?php 
3
if ( ! $items ) : 
4
  echo '<p>No items found.</p>'; 
5
 
6
else : 
7
  echo '<h2>Items</h2>'; 
8
  echo '<ul>'; 
9
 
10
  foreach ( $items as $item ) { 
11
    $segments = array( 'item', url_title( $item->name, 'dash', true ), $item->id ); 
12
    echo '<li>' . anchor( $segments, $item->name ) . ' &ndash; $' . $item->price . '</li>'; 
13
  } 
14
 
15
  echo '</ul>'; 
16
 
17
endif;

First, we check whether we retrieved any items from the database and display a message saying so if no items were found.

If items were retrieved from the database, we display a title and open an unordered list. We then loop through each item with foreach().

CodeIgniter returns each entry from the database as an object, so you can access an item's details with $item->name, $item->id, $item->price etc. for each field in the database.

We're going to give each item a SEO-friendly URL, with the item's name in it. For example: http://example.com/item/unix-and-chmod/1/ (the name being in the second segment and the ID in the third).

At line 10 we set the $segments variable to an array containing data which will be converted into a URL (each item in the array will be a segment of the URL). The first segment is simply 'item', then we use the CodeIgniter's url_title() function to make the item's name URL-friendly (removing capital letters and replacing spaces with dashes). And the third segment is the item's ID.

On the next line we create a new list item and pass the $segments to anchor() to create a URL. We also display the item's price. The loop is then closed off, the list closed and the 'if' statement terminated.

Refresh the page in your browser and you should see the two items from the database:



View Single Items

You may be wondering why the URL segments for a single item are item/unix-and-chmod/1 as in CodeIgniter that means we're pointing at a controller named 'item' with a method of 'unix-and-chmod' which doesn't quite make sense as our controller is named 'items' and having a separate method for each item is madness. Well, you're right. We're going to use CodeIgniter's 'Routes' to forward all 'item' requests to a 'details' method in our 'items' controller to help keep our URLs short.

Inside system/application/config/routes.php, after the 'default_controller' and 'scaffolding_trigger' options add:

1
 
2
$route['item/:any'] = 'items/details';

What this piece of code does is internally forward all requests to item/ (an 'item' controller) to items/details ('items' controller - 'details' method).

So inside controllers/items.php add the following method:

1
 
2
function details() { // ROUTE: item/{name}/{id} 

3
  echo 'Hello, World!'; 
4
}

When writing methods which are the result of a route, as this one is, I like to include a comment describing the route's path.

Click the link for either item on the main page and you should see the infamous 'Hello, World!' message.

Now we're sure routes are working fine, replace the 'Hello, World!' echo statement with the following:

1
 
2
$id = $this->uri->segment( 3 ); 
3
$item = $this->Item->get( $id ); 
4
 
5
if ( ! $item ) { 
6
  $this->session->set_flashdata( 'error', 'Item not found.' ); 
7
  redirect( 'items' ); 
8
} 
9
 
10
$data['page_title'] = $item->name; 
11
$data['item'] = $item; 
12
 
13
$this->load->view( 'header', $data ); 
14
$this->load->view( 'items/details', $data ); 
15
$this->load->view( 'footer', $data );

On the first line we grab the ID from the third segment of the URL, then use it to get the item with that ID (we'll create the get() method in the model after).

If the item can't be found in the database, we set an 'Item not found' error message in the user's session and redirect them back to the main page.

We set the page title to the item's name, make the item accessible in the view and load the relevant view files.

Model – Get a Single Item

Inside models/items_model.php add the following method:

1
 
2
function get( $id ) { 
3
  $r = $this->db->where( 'id', $id )->get( 'items' )->result(); 
4
  if ( $r ) return $r[0]; 
5
  return false; 
6
}

On the first line we query the database for an entry with the provided ID in the 'items' table. We get the result and store it in variable $r.

If an entry was found, we return the first array item. Otherwise, we return false.

Displaying Errors

In our controller we set an error message in the user's session using set_flashdata() if an item isn't found. To display that error in the browser, add the following to the bottom of views/header.php:

1
 
2
<?php 
3
if ( $this->session->flashdata( 'success' ) ) 
4
  echo '<p class="success">' . $this->session->flashdata( 'success' ) . '</p>'; 
5
 
6
if ( $this->session->flashdata( 'error' ) ) 
7
  echo '<p class="error">' . $this->session->flashdata( 'error' ) . '</p>'; 
8
?>

This simply displays a 'success' or 'error' message if either exist in the user's session.

Note: Anything stored in 'flashdata' (as our success and error messages are) are displayed only on the next page the user loads - CodeIgniter clears them on the next page load so they only display once.

Single Item View

Create a file named views/items/details.php and type the following inside:

1
 
2
<h2><?php echo $item->name . '&ndash; $' . $item->price; ?></h2> 
3
 
4
<p><?php echo nl2br( $item->description ); ?></p> 
5
 
6
<?php $segments = array( 'purchase', url_title( $item->name, 'dash', true ), $item->id ); ?> 
7
<p class="purchase"><?php echo anchor( $segments, 'Purchase' ); ?></p>

Here, we're simply displaying the item's name and price in the header, we use the nl2br() function to convert SQL-style linebreaks in the item's description into HTML-style <br /> tags. We then create a SEO-friendly link to a purchase page (for example http://example.com/purchase/unix-and-chmod/1).



Purchase Page

First we need to add a new route to direct requests for a 'purchase' link to 'items/purchase' instead - just as we did with the single item pages. Add the following to config/routes.php:

1
 
2
$route['purchase/:any'] = 'items/purchase';

Now, inside controllers/items.php add the following 'purchase' method:

1
 
2
function purchase() { // ROUTE: purchase/{name}/{id} 

3
  $item_id = $this->uri->segment( 3 );  
4
  $item = $this->Item->get( $item_id ); 
5
   
6
  if ( ! $item ) { 
7
    $this->session->set_flashdata( 'error', 'Item not found.' ); 
8
    redirect( 'items' ); 
9
  } 
10
   
11
  $data['page_title'] = 'Purchase &ldquo;' . $item->name . '&rdquo;'; 
12
  $data['item'] = $item; 
13
   
14
  $this->load->view( 'header', $data ); 
15
  $this->load->view( 'items/purchase', $data ); 
16
  $this->load->view( 'footer', $data ); 
17
}

It's basically the same as the single item view, just with 'Purchase “ ... ” ' in the page title and we're loading the items/purchase view instead.

The View

Create a file at views/items/purchase.php with the following inside:

1
 
2
<h2>Purchase</h2> 
3
 
4
<?php $segments = array( 'item', url_title( $item->name, 'dash', true ), $item->id ); ?> 
5
<p>To purchase &ldquo;<?php echo anchor( $segments, $item->name ); ?>&rdquo;, enter your email 
6
address below and click through to pay with PayPal. Upon confirmation of your payment, we will 
7
email you your download link to the address you enter below.</p> 
8
 
9
<?php 

10
$url_title = url_title( $item->name, 'dash', true ); 

11
echo form_open( 'purchase/' . $url_title . '/' . $item->id ); 

12
echo validation_errors( '<p class="error">', '</p>' ); 

13
?> 
14
  <p> 
15
    <label for="email">Email:</label> 
16
    <input type="email" name="email" id="email" /> &nbsp; 
17
    <input type="submit" value="Pay $<?php echo $item->price; ?> via PayPal" /> 
18
  </p> 
19
</form>

Here we use CodeIgniter's form helper to create the opening tag for the form which directs back to the current page. In the form we collect the user's email address so we can send them their download link after once we receive confirmation of their purchase from PayPal.


Interfacing with PayPal

To communicate with PayPal, we're going to use a version of the PayPal Lib CodeIgniter library by Ran Aroussi which I modified to include the recommended changes from the Wiki page and add easy support for PayPal's Sandbox for development purposes.

PayPal Lib

Create a new file under application/config/ named paypallib_config.php with the following inside:

1
 
2
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
3
 
4
// ------------------------------------------------------------------------ 

5
// Ppal (Paypal IPN Class) 

6
// ------------------------------------------------------------------------ 

7
 
8
// If (and where) to log ipn to file 

9
$config['paypal_lib_ipn_log_file'] = BASEPATH . 'logs/paypal_ipn.log'; 
10
$config['paypal_lib_ipn_log'] = TRUE; 
11
 
12
// Where are the buttons located at  

13
$config['paypal_lib_button_path'] = 'buttons'; 
14
 
15
// What is the default currency? 

16
$config['paypal_lib_currency_code'] = 'USD'; 
17
 
18
// Enable Sandbox mode? 

19
$config['paypal_lib_sandbox_mode'] = TRUE;

And inside application/libraries/ create a file named Paypal_Lib.php (the capital letters are important) with the following inside:

1
 
2
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');  
3
/** 

4
 * Code Igniter 

5
 * 

6
 * An open source application development framework for PHP 4.3.2 or newer 

7
 * 

8
 * @package		CodeIgniter 

9
 * @author		Rick Ellis 

10
 * @copyright	Copyright (c) 2006, pMachine, Inc. 

11
 * @license		http://www.codeignitor.com/user_guide/license.html 

12
 * @link		http://www.codeigniter.com 

13
 * @since		Version 1.0 

14
 * @filesource 

15
 */ 
16
 
17
// ------------------------------------------------------------------------ 

18
 
19
/** 

20
 * PayPal_Lib Controller Class (Paypal IPN Class) 

21
 * 

22
 * This CI library is based on the Paypal PHP class by Micah Carrick 

23
 * See www.micahcarrick.com for the most recent version of this class 

24
 * along with any applicable sample files and other documentaion. 

25
 * 

26
 * This file provides a neat and simple method to interface with paypal and 

27
 * The paypal Instant Payment Notification (IPN) interface.  This file is 

28
 * NOT intended to make the paypal integration "plug 'n' play". It still 

29
 * requires the developer (that should be you) to understand the paypal 

30
 * process and know the variables you want/need to pass to paypal to 

31
 * achieve what you want.   

32
 * 

33
 * This class handles the submission of an order to paypal as well as the 

34
 * processing an Instant Payment Notification. 

35
 * This class enables you to mark points and calculate the time difference 

36
 * between them.  Memory consumption can also be displayed. 

37
 * 

38
 * The class requires the use of the PayPal_Lib config file. 

39
 * 

40
 * @package     CodeIgniter 

41
 * @subpackage  Libraries 

42
 * @category    Commerce 

43
 * @author      Ran Aroussi <ran@aroussi.com> 

44
 * @copyright   Copyright (c) 2006, http://aroussi.com/ci/ 

45
 * 

46
 */ 
47
 
48
// ------------------------------------------------------------------------ 

49
 
50
class Paypal_Lib { 
51
 
52
	var $last_error;			// holds the last error encountered 

53
	var $ipn_log;				// bool: log IPN results to text file? 

54
 
55
	var $ipn_log_file;			// filename of the IPN log 

56
	var $ipn_response;			// holds the IPN response from paypal	 

57
	var $ipn_data = array();	// array contains the POST values for IPN 

58
	var $fields = array();		// array holds the fields to submit to paypal 

59
 
60
	var $submit_btn = '';		// Image/Form button 

61
	var $button_path = '';		// The path of the buttons 

62
 
63
	var $CI; 
64
 
65
	function Paypal_Lib() 
66
	{ 
67
		$this->CI =& get_instance(); 
68
		$this->CI->load->helper('url'); 
69
		$this->CI->load->helper('form'); 
70
		$this->CI->load->config('paypallib_config'); 
71
 
72
		$this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr'; 
73
		if ( $this->CI->config->item('paypal_lib_sandbox_mode') ) 
74
			$this->paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
75
 
76
		$this->last_error = ''; 
77
		$this->ipn_response = ''; 
78
 
79
		$this->ipn_log_file = $this->CI->config->item('paypal_lib_ipn_log_file'); 
80
		$this->ipn_log = $this->CI->config->item('paypal_lib_ipn_log');  
81
 
82
		$this->button_path = $this->CI->config->item('paypal_lib_button_path'); 
83
 
84
		// populate $fields array with a few default values.  See the paypal 

85
		// documentation for a list of fields and their data types. These defaul 

86
		// values can be overwritten by the calling script. 

87
		$this->add_field('rm','2');			  // Return method = POST 

88
		$this->add_field('cmd','_xclick'); 
89
 
90
		$this->add_field('currency_code', $this->CI->config->item('paypal_lib_currency_code')); 
91
	    $this->add_field('quantity', '1'); 
92
		$this->button('Pay Now!'); 
93
	} 
94
 
95
	function button($value) 
96
	{ 
97
		// changes the default caption of the submit button 

98
		$this->submit_btn = form_submit('pp_submit', $value); 
99
	} 
100
 
101
	function image($file) 
102
	{ 
103
		$this->submit_btn = '<input type="image" name="add" src="' . base_url() . $this->button_path .'/'. $file . '" border="0" />'; 
104
	} 
105
 
106
 
107
	function add_field($field, $value)  
108
	{ 
109
		// adds a key=>value pair to the fields array, which is what will be  

110
		// sent to paypal as POST variables.  If the value is already in the  

111
		// array, it will be overwritten. 

112
		$this->fields[$field] = $value; 
113
	} 
114
 
115
	function paypal_get_request_link() { 
116
		$url = $this->paypal_url . '?'; 
117
 
118
		foreach ( $this->fields as $name => $value ) 
119
			$url .= $name . '=' . $value . "&"; 
120
 
121
		return $url; 
122
	} 
123
 
124
	function paypal_auto_form()  
125
	{ 
126
		// this function actually generates an entire HTML page consisting of 

127
		// a form with hidden elements which is submitted to paypal via the  

128
		// BODY element's onLoad attribute.  We do this so that you can validate 

129
		// any POST vars from you custom form before submitting to paypal.  So  

130
		// basically, you'll have your own form which is submitted to your script 

131
		// to validate the data, which in turn calls this function to create 

132
		// another hidden form and submit to paypal. 

133
 
134
		$this->button('Click here if you\'re not automatically redirected...'); 
135
 
136
		echo '<html>' . "\n"; 
137
		echo '<head><title>Processing Payment...</title></head>' . "\n"; 
138
		echo '<body onLoad="document.forms[\'paypal_auto_form\'].submit();">' . "\n"; 
139
		echo '<p>Please wait, your order is being processed and you will be redirected to the paypal website.</p>' . "\n"; 
140
		echo $this->paypal_form('paypal_auto_form'); 
141
		echo '</body></html>'; 
142
	} 
143
 
144
	function paypal_form($form_name='paypal_form')  
145
	{ 
146
		$str = ''; 
147
		$str .= '<form method="post" action="'.$this->paypal_url.'" name="'.$form_name.'"/>' . "\n"; 
148
		foreach ($this->fields as $name => $value) 
149
			$str .= form_hidden($name, $value) . "\n"; 
150
		$str .= '<p>'. $this->submit_btn . '</p>'; 
151
		$str .= form_close() . "\n"; 
152
 
153
		return $str; 
154
	} 
155
 
156
	function validate_ipn() 
157
	{ 
158
		// parse the paypal URL 

159
		$url_parsed = parse_url($this->paypal_url);		   
160
 
161
		// generate the post string from the _POST vars aswell as load the 

162
		// _POST vars into an arry so we can play with them from the calling 

163
		// script. 

164
		$post_string = '';	  
165
		if (isset($_POST)) 
166
        { 
167
            foreach ($_POST as $field=>$value) 
168
            {       // str_replace("\n", "\r\n", $value) 

169
                    // put line feeds back to CR+LF as that's how PayPal sends them out 

170
                    // otherwise multi-line data will be rejected as INVALID 

171
 
172
                $value = str_replace("\n", "\r\n", $value); 
173
                $this->ipn_data[$field] = $value; 
174
                $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
175
 
176
            } 
177
        } 
178
 
179
		$post_string.="cmd=_notify-validate"; // append ipn command 

180
 
181
		// open the connection to paypal 

182
		$fp = fsockopen($url_parsed['host'],"80",$err_num,$err_str,30);  
183
		if(!$fp) 
184
		{ 
185
			// could not open the connection.  If loggin is on, the error message 

186
			// will be in the log. 

187
			$this->last_error = "fsockopen error no. $errnum: $errstr"; 
188
			$this->log_ipn_results(false);		  
189
			return false; 
190
		}  
191
		else 
192
		{  
193
			// Post the data back to paypal 

194
			fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");  
195
			fputs($fp, "Host: $url_parsed[host]\r\n");  
196
			fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");  
197
			fputs($fp, "Content-length: ".strlen($post_string)."\r\n");  
198
			fputs($fp, "Connection: close\r\n\r\n");  
199
			fputs($fp, $post_string . "\r\n\r\n");  
200
 
201
			// loop through the response from the server and append to variable 

202
			while(!feof($fp)) 
203
				$this->ipn_response .= fgets($fp, 1024);  
204
 
205
			fclose($fp); // close connection 

206
		} 
207
 
208
		if (eregi("VERIFIED",$this->ipn_response)) 
209
		{ 
210
			// Valid IPN transaction. 

211
			$this->log_ipn_results(true); 
212
			return true;		  
213
		}  
214
		else  
215
		{ 
216
			// Invalid IPN transaction.  Check the log for details. 

217
			$this->last_error = 'IPN Validation Failed.'; 
218
			$this->log_ipn_results(false);	 
219
			return false; 
220
		} 
221
	} 
222
 
223
	function log_ipn_results($success)  
224
	{ 
225
		if (!$this->ipn_log) return;  // is logging turned off? 

226
 
227
		// Timestamp 

228
		$text = '['.date('m/d/Y g:i A').'] - ';  
229
 
230
		// Success or failure being logged? 

231
		if ($success) $text .= "SUCCESS!\n"; 
232
		else $text .= 'FAIL: '.$this->last_error."\n"; 
233
 
234
		// Log the POST variables 

235
		$text .= "IPN POST Vars from Paypal:\n"; 
236
		foreach ($this->ipn_data as $key=>$value) 
237
			$text .= "$key=$value, "; 
238
 
239
		// Log the response from the paypal server 

240
		$text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response; 
241
 
242
		// Write to log 

243
		$fp=fopen($this->ipn_log_file,'a'); 
244
		fwrite($fp, $text . "\n\n");  
245
 
246
		fclose($fp);  // close file 

247
	} 
248
 
249
 
250
	function dump()  
251
	{ 
252
		// Used for debugging, this function will output all the field/value pairs 

253
		// that are currently defined in the instance of the class using the 

254
		// add_field() function. 

255
 
256
		ksort($this->fields); 
257
		echo '<h2>ppal->dump() Output:</h2>' . "\n"; 
258
		echo '<code style="font: 12px Monaco, \'Courier New\', Verdana, Sans-serif;  background: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0; padding: 12px 10px;">' . "\n"; 
259
		foreach ($this->fields as $key => $value) echo '<strong>'. $key .'</strong>:	'. urldecode($value) .'<br/>'; 
260
		echo "</code>\n"; 
261
	} 
262
 
263
}

Send Details to PayPal

When a user enters their email address into the form on the 'Purchase' page, we want to:

  1. Verify the user has entered an email address;
  2. Add their email address to our database, along with a random key which we'll use to reference their purchase;
  3. Send the item's details, along with the user's random key, to PayPal to process payment.

Inside controllers/items.php, add the following before the $data['page_title'] = ... line inside the purchase() method:

1
 
2
$this->form_validation->set_rules( 'email', 'Email', 'required|valid_email|max_length[127]' ); 
3
 
4
if ( $this->form_validation->run() ) { 
5
  $email = $this->input->post( 'email' ); 
6
   
7
  $key = md5( $item_id . time() . $email . rand() ); 
8
  $this->Item->setup_payment( $item->id, $email, $key ); 
9
   
10
  $this->load->library( 'Paypal_Lib' ); 
11
  $this->paypal_lib->add_field( 'business', $this->config->item( 'paypal_email' )); 
12
  $this->paypal_lib->add_field( 'return', site_url( 'paypal/success' ) ); 
13
  $this->paypal_lib->add_field( 'cancel_return', site_url( 'paypal/cancel' ) ); 
14
  $this->paypal_lib->add_field( 'notify_url', site_url( 'paypal/ipn' ) ); // <-- IPN url 

15
   
16
  $this->paypal_lib->add_field( 'item_name', $item->name ); 
17
  $this->paypal_lib->add_field( 'item_number', '1' ); 
18
  $this->paypal_lib->add_field( 'amount', $item->price ); 
19
   
20
  $this->paypal_lib->add_field( 'custom', $key ); 
21
   
22
  redirect( $this->paypal_lib->paypal_get_request_link() ); 
23
}

On the first line we check that the submitted 'email' form field is a valid email address. Everything inside the if loop on line 3 will run if the 'email' field validated.

On line 6 we generate a unique key for the current purchase by hashing the item's ID, the current time, the user's email address and a random number using a MD5 hash.

Line 7 adds the item ID, user's email and the random key to the database via the setup_payment() method we have yet to create.

The rest of the code loads the 'Paypal_Lib' library and adds details about the item and site using the PayPal library's functions. We also pass the random key we generated to PayPal using the 'custom' field. PayPal will send this key back to us upon payment confirmation so we can activate the user's download.

Finally, we redirect the user to the PayPal payment page.

Now we need to create the setup_payment() function in the model. So inside models/items_model.php add the following:

1
 
2
function setup_payment( $item_id, $email, $key ) { 
3
  $data = array( 
4
    'item_id'  => $item_id, 
5
    'key'      => $key, 
6
    'email'    => $email, 
7
    'active'   => 0 // hasn't been purchased yet 

8
  ); 
9
  $this->db->insert( 'purchases', $data ); 
10
}

This is pretty simple: we create an array containing the provided item ID, user's email address and random key. We also set 'active' to 0 (this gets set to '1' upon payment confirmation). The array is then inserted into the 'purchases' table.

PayPal's Process

With the system we're using to interface with PayPal, once a user has completed payment, PayPal will send the user to a 'success' URL which we provide. This page will simply say "Your payment has been received. We're currently processing it".

Behind-the-scenes, PayPal will send our 'IPN listener' confirmation of the payment and some details about it and the listener sends these details back to PayPal to confirm the message is genuine. It's after our IPN listener receives this second confirmation that we process the purchase and activate the user's download.

We're going to create a new controller to handle these requests from PayPal. So inside controllers/ create a file named paypal.php and enter the following inside:

1
 
2
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 
3
 
4
class Paypal extends Controller { 
5
 
6
  function Paypal() { 
7
    parent::Controller(); 
8
    $this->load->model( 'items_model', 'Item' ); 
9
    $this->load->library( 'Paypal_Lib' ); 
10
    $data['site_name'] = $this->config->item( 'site_name' ); 
11
    $this->load->vars( $data ); 
12
  } 
13
 
14
  function index() { 
15
    redirect( 'items' ); 
16
  } 
17
 
18
  function success() { 
19
    $this->session->set_flashdata( 'success', 'Your payment is being processed now. Your download link will be emailed to your shortly.' ); 
20
    redirect( 'items' ); 
21
  } 
22
 
23
  function cancel() { 
24
    $this->session->set_flashdata( 'success', 'Payment cancelled.' ); 
25
    redirect( 'items' ); 
26
    } 
27
 
28
}

That's the 'success' and 'cancel' pages (cancel is used with a user doesn't continue with the payment and clicks the cancel button on PayPal instead).

PayPal Developer Tools

PayPal provide a 'Sandbox' for developers to test their code with. You can create your own sandbox PayPal addresses to send pretend payments. Sign up for a free developer account at https://developer.paypal.com/ then go to 'Create a preconfigured buyer or seller account':


Fill out the form to create a new buyer account, enter a balance and click on through. On the 'Test Accounts' page you will find the email address for your new Sandbox buyer email address:


Now go back to the site we're creating and click through to purchase an item. Notice that when you get to PayPal, the address is https://www.sandbox.paypal.com/..... Login with the 'buyer' account you created on the right:


Continue through the payment process and click the 'Return to Merchant' button upon completion. You should be directed back to your homepage, with the "Your payment is being processed now. Your download link will be emailed to your shortly." message below the header.


The main interface of the site is now complete. We just need to add in our IPN listener and email the item to the buyer.


IPN Listener

As mentioned above, once PayPal has confirmed payment, it will send data to our IPN listener, once we validate the data with PayPal (to prevent fraudulent data), we can use the data to activate the buyer's purchase.

The IPN function is a little big, so I'll break it up. Inside the PayPal controller, add the following function:

1
 
2
function ipn() { 
3
  if ( $this->paypal_lib->validate_ipn() ) { 
4
    $item_name = $this->paypal_lib->ipn_data['item_name']; 
5
    $price = $this->paypal_lib->ipn_data['mc_gross']; 
6
    $currency = $this->paypal_lib->ipn_data['mc_currency']; 
7
    $payer_email = $this->paypal_lib->ipn_data['payer_email']; 
8
    $txn_id = $this->paypal_lib->ipn_data['txn_id']; 
9
    $key = $this->paypal_lib->ipn_data['transaction_subject']; 
10
     
11
    $this->Item->confirm_payment( $key, $payer_email, $txn_id ); 
12
    $purchase = $this->Item->get_purchase_by_key( $key ); 
13
    $item = $this->Item->get( $purchase->item_id ); 
14
  } 
15
}

Right at the start we validate the data sent to the listener with PayPal – the library takes care of all this. If the data is valid, we grab a some details (the item name, price, currency, the payer's PayPal email address, the transaction ID and the unique key we sent to PayPal when the payment process began).

We can then use the key to confirm the payment (by setting the 'active' field to '1') and add the payer's PayPal email and transaction ID to the database for future reference.

Using the key we can get the purchase details from the database, along with the item purchased. Before continuing with the IPN function, let's create the confirm_payment() and get_purchase_by_key() model methods.

So inside the model, add the following:

1
 
2
function confirm_payment( $key, $paypal_email, $payment_txn_id ) { 
3
  $data = array( 
4
    'purchased_at'  => time(), 
5
    'active'        => 1, 
6
    'paypal_email'  => $paypal_email, 
7
    'paypal_txn_id' => $paypal_txn_id 
8
  ); 
9
  $this->db->where( 'key', $key ); 
10
  $this->db->update( 'purchases', $data ); 
11
} 
12
 
13
function get_purchase_by_key( $key ) { 
14
  $r = $this->db->where( 'key', $key )->get( 'purchases' )->result(); 
15
  if ( $r ) return $r[0]; 
16
  return false; 
17
}

The functions should be pretty self explanatory by now, so now we need to email the customer their download link. This is handled in the IPN listender, so back in the controller add the following to the end of the ipn() function:

1
 
2
// Send download link to customer 

3
$to = $purchase->email; 
4
$from = $this->config->item( 'no_reply_email' ); 
5
$name = $this->config->item( 'site_name' ); 
6
$subject = $item->name . ' Download'; 
7
 
8
$segments = array( 'item', url_title( $item->name, 'dash', true ), $item->id ); 
9
$message = '<p>Thanks for purchasing ' . anchor( $segments, $item->name ) . ' from ' . anchor( '', $name ) . '. Your download link is below.</p>'; 
10
$message .= '<p>' . anchor( 'download/' . $key ) . '</p>'; 
11
 
12
$this->email->from( $from, $name ); 
13
$this->email->to( $to ); 
14
$this->email->subject( $subject ); 
15
$this->email->message( $message ); 
16
$this->email->send(); 
17
$this->email->clear();

Here we're using CodeIgniter's Email class to send the email. We start by setting up 'To', 'From', 'Name' and 'Subject' variables with the relevant data.

We then write a short message for the body with a link to the file they purchased, followed by their download link (which will be in the format of: http://example.com/download/{key}). Finally we add the variables into the Email class methods and send it.

The final thing we need in the IPN listener is to send the site's admin an email with the transaction details. Add the following to the end of the ipn() function:

1
 
2
// Send confirmation of purchase to admin 

3
$message = '<p><strong>New Purchase:</strong></p><ul>'; 
4
$message .= '<li><strong>Item:</strong> ' . anchor( $segments, $item->name ) . '</li>'; 
5
$message .= '<li><strong>Price:</strong> $' . $item->price . '</li>'; 
6
$message .= '<li><strong>Email:</strong> ' . $purchase->email . '</li><li></li>'; 
7
$message .= '<li><strong>PayPal Email:</strong> ' . $payer_email . '</li>'; 
8
$message .= '<li><strong>PayPal TXN ID:</strong> ' . $txn_id . '</li></ul>'; 
9
$this->email->from( $from, $name ); 
10
$this->email->to( $this->config->item( 'admin_email' ) ); 
11
$this->email->subject( 'A purchase has been made' ); 
12
$this->email->message( $message ); 
13
$this->email->send(); 
14
$this->email->clear();

IMPORTANT! The IPN listener won't work if you're running on a local server ('localhost'). Clearly if PayPal attempted to visit http://localhost/paypal/ipn/ they're not going to arrive at your system. Upload your files to a remote server accessible by a domain name, or external IP address, for this to work.


File Downloads

The final step to getting the site fully working is to get the download links to work. When a customer goes to the download link we email them (eg. http://example.com/download/{key}), we use their key to look up the download. If the purchase associated with the key is set to active (payment fulfilled) and the file exists on the server, the download will start.

First thing we need to do is add another route to set /download/ requests to go to items/download. Add the following to your config/routes.php file:

1
 
2
$route['download/:any'] = 'items/download';

Now, inside your items controller, add the following download() method:

1
 
2
function download() { // ROUTE: download/{purchase_key} 

3
  $key = $this->uri->segment( 2 ); 
4
  $purchase = $this->Item->get_purchase_by_key( $key ); 
5
   
6
  // Check purchase was fulfilled 

7
  if ( ! $purchase ) { 
8
    $this->session->set_flashdata( 'error', 'Download key not valid.' ); 
9
    redirect( 'items' ); 
10
  } 
11
  if ( $purchase->active == 0 ) { 
12
    $this->session->set_flashdata( 'error', 'Download not active.' ); 
13
    redirect( 'items' ); 
14
  } 
15
   
16
  // Get item and initiate download if exists 

17
  $item = $this->Item->get( $purchase->item_id ); 
18
   
19
  $file_name = $item->file_name; 
20
  $file_data = read_file( 'files/' . $file_name ); 
21
   
22
  if ( ! $file_data ) { // file not found on server 

23
    $this->session->set_flashdata( 'error', 'The requested file was not found. Please contact us to resolve this.' ); 
24
    redirect( 'items' ); 
25
  } 
26
	 
27
  force_download( $file_name, $file_data ); 
28
}

In the first couple lines we lookup the purchase using the key in the URL. On line 6, if we can't find a purchase record with that key, we set an error message that the key is invalid and redirect the user to the homepage. Similarly, on line 10, if the purchase was not fulfilled (active is '0'), we display an error.

We then retrieve the actual item from the database, and retrieve the file name. We use the read_file() method from CodeIgniter's File helper to get the contents of the file. If the file can't be found (or is empty), we display an error. Otherwise, we use the force_download() method to initiate a download.

Item Theft

We do have one problem—it's possible to guess the name of a file and download it directly (ie. by visiting http://example.com/files/UNIX and CHMOD.txt). To fix this, simply create a file named .htaccess inside the files/ directory with the following line inside:

1
 
2
deny from all

This tells the server to deny any requests for files in this directory – but the server itself can still access the files so buyers can still download using their own unique link.


Stylish

At the root of your app (same location as the files and system directories), create a new directory named css, inside it create a file named style.css and add the following inside to spice things up a little:

1
 
2
body { 
3
  background-color: #f9f9f9; 
4
  color: #222; 
5
  font-family: sans-serif; 
6
} 
7
 
8
#wrap { 
9
  background-color: #fff; 
10
  border: 1px solid #ddd; 
11
  border-radius: 5px; 
12
  -moz-border-radius: 5px; 
13
  -webkit-border-radius: 5px; 
14
  box-shadow: #e6e6e6 0 0 15px; 
15
  -moz-box-shadow: #e6e6e6 0 0 15px; 
16
  -webkit-box-shadow: #e6e6e6 0 0 15px; 
17
  margin: 15px auto; 
18
  padding: 15px; 
19
  width: 760px; 
20
} 
21
 
22
a { 
23
  color: #24badb; 
24
  text-decoration: none; 
25
} 
26
 
27
header h1 { 
28
  text-align: center; 
29
} 
30
 
31
header a { 
32
  color: #222; 
33
  padding: 7px 10px; 
34
} 
35
 
36
header a:hover { 
37
  background-color: #222; 
38
  color: #fff; 
39
} 
40
 
41
li { 
42
  margin-bottom: 10px; 
43
} 
44
 
45
section { 
46
  line-height: 1.5em; 
47
} 
48
 
49
section a { 
50
  padding: 3px 4px; 
51
} 
52
 
53
section a:hover { 
54
  background-color: #24badb; 
55
  color: #fff; 
56
} 
57
 
58
footer { 
59
  color: #bbb; 
60
  text-align: right; 
61
} 
62
 
63
footer a { 
64
  color: #bbb; 
65
} 
66
 
67
footer a:hover { 
68
  color: #a0a0a0; 
69
}


BONUS: Download Limits

You may want to limit how often a user may download their purchase within a certain time period (perhaps to stop them sharing the download link around). Implementing this feature doesn't take much work, so let's add it in now.

We already set up a database table to log file downloads–'downloads'–which we haven't used yet. We also have the 'Download Limit' setting in the config/config.php file, so go ahead and 'enable' it now:

1
 
2
/* 

3
|-------------------------------------------------------------------------- 

4
| Download Limit 

5
|-------------------------------------------------------------------------- 

6
| 

7
| How many times can an item be downloaded within a certain time frame? 

8
|   eg. 4 downloads within 7 days 

9
| 

10
*/ 
11
$config['download_limit'] = array( 
12
	'enable'	=> true, 
13
	'downloads'	=> '4', 
14
	'days'		=> '7' 
15
);

The default setting is to allow up to four file downloads in a seven day period. If, for example, the buyer tries to download five times in seven days, we'll forward them back to the home page and display an error explaining why we can't serve up their download right now.

The first thing we need to do is keep a log every time a download is initiated. To do this, add the following directly before the force_download(...) statement at the end of the download() function in the Items controller:

1
 
2
$this->Item->log_download( $item->id, $purchase->id, $this->input->ip_address(), $this->input->user_agent() );

Here we're sending the item id, purchase id and the user's IP address and user agent to a log_download() method in the Items model which we'll create next.

Add the following method to your Items_model:

1
 
2
function log_download( $item_id, $purchase_id, $ip_address, $user_agent ) { 
3
  $data = array( 
4
    'item_id'      => $item_id, 
5
    'purchase_id'  => $purchase_id, 
6
    'download_at'  => time(), 
7
    'ip_address'   => $ip_address, 
8
    'user_agent'   => $user_agent 
9
  ); 
10
  $this->db->insert( 'downloads', $data ); 
11
}

This simply adds the data we provided, and the current time, to the 'downloads' table.

We'll also need a method to get the downloads of a purchase, so add the following to the model:

1
 
2
function get_purchase_downloads( $purchase_id, $limit ) { 
3
  return $this->db->where( 'purchase_id', $purchase_id )->limit( $limit )->order_by( 'id', 'desc' )->get( 'downloads' )->result(); 
4
}

Now, to actually add in the download limit, find the following piece of code in the download() method in the Items controller:

1
 
2
// Check purchase was fulfilled 

3
if ( ! $purchase ) { 
4
  $this->session->set_flashdata( 'error', 'Download key not valid.' ); 
5
  redirect( 'items' ); 
6
} 
7
if ( $purchase->active == 0 ) { 
8
  $this->session->set_flashdata( 'error', 'Download not active.' ); 
9
  redirect( 'items' ); 
10
}

Directly after this, add the following:

1
 
2
// Check download limit 

3
$download_limit = $this->config->item( 'download_limit' ); 
4
if ( $download_limit['enable'] ) { 
5
  $downloads = $this->Item->get_purchase_downloads( $purchase->id, $download_limit['downloads'] ); 
6
  $count = 0; 
7
  $time_limit = time() - (86400 * $download_limit['days']); 
8
  foreach ( $downloads as $download ) { 
9
    if ( $download->download_at >= $time_limit ) 
10
      $count++; // download within past x days 

11
    else 
12
      break; // later than x days, so can stop foreach 

13
  } 
14
 
15
  // If over download limit, error 

16
  if ( $count >= $download_limit['downloads'] ) { // can only download x times within y days 

17
    $this->session->set_flashdata( 'error', 'You can only download a file ' . $download_limit['downloads'] . ' times in a ' . $download_limit['days'] . ' day period. Please try again later.' ); 
18
    redirect( 'items' ); 
19
  } 
20
}

On the third line we check whether the 'Download Limit' functionality is enabled in the config file. If it is, we retrieve the downloads of the current purchase (limited to how many file downloads is permitted).

At line 6, we calculate the furthest time away downloads are limited to (eg. if we have a limit of 4 downloads in 7 days, we find the time 7 days ago) by multiplying the number of days by 86400 (the number of seconds in a day) and subtracting it from the current time.

The loop starting at line 7 checks each download logged to see if it was downloaded within the time limit (eg. 7 days). If it is, we increase $count, otherwise, we break out of the loop as we know if this logged download is older than the limit, all subsequent logs will be to.

At line 15, if the $count is greater than the number of downloads allowed, we display an error message. Otherwise, the rest of the code will be executed and the download will be logged and initiated.


We're done. Try it out!

Note: To disable the PayPal 'sandbox' mode so you can recieve real payments, change the $config['paypal_lib_sandbox_mode'] option in config/paypallib_config.php to false.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.