PayPal Integration And IPN (Instant Payment Notification)

  1. Introduction
  2. Preparation
  3. Implementation of payments
  4. IPN (Instant Payment Notification) Listener Script
  5. That's about it folks


Had to implement PayPal payments for a client and would like to share part of my code with you. Because the tutorial will become too long, I'll leave some code to be done by you, e.g. creating models, controllers and db tables for products, orders.


Sign up a developer account
  1. Head to and sign up a developer account.
  2. Grap a cool PHP IPN Listener from and put the file IpnListener.php in protected/components/ directory. On my host the default setting for using CURL did not work, so I had to change the property: public $use_curl = false; in that class to use fsockopen();
  3. Log in and create test accounts. One for merchant and one for client.
    Configure Yii

    Edit protected/config/main.php and add blocks of code for LIVE and DEV environment.

// Define LIVE constant as true if 'localhost' is not present in the host name. Configure the detecting of environment as necessary of course.
defined('LIVE') || define('LIVE', strpos($_SERVER['HTTP_HOST'],'localhost')===false ? true : false);
if (LIVE) {
  define('PAYPAL_HOST', '');
  define('PAYPAL_URL', '');
  define('PAYPAL_EMAIL',''); // live email of merchant
  define('PAYPAL_HOST', '');
  define('PAYPAL_URL', '');
  define('PAYPAL_EMAIL', ''); // dev email of merchant

Implementation of payments

Assuming we're going to use the HTML forms method, in your view script enter:

<div class="form">
	$form=$this->beginWidget('CActiveForm', array(
		'htmlOptions'=>array('onsubmit'=>'return false;')
	// paypal fields
	echo CHtml::hiddenField('cmd','_cart');
	echo CHtml::hiddenField('upload','1');
	echo CHtml::hiddenField('currency_code','EUR'); // enter currency
	echo CHtml::hiddenField('business',PAYPAL_EMAIL);
// set up path to successful order
	echo CHtml::hiddenField('return',Yii::app()->getRequest()->getBaseUrl(true).'order/success');
// set up url to cancel order
	echo CHtml::hiddenField('cancel_return',Yii::app()->getRequest()->getBaseUrl(true).'order/canceled');
// set up path to paypal IPN listener
	echo CHtml::hiddenField('notify_url',Yii::app()->getRequest()->getBaseUrl(true).'order/paypalNotify');
	echo CHtml::hiddenField('item_name_1',$productLang->title); // product title goes here
	echo CHtml::hiddenField('quantity_1','',array('id'=>'paypalQty'));
	echo CHtml::hiddenField('amount_1','',array('id'=>'paypalPrice'));
	echo CHtml::hiddenField('custom','',array('id'=>'paypalOrderId')); // here we will set order id after we create the order via ajax
	echo CHtml::hiddenField('charset','utf-8');
	// order fields
	echo CHtml::hiddenField('currencyCode','EUR'); // currency code
	echo CHtml::submitButton('',array('style'=>'display:none;'));
	<div class="note">Fields with asterisk are required<span class="required">*</span></div>
	<?php echo $form->errorSummary($model); ?>

	<div class="row indent-bot5">
		<?php echo $form->labelEx($model,'qty'); ?>
		<?php echo $form->textField($model,'qty',array('size'=>4)); ?>
		<?php echo $form->error($model,'qty'); ?>

	<div class="row indent-bot5">
		<h2 class="strong">Price: <span id="singlePrice"><?php echo $product->price // model product with property price ?> EURO</span></h2>

	<div class="row indent-bot5">
		<h2 class="strong">Total: <span id="totalPriceTxt"><?php echo $product->price?></span> EURO</h2>

	<div class="row indent-bot5">
		<?php // set path to paypal button image
 echo CHtml::imageButton(bu('images/paypalButton.png'),array('id'=>'paypalBtn','name'=>'paypalBtn',

<?php $this->endWidget(); ?>
<script type="text/javascript">

	function setTotalPrice(){
		var qty=parseInt($('#Order_qty').val(),10);
		qty = isNaN(qty) ? 0 : qty;
		var totalPrice = qty * parseFloat($('#singlePrice').html());

	// create db record in tbl order, update paypalPrice field and submit the form
	function createOrder(btn,totalPrice,action){
		var requiredFields = ['qty']; // enter more required fields (field with id="Order_qty" will be checked for value)
		var error = false;
		$.each(requiredFields, function(key, field) {
				error = true;
				alert('Please fill out all required fields.');
				return false;
		if (error)
			return false;
		if($('#Order_qty').val() > <?php echo $product->qty?>){
			alert('<?php echo Exceeding available quantity. We are sorry for the inconvenience.'); // assuming we have property qty in model $product

// OrderController needs to create a record in tbl order and return response in JSON format (in this case) use json_encode for example if your PHP version supports this function
		$.post(Yii::app()->getRequest()->getBaseUrl().'order/create'; ?>',$('#orderForm').serializeArray(),function(orderResp) {
			if(orderResp.error === undefined){
				var action;
				$('#orderForm').attr({action:'<?php echo PAYPAL_URL?>',onsubmit:true}).submit();

Note: You will have to login in in advance, before making a test payment.

IPN (Instant Payment Notification) Listener Script

The listener script is there to accept the request from PayPal about the status of payments. Remember that we're going to use a ready IPN class and we set the notify URL to be order/paypalNotify? Here's a sample: OrderController::actionPaypalNotify()

public function actionPaypalNotify(){
	$paypal = new PayPal();

This assumes we have a PayPal.php file and PayPal class in protected/components dir.

class PayPal {

	public function notify(){
		$logCat = 'paypal';
		$listener = new IpnListener();
		$listener->use_sandbox = PAYPAL_SANDBOX;
		try {
			if ($listener->processIpn() && $_POST['payment_status']==='Completed') {
				$order = Order::model()->findByPk($_POST['custom']); // we set custom as our order id on sending the request to paypal
				if ($order === null) {
					Yii::log('Cannot find order with id ' . $custom, CLogger::LEVEL_ERROR, $logCat);
					Yii::app()->end(); // note that die; will not execute Yii::log() so we have to use Yii::app()->end();
					'payDate'=>date('Y-m-d H:m:i'), // payDate field in model Order
					'statusId'=>Order::STATUS_PAID // statusId field in model Order
				Product::deductQty($order); // deduct quantity for this product
				Product::sendSuccessEmails($order); // send success emails to merchant and buyer
				Yii::log('invalid ipn', CLogger::LEVEL_ERROR, $logCat);
		} catch (Exception $e) {
			Yii::log($e->getMessage(), CLogger::LEVEL_ERROR, $logCat);


And here's part of Order model just to show some constants and statuses:

class Order extends CActiveRecord
	const STATUS_EXPIRED = 3;
	const STATUS_PAID = 4;
	public $statuses = array(
		self::STATUS_INITIATED => 'Initiated',
		self::STATUS_CANCELED => 'Canceled',
		self::STATUS_EXPIRED => 'Expired',
		self::STATUS_PAID => 'Paid',
// more code of Order model

That's about it folks

Hope the tutorial is clear enough. Will update it if needed. Help me improve it by comments and opinions. Please use the forum if you have any questions. Thank you.

5 0
Viewed: 39 561 times
Version: 1.1
Category: How-tos
Written by: yasen
Last updated by: yasen
Created on: Dec 12, 2012
Last updated: 10 years ago
Update Article


View all history

Related Articles