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