Coder Social home page Coder Social logo

cakephp-paypal-ipn-plugin's Introduction

Paypal IPN plugin (Paypal Instant Payment Notification)

Get it

Required:

CakePHP 2.x

Note: CakePHP 1.3 use the cakephp1.3 branch

More From WebTechNick

http://github.com/webtechnick

CHANGELOG:

  • 1.0: Initial release
  • 1.1: Added cleaner routes
  • 2.0: Helper added
  • 2.1: Added cake schema install script
  • 2.2: Added paypal unsubscribe type
  • 2.2.1: Bug fix with subscription issues
  • 2.2.2: Fixed validation issues with paypal button in strict doctype
  • 3.0: Added new basic Paypal IPN email capabality.
  • 3.5 Added checkout feature for multiple items paypal button. Documentation bellow
  • 3.5.1: Renamed columns option_name_1 and option_name_2 to option_name1 and option_name2 respectively
  • 3.5.2: Updating to latest conventions in CakePHP 1.3, no longer requires Auth, all cart items will be reviewable in paypal_items table
  • 3.6.0: Adding View Cart button option
  • 4.0.0: CakePHP 2.x ready.

Install:

  1. Copy plugin into your app/Plugin/PaypalIpn directory

  2. Load the plugin in bootstrap.php

     CakePlugin::load('PaypalIpn');
    
  3. Run the schema to create the required tables.

     $ cake schema create --plugin PaypalIpn
    
  4. Add the following into your /app/Config/Routes.php file (optional):

     /* Paypal IPN plugin */
     Router::connect('/paypal_ipn/process', array('plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'process'));
     /* Optional Route, but nice for administration */
     Router::connect('/paypal_ipn/:action/*', array('admin' => 'true', 'plugin' => 'paypal_ipn', 'controller' => 'instant_payment_notifications', 'action' => 'index'));
     /* End Paypal IPN plugin */
    

Paypal Setup:

  1. I suggest you start a sandbox account at https://developer.paypal.com
  2. Enable IPN in your account.

Administration: (optional) If you want to use the built in admin access to IPNs:

  1. Make sure you're logged in as an Administrator via the Auth component.
  2. Navigate to www.yoursite.com/paypal_ipn

Paypal Button Helper: (optional) if you plan on using the paypal helper for your PayNow or Subscribe Buttons

  1. Update Config/paypal_ipn_config.php with your paypal information

  2. Add PaypalIpn.Paypal to your helpers list in AppController.php

     public $helpers = array('Html','Form','PaypalIpn.Paypal');
    

Usage: (view the actual Plugin/PaypalIpn/View/Helper/PaypalHelper.php for more information)

	$this->Paypal->button(String tittle, Options array); 
 
	$this->Paypal->button('Pay Now', array('amount' => '12.00', 'item_name' => 'test item'));
	$this->Paypal->button('Subscribe', array('type' => 'subscribe', 'amount' => '60.00', 'term' => 'month', 'period' => '2'));
	$this->Paypal->button('Donate', array('type' => 'donate', 'amount' => '60.00'));
	$this->Paypal->button('Add To Cart', array('type' => 'addtocart', 'amount' => '15.00'));
	$this->Paypal->button('View Cart', array('type' => 'viewcart', 'amount' => '15.00'));
	$this->Paypal->button('Unsubscribe', array('type' => 'unsubscribe'));
	$this->Paypal->button('Checkout', array(
		'type' => 'cart',
		'items' => array(
			array('item_name' => 'Item 1', 'amount' => '120', 'quantity' => 2, 'item_number' => '1234'),
			array('item_name' => 'Item 2', 'amount' => '50'),
			array('item_name' => 'Item 3', 'amount' => '80', 'quantity' => 3),
		)
	));
	
	//Test Example
	$this->Paypal->button('Pay Now', array('test' => true, 'amount' => '12.00', 'item_name' => 'test item'));

Alternatively to Paypal Helper

Instead of the Paypal Helper you can use your custom buttons but make sure to set notify_url to your configured route.

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
	...
	<input type="hidden" name="notify_url" value="http://www.yoursite.com/paypal_ipn/process" />
	...
</form>

It is generally recommened to use the paypal helper as it will generate everything for you based on your configurations

Paypal Notification Callback:

Create a function in your app/Controller/AppController.php like so:

function afterPaypalNotification($txnId){
	//Here is where you can implement code to apply the transaction to your app.
	//for example, you could now mark an order as paid, a subscription, or give the user premium access.
	//retrieve the transaction using the txnId passed and apply whatever logic your site needs.
	
	$transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
	$this->log($transaction['InstantPaymentNotification']['id'], 'paypal');
	
	//Tip: be sure to check the payment_status is complete because failure 
	//     are also saved to your database for review.
	
	if ($transaction['InstantPaymentNotification']['payment_status'] == 'Completed') {
		//Yay!  We have monies!
	}	else {
		//Oh no, better look at this transaction to determine what to do; like email a decline letter.
	}
} 

Basic Email Feature:

Utility method to send basic emails based on a paypal IPN transaction. This method is very basic, if you need something more complicated I suggest creating your own method in the afterPaypalNotification function you build in the app_controller.php

$IPN = ClassRegistry::init('PaypalIpn.InstantPaymentNotification');
$IPN->id = '4aeca923-4f4c-49ec-a3af-73d3405bef47';
$IPN->email('Thank you for your transaction!');

//OR passed in as an array of options
$IPN->email(array(
	'id' => '4aeca923-4f4c-49ec-a3af-73d3405bef47',
	'subject' => 'Donation Complete!',
	'message' => 'Thank you for your donation!',
	'sendAs' => 'text'
));

Hint: use this in your afterPaypalNotification callback in your AppController.php

function afterPaypalNotification($txnId){
	ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->email(array(
		'id' => $txnId,
		'subject' => 'Thanks!',
		'message' => 'Thank you for the transaction!'
	));
}

Email Options:

  • id: id of instant payment notification to base email off of
  • subject: subject of email (default: Thank you for your paypal transaction)
  • sendAs: html | text (default: html)
  • to: email address to send email to (default: ipn payer_email)
  • from: from email address (default: ipn business)
  • cc: array of email addresses to carbon copy to (default: array())
  • bcc: array of email addresses to blind carbon copy to (default: array())
  • layout: layout of email to send (default: default)
  • template: template of email to send (default: null)
  • log: boolean true | false if you'd like to log the email being sent. (default: true)
  • message: actual body of message to be sent (default: null)

cakephp-paypal-ipn-plugin's People

Contributors

josegonzalez avatar pur3forlyphe avatar webtechnick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cakephp-paypal-ipn-plugin's Issues

Verification does not validate receiver_email / business

There is a slight problem with the isValid() method. There is no check to see that the receiver_email or business field matches the intended receiver.

The reason this is important is I can configure a paypal account with an IPN post back that points to your process method. The IPN will verify but the funds will be deposited into my account.

I guess this logic could be implemented in the method that you put in the app_controller. This may be confusing as the transaction will be recorded in the database although it is an invalid transaction.

Wait 10s then update order

I have to wait 10 seconds after successful payment will therefore be redirect to the success of the website page of me. So I want after the payment is successful, it automatically to inform my page so I can update the order information

Does this plugin check for duplicate txn_id ?

(Sorry, I don't know how to simply ask something on github without creating an issue...)

I just wanted to know if this plugin checks for duplicate txn_id before processing an IPN, or have we to manage this in the afterPaypalNotification action by ourselves ?

Edit: As a workaround (if the plugin doesn't check for multiples txn_id), I just created an unique constraint on the txn_id field in the db... Hope this won't produce any bug.

Thanks !

Need an exit() on __processTransaction() ?

Hi there,

I spent a ton of time on this issue. Every time a valid post would come through, my cake error.log was being spammed with hundreds (possibly thousands) of lines complaining about headers already being sent. But it wouldn't happen on an invalid post. I ended up resolved it by putting an exit(); right after $this->afterPaypalNotification($txnId); in the __processTransaction() function. I'll paste it as such:

private function __processTransaction($txnId) {
    $this->log("Processing Trasaction: {$txnId}");
    $this->afterPaypalNotification($txnId);
    exit();
}

Not sure exactly why this would happen, but glad it's fixed now. I suppose it could also be a problem with my particular project since I haven't seen any related issues.

Thanks!

Not able to setup Paypal IPN plugin

Hi nick,
i am using your cakephp paypal ipn plugin and i am unable to setup. i have followed all the instructions and i am getting this error

Fatal error: PaypalIpnConfig: The configuration could not be loaded. in /var/app/current/app/Plugin/PaypalIpn/View/Helper/PaypalHelper.php on line 45 Fatal error: PaypalIpnConfig: The configuration could not be loaded. in /var/app/current/app/Plugin/PaypalIpn/View/Helper/PaypalHelper.php on line 45

can u please help me out with this issue. i am using cakephp 2.*

does it support subscription(recurring payment)

Hi does your plugin support recurring payment and consumer profile creation i need to implement a recurring payment system in my existing site where user subscribe for a package and money deducted from his credit/debit or paypal account on every month.
please suggest a easy way to do it in cakephp framework also please mention cons and prons.

Thanks

Dead?

I can haz working webtechnick CakePHP PayPal IPN Plugin?

Returning response code 400 after IPN data processing.

Recently encountered a problem with the plugin that the IPN request gets processed properly but Paypal servers received a 400 response code.

I solved this by explicitly sending a 200 header as soon as notification was received and exiting after processing. Seems like a recent change at Paypal requiring a 200 OK to be sent

Hope this helps someone

PayPal IPN Failed Handshake Fix

I recently received a notification that PayPal IPN notifications sent to my website PayPal listener were failing. I was able to determine that all of the data was successfully received and stored in the database table, along with the VERIFIED response from PayPal, but they were receiving a 302 instead of a 200 response from my website. This turned out to be a needed fix in the return from the InstantPaymentNotificationsController::process method:

public function process() {
$this->autoRender = false;
$this->log('Process accessed', 'paypal');
if ($this->request->is('post')) {
$this->log('POST ' . print_r($_POST, true), 'paypal');
}
if ($this->InstantPaymentNotification->isValid($_POST)) {
$this->log('POST Valid', 'paypal');
$notification = $this->InstantPaymentNotification->buildAssociationsFromIPN($_POST);
$existingIPNId = $this->InstantPaymentNotification->searchIPNId($notification);
if ($existingIPNId !== false) {
$notification['InstantPaymentNotification']['id'] = $existingIPNId;
}
$this->InstantPaymentNotification->saveAll($notification);
$this->__processTransaction($this->InstantPaymentNotification->id);
} else {
$this->log('POST Not Validated', 'paypal');
}
return $this->redirect('/');
}

Specifically line 50: return $this->redirect('/');. This needs to be changed to

return CURLOPT_HTTP200ALIASES;

I am opening this issue and supplying the resolution in the hopes that anyone else who is still using CakePHP 2 and this plugin will find it helpful, since it appears this plugin has not been updated for a long time.

Bug with single item

i have a small bug while while working with the paypal cart. This is several options to re-creat the bug

  • With the single item and type => 'cart', paypal said that the shopping cart is empty.

in ctp file: $paypal->button ('Pay Now', array ('type' => 'cart', 'custom' => $this->Session->read ('User.id').'-'.session_id(), 'test' => true, 'item_number' => $item['id'],'item_name' => $item['title'], 'quantity' => $item['quantity'], 'amount' => $item['price']))

  • With the array of items with only 1 single item, and type => 'cart', the $transaction['PaypalItem'] returned from paypal is empty as well

log file: http://bin.cakephp.org/view/870211664

  • With 2 items or more and the type => 'cart', plugin works perfectly:

log file: http://bin.cakephp.org/view/678523783

Updates

Nick, I was curious to know if you'd be making anymore updates to this plugin? I've used it in two projects recently and another one in the works. Would be nice to see some updates!

Thanks

Paypal form utf8 encoding issue

The Paypal form created by the helper does not include the "accept-charset" tag nor the charset input field. That causes problems with the correct encoding of special characters like accents and the like.

Line 116 of PaypalHelper should be:

$retval = "<form action='{$options['server']}/cgi-bin/webscr' method='post' accept-charset='utf-8'>

";

instead of:

$retval = "<form action='{$options['server']}/cgi-bin/webscr' method='post'>

";

Http 500 errors causes duplicate entries to database

Hello

There is a problem with the plugin that it returns Http 500 errors and causes paypal to send an ipn a couple of times which also populates in the database with the same txn_id. I have tried to debug the data and on paypal it shows "We could not send an IPN due to an HTTP error: 500: Internal Server Error"

Any help for this one?

Thnks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.