Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/res/features/paypal/PayPalController.php
<?php
namespace __APP_NAME__\controller;

use \gnomephp\paypal\IpnListener;
use __APP_NAME__\model\PayPalOrder;
use \gnomephp\doctrine\Doctrine;

/**
 * IPN path script.
 * Note default router rule when include core/paypal in routes is POST /index.php/paypal/ipn
 * 
 * @todo Implement me in your own way!
 * @author peec
 *
 */
class PayPalController extends \gnomephp\mvc\Controller{



	/**
	 * Process IPN script.
	 *
	 * @todo: replace me with how you want to confirm your order.
	 *
	 */
	public function index(){

		$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();
				
			if ($verified){

				if (!$this->input->post){
					die("Nothing to process.");
				}


				// Your paypal email ( The email that recieves the cash )
				$MY_PAYPAL_EMAIL = 'MY_PAYPAL_EMAIL';



				// These should come from database of items you can order.
				$ITEM_COST = '9.99';
				$ITEM_COST_CURRENCY = 'USD';




				// Checks if order is already processed.
				$txnId = $this->input->post->get('txn_id');
				if (PayPalOrder::orderProcessed($txnId)){
					die("Order is already processed.");
				}



				$payment_status = $this->input->post->get('payment_status');
				$receiver_email = $this->input->post->get('receiver_email');
				$mc_gross = $this->input->post->get('mc_gross');
				$mc_currency = $this->input->post->get('mc_currency');


				// Important check.
				if (
				$payment_status != 'Completed' ||
				$receiver_email != $MY_PAYPAL_EMAIL ||
				$mc_gross != $ITEM_COST ||
				$mc_currency != $ITEM_COST_CURRENCY
				){
					die("We could not accept this order at this time.");
				}else{
						
					// Order successful and validated !
						
					// Do things here.
						
					// Now add order to db.
					Doctrine::load();	
					$order = new PayPalOrder($txnId, $payer_email, $mc_gross, $mc_currency);
					Doctrine::getEM()->persist($order);
					Doctrine::getEM()->flush();
				}
			}

		} catch (\Exception $e) {
			echo $e->getMessage();
			exit(0);
		}

	}



}
Return current item: GnomePHP