* * * $Id: PayPalExpressReturnModule.class.php 420 2008-12-10 03:36:18Z erikwinn $ *@version 0.1 * *@copyright (C) 2008 by Erik Winn *@license GPL v.2 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA * *@package Quasi * @subpackage Modules */ class PayPalExpressReturnModule extends CheckOutConfirmationModule { /** * This instance of the order is recreated from the database when the user is returned to our site. * Although this may change, currently the order id is passed as a GET value from paypal. this is * not entirely secure, we may need to tighten this up, not clear how yet ... *@todo * - load order safely, see comment above .. * - handle cancelations .. * *@var Order objOrder - recreated instance of the order to be completed (or removed) */ protected $objOrder; /** * This is a PayPalExpressCheckoutAction - we use the final calls only. *@var PaymentAction objPaymentAction - the payment action employed */ protected $objPaymentAction; /** * This label is used in the template to show whatever message is appropriate for the status * of the payment transaction. *@var QLabel lblMessages - displays messages regarding order payment status */ // public $lblMessages; /** * Module constructor * NOTE: This module ignores the required extra parameters .. *@param QPanel - objContentBlock parent controller object. *@param mixed - extra parameters, ignored */ public function __construct( QPanel $objContentBlock, $mixParameters=null) { $objOrder = Order::LoadById($_REQUEST['orderid']); if(!$objOrder) throw new QCallerException('PayPal Return error for order ' . $_REQUEST['orderid']); try { parent::__construct($objContentBlock, $objContentBlock, $objOrder); } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; } //reset the template // $this->strTemplate = __QUASI_CORE_TEMPLATES__ . '/PayPalExpressReturnModule.tpl.php'; if( IndexPage::$strPageRequest == 'PayPalExpressReturn') $this->handleCompletion(); elseif( IndexPage::$strPageRequest == 'PayPalExpressCancel') $this->handleCancellation(); } /** * This overrides the display initialization in CheckOutConfirmationModule */ protected function init() { $this->aryCheckOutItemViews = array(); //construct the list of items $aryOrderItems = $this->objOrder->GetOrderItemArray(); foreach( $aryOrderItems as $objOrderItem) { $objItemView = new CheckOutItemView( $this, $objOrderItem, false ); $this->aryCheckOutItemViews[] = $objItemView; } $this->objOrderTotalsView = new OrderTotalsView($this, $this->objOrder, false); $intShippingAddressId = $this->objOrder->GetShippingAddress()->Id; $intBillingAddressId = $this->objOrder->GetBillingAddress()->Id; $this->objShippingAddressView = new AddressView($this, $intShippingAddressId, 'ShippingAddress: '); $this->objShippingAddressView->CssClass = 'ShippingAddressReview'; $this->objShippingAddressView->AutoRenderChildren = true; $this->objBillingAddressView = new AddressView($this, $intBillingAddressId, 'BillingAddress: '); $this->objBillingAddressView->CssClass = 'BillingAddressReview'; $this->objBillingAddressView->AutoRenderChildren = true; if($this->objOrder->PaymentMethodId) { $objPaymentMethod = PaymentMethod::Load($this->objOrder->PaymentMethodId); $this->pnlPaymentMethod = new QPanel($this); $this->pnlPaymentMethod->HtmlEntities = false; $this->pnlPaymentMethod->CssClass = 'PaymentMethodReview'; $this->pnlPaymentMethod->AutoRenderChildren = true; $strText = '
' . Quasi::Translate('Payment Method') . ':
' . sprintf( '
%s

%s ', $objPaymentMethod->Title, $objPaymentMethod->Description ); $this->pnlPaymentMethod->Text = $strText; } if($this->objOrder->ShippingMethodId) { $objShippingMethod = ShippingMethod::Load($this->objOrder->ShippingMethodId); $this->pnlShippingMethod = new QPanel($this); $this->pnlShippingMethod->HtmlEntities = false; $this->pnlShippingMethod->CssClass = 'ShippingMethodReview'; $this->pnlShippingMethod->AutoRenderChildren = true; $strText = '
' . Quasi::Translate('Shipping Method') . ':
' . sprintf( '
%s

%s ', $objShippingMethod->Title, $objShippingMethod->Description ); $this->pnlShippingMethod->Text = $strText; } } /** * This function handles a successful return from PayPal. *@todo * - load order safely, this is a quick cludge that needs work .. * - make order completion messages configurable, error, approved and pending .. */ public function handleCompletion() { $this->objPaymentAction = new PayPalExpressCheckoutAction($this->objOrder); $this->objPaymentAction->getExpressCheckoutDetails(); $this->objPaymentAction->doExpressCheckoutPayment(); $blnIsPending = ( 'PENDING' == strtoupper($this->objPaymentAction->PaypalTransaction->PaymentStatus)); $strApprovedText = '
' . Quasi::Translate("Thank You for your purchase") . '!
'; $strApprovedText .= '

' . Quasi::Translate('Your Order Number is') . ': ' . $this->objOrder->Id . '

'; if( $blnIsPending) $strApprovedText .= '

' . Quasi::Translate('Your PayPal payment is being approved - it will be processed as soon as it has been approved.') . '

'; $strApprovedText .= '

' . Quasi::Translate('We will email you shortly with a confirmation of your order') . '.

' . Quasi::Translate('Please make sure that you have given a correct email address with which to contact you') . '.

'; $strDeclinedText = '

' . Quasi::Translate("We're Sorry - Your payment has been declined") . '.

'; $strErrorMessage = ''; if($this->objPaymentAction->HasErrors) $strErrorMessage = '

' . Quasi::Translate('Oops, there was an error during the transaction!') . '

' . $this->objPaymentAction->Errors . '

' . $this->objPaymentAction->StatusText . '

'; if($this->objPaymentAction->Approved || $blnIsPending) $this->lblMessage->Text = $strApprovedText; else $this->lblMessage->Text = $strDeclinedText; if($this->objPaymentAction->HasErrors) $this->lblMessage->Text .= $strErrorMessage; //clear the shopping cart here for pending payments .. completeOrder() only does this if approved. if( !$this->objPaymentAction->HasErrors && ( $this->objPaymentAction->Approved || $blnIsPending) ) IndexPage::$objShoppingCart->DeleteAllShoppingCartItems(); //all ok? then finish up .. otherwise we'll wait for IPN to complete the order. if($this->objPaymentAction->Approved) $this->objPaymentAction->completeOrder(); } /** * This fuction handles cancelation returns from PayPal, ie, if the user does not complete payment * process but hits "Cancel" at paypal .. */ public function handleCancellation() { /* $objOrderStatusHistory = new OrderStatusHistory(); $objOrderStatusHistory->OrderId = $this->objOrder->Id; $objOrderStatusHistory->StatusId = OrderStatusType::Cancelled; $objOrderStatusHistory->Save(); */ $this->objOrder->Delete(); Quasi::Redirect(__QUASI_SUBDIRECTORY__ . '/index.php/CheckOut'); } public function Validate(){return true;} public function __get($strName) { switch ($strName) { case 'Order': return $this->objOrder ; default: try { return parent::__get($strName); } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; } } } public function __set($strName, $mixValue) { switch ($strName) { case 'Order': try { return ($this->objOrder = QType::Cast($mixValue, 'Order' )); } catch (QInvalidCastException $objExc) { $objExc->IncrementOffset(); throw $objExc; } default: try { return (parent::__set($strName, $mixValue)); } catch (QCallerException $objExc) { $objExc->IncrementOffset(); throw $objExc; } } } }//end class }//end define ?>