00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_PaypalUk_ExpressController extends Mage_Core_Controller_Front_Action
00034 {
00035
00036
00037
00038
00039 protected function _expireAjax()
00040 {
00041 if (!Mage::getSingleton('checkout/session')->getQuote()->hasItems()) {
00042 $this->getResponse()->setHeader('HTTP/1.1','403 Session Expired');
00043 exit;
00044 }
00045 }
00046
00047
00048
00049
00050
00051
00052 public function getExpress()
00053 {
00054 return Mage::getSingleton('paypaluk/express');
00055 }
00056
00057
00058
00059
00060
00061 public function shortcutAction()
00062 {
00063
00064
00065
00066
00067 $this->getExpress()->shortcutSetExpressCheckout();
00068
00069
00070
00071 $this->getResponse()->setRedirect($this->getExpress()->getRedirectUrl());
00072 }
00073
00074
00075
00076
00077 public function cancelAction()
00078 {
00079 $this->_redirect('checkout/cart');
00080 }
00081
00082
00083
00084
00085
00086 public function returnAction()
00087 {
00088 $this->getExpress()->returnFromPaypal();
00089
00090 $this->getResponse()->setRedirect($this->getExpress()->getRedirectUrl());
00091 }
00092
00093
00094
00095
00096
00097 public function reviewAction()
00098 {
00099 $payment = Mage::getSingleton('checkout/session')->getQuote()->getPayment();
00100 if ($payment && $payment->getPaypalPayerId()) {
00101 $this->loadLayout();
00102 $this->_initLayoutMessages('paypaluk/session');
00103 $this->renderLayout();
00104 } else {
00105 $this->_redirect('checkout/cart');
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114 public function getReview()
00115 {
00116 return Mage::getSingleton('paypaluk/express_review');
00117 }
00118
00119
00120
00121
00122 public function saveShippingMethodAction()
00123 {
00124 if ($this->getRequest()->getParam('ajax')) {
00125 $this->_expireAjax();
00126 }
00127
00128 if (!$this->getRequest()->isPost()) {
00129 return;
00130 }
00131
00132 $data = $this->getRequest()->getParam('shipping_method', '');
00133
00134 $result = $this->getReview()->saveShippingMethod($data);
00135
00136 if ($this->getRequest()->getParam('ajax')) {
00137 $this->loadLayout('paypaluk_express_review_details');
00138 $this->getResponse()->setBody($this->getLayout()->getBlock('root')->toHtml());
00139 } else {
00140 $this->_redirect('paypaluk/express/review');
00141 }
00142 }
00143
00144
00145
00146
00147
00148 public function saveOrderAction()
00149 {
00150
00151
00152
00153
00154
00155 $error_message = '';
00156 $payPalSession = Mage::getSingleton('paypaluk/session');
00157
00158 try {
00159 $address = $this->getReview()->getQuote()->getShippingAddress();
00160 if (!$address->getShippingMethod()) {
00161 if ($shippingMethod = $this->getRequest()->getParam('shipping_method')) {
00162 $this->getReview()->saveShippingMethod($shippingMethod);
00163 } else if (!$this->getReview()->getQuote()->getIsVirtual()) {
00164 $payPalSession->addError(Mage::helper('paypalUk')->__('Please select a valid shipping method'));
00165 $this->_redirect('paypaluk/express/review');
00166 return;
00167 }
00168 }
00169 $billing = $this->getReview()->getQuote()->getBillingAddress();
00170 $shipping = $this->getReview()->getQuote()->getShippingAddress();
00171
00172 $convertQuote = Mage::getModel('sales/convert_quote');
00173
00174 $order = Mage::getModel('sales/order');
00175
00176
00177 if ($this->getReview()->getQuote()->isVirtual()) {
00178 $order = $convertQuote->addressToOrder($billing);
00179 } else {
00180 $order = $convertQuote->addressToOrder($shipping);
00181 }
00182 $order->setBillingAddress($convertQuote->addressToOrderAddress($billing));
00183 $order->setShippingAddress($convertQuote->addressToOrderAddress($shipping));
00184 $order->setPayment($convertQuote->paymentToOrderPayment($this->getReview()->getQuote()->getPayment()));
00185
00186 foreach ($this->getReview()->getQuote()->getAllItems() as $item) {
00187 $order->addItem($convertQuote->itemToOrderItem($item));
00188 }
00189
00190
00191
00192 Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$this->getReview()->getQuote()));
00193
00194
00195 if (!$order->getCustomerEmail()) {
00196 $order->setCustomerEmail($shipping->getEmail());
00197 }
00198
00199 $order->place();
00200
00201 if (isset($customer) && $customer && $this->getReview()->getQuote()->getCheckoutMethod()==Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER) {
00202 $customer->save();
00203 $customer->setDefaultBilling($customerBilling->getId());
00204 $customerShippingId = isset($customerShipping) ? $customerShipping->getId() : $customerBilling->getId();
00205 $customer->setDefaultShipping($customerShippingId);
00206 $customer->save();
00207
00208 $order->setCustomerId($customer->getId())
00209 ->setCustomerEmail($customer->getEmail())
00210 ->setCustomerPrefix($customer->getPrefix())
00211 ->setCustomerFirstname($customer->getFirstname())
00212 ->setCustomerMiddlename($customer->getMiddlename())
00213 ->setCustomerLastname($customer->getLastname())
00214 ->setCustomerSuffix($customer->getSuffix())
00215 ->setCustomerGroupId($customer->getGroupId())
00216 ->setCustomerTaxClassId($customer->getTaxClassId());
00217
00218 $billing->setCustomerId($customer->getId())->setCustomerAddressId($customerBilling->getId());
00219 $shipping->setCustomerId($customer->getId())->setCustomerAddressId($customerShippingId);
00220 }
00221
00222 } catch (Mage_Core_Exception $e){
00223 $error_message = $e->getMessage();
00224 } catch (Exception $e){
00225 if (isset($order)) {
00226 $error_message = $order->getErrors();
00227 } else {
00228 $error_message = $e->getMessage();
00229 }
00230 }
00231
00232 if ($error_message) {
00233 $payPalSession->addError($e->getMessage());
00234 $this->_redirect('paypaluk/express/review');
00235 return;
00236 }
00237
00238 $order->save();
00239
00240 $this->getReview()->getQuote()->setIsActive(false);
00241 $this->getReview()->getQuote()->save();
00242
00243 $orderId = $order->getIncrementId();
00244 $this->getReview()->getCheckout()->setLastQuoteId($this->getReview()->getQuote()->getId());
00245 $this->getReview()->getCheckout()->setLastSuccessQuoteId($this->getReview()->getQuote()->getId());
00246 $this->getReview()->getCheckout()->setLastOrderId($order->getId());
00247 $this->getReview()->getCheckout()->setLastRealOrderId($order->getIncrementId());
00248
00249 $order->sendNewOrderEmail();
00250
00251 $payPalSession->unsExpressCheckoutMethod();
00252
00253 $this->_redirect('checkout/onepage/success');
00254 }
00255
00256
00257
00258
00259
00260 public function errorAction()
00261 {
00262 $this->_redirect('checkout/cart');
00263 }
00264
00265
00266
00267
00268
00269
00270 public function updateOrderAction() {
00271 $error_message = '';
00272 $payPalSession = Mage::getSingleton('paypal/session');
00273
00274 $order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
00275
00276 if ($order->getId()) {
00277 $comment = null;
00278 if ($order->canInvoice() && $this->getExpress()->getPaymentAction() == Mage_Paypal_Model_Api_Abstract::PAYMENT_TYPE_SALE) {
00279 $invoice = $order->prepareInvoice();
00280 $invoice->register()->capture();
00281 Mage::getModel('core/resource_transaction')
00282 ->addObject($invoice)
00283 ->addObject($invoice->getOrder())
00284 ->save();
00285
00286 $orderState = Mage_Sales_Model_Order::STATE_PROCESSING;
00287 $orderStatus = $this->getExpress()->getConfigData('order_status');
00288 $comment = Mage::helper('paypal')->__('Invoice #%s created', $invoice->getIncrementId());
00289 } else {
00290 $this->getExpress()->placeOrder($order->getPayment());
00291
00292 $orderState = Mage_Sales_Model_Order::STATE_PROCESSING;
00293 $orderStatus = $this->getExpress()->getConfigData('order_status');
00294 }
00295
00296 if (!$orderStatus) {
00297 $orderStatus = $order->getConfig()->getStateDefaultStatus($orderState);
00298 }
00299 if (!$comment) {
00300 $comment = Mage::helper('paypal')->__('Customer returned from PayPal site.');
00301 }
00302
00303 $order->setState($orderState, $orderStatus, $comment, $notified = true);
00304 $order->save();
00305
00306 Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false);
00307 Mage::getSingleton('checkout/session')->getQuote()->save();
00308
00309 $order->sendNewOrderEmail();
00310 }
00311
00312 $payPalSession->unsExpressCheckoutMethod();
00313
00314 $this->_redirect('checkout/onepage/success');
00315 }
00316
00317 }