Wonder how to integrate paypal IPN in Yii framework? Are you searching for Yii extension to integrate paypal IPN into your application? Well, i used to do that until i got fed up with reading all the documentation all excessive code dated by 2012 that hasn't been update for months! So i decided to just integrate paypal ipn alone into my Yii framework and guess what, it was so dead simple that it simply doesn't need any extension to began with.
PayPal IPN Tutorial
Before i began writing this tutorial, you should really understand what is paypal ipn is and why you are integrating it. Paypal IPN refers PayPal's Instant Payment Notification (IPN) which is a service provides by paypal to instantly notify your application whenever there is a payment made to you. And Micah Carrick had explained it perfectly on how to integrate it on a PHP level.
Coding Paypal IPN in Yii Framework
Here we go, firstly, you will need to get the code PHP-PayPal-IPN from github. The library provides you with a simple integration to Paypal IPN. Download the code and placed into your project/protected/vendors/PHP-PayPal-IPN and rename the ipnlistener.php to IpnListener.php.
Next, you will need to create a controller to listen to incoming ipn request and paste the following code into your listener method.
{ class PaymentController extends Controller{ public function actionListen(){ // intantiate the IPN listener Yii::import("application.vendors.PHP-PayPal-IPN.*", true); $listener = new IpnListener(); // tell the IPN listener to use the PayPal test sandbox $listener->use_sandbox = true; // try to process the IPN POST try { $listener->requirePostMethod(); $verified = $listener->processIpn(); } catch (Exception $e) { // error_log($e->getMessage()); Yii::log("IPN FAILED! returned an error: $e",CLogger::LEVEL_ERROR); Yii::app()->end(); } // TODO: Handle IPN Response here if ($verified) { $model = new Transaction('search'); $model->attributes=$_POST; $model->save(); // TODO: Implement additional fraud checks and MySQL storage mail('[email protected]', 'Valid IPN', $listener->getTextReport()); } else { // manually investigate the invalid IPN mail('[email protected]', 'Invalid IPN', $listener->getTextReport()); } } }
Do take note that you will need to give your method public access in order for paypal ipn services to reach it. Now, take note that the model i am using here belongs to my own, you can create your own and save every single data passed in by paypal to your database like what i did but do make sure that the naming convention are the same for both paypal $POST request and your own table fields.
Test Paypal IPN
In order to test your Paypal IPN request, you will need to create a sandbox paypal account and use its Instant Payment Notification (IPN) Simulator as show below,
All you need to do is to scroll it all the way down and click "Send IPN". And you should be able to test your Paypal ipn listener! And guess what? You don't even need to create any sandbox paypal account to create a paypal IPN listener!