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 class Mage_Paypal_Model_Express_Review
00029 {
00030
00031
00032
00033
00034
00035 public function getCheckout()
00036 {
00037 return Mage::getSingleton('checkout/session');
00038 }
00039
00040
00041
00042
00043
00044
00045 public function getQuote()
00046 {
00047 return $this->getCheckout()->getQuote();
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 public function getAddress($addressId)
00057 {
00058 $address = Mage::getModel('customer/address')->load((int)$addressId);
00059 $address->explodeStreetAddress();
00060 if ($address->getRegionId()) {
00061 $address->setRegion($address->getRegionId());
00062 }
00063 return $address;
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 public function saveShippingMethod($shippingMethod)
00073 {
00074 if (empty($shippingMethod)) {
00075 $res = array(
00076 'error' => -1,
00077 'message' => Mage::helper('paypalUk')->__('Invalid data')
00078 );
00079 return $res;
00080 }
00081
00082 $this->getQuote()->getShippingAddress()
00083 ->setShippingMethod($shippingMethod)
00084 ->setCollectShippingRates(true);
00085 $this->getQuote()->collectTotals()->save();
00086 return array();
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096 public function saveOrder()
00097 {
00098 $res = array('error'=>true);
00099
00100 try {
00101 $billing = $this->getQuote()->getBillingAddress();
00102 $shipping = $this->getQuote()->getShippingAddress();
00103
00104
00105 $convertQuote = Mage::getModel('sales/convert_quote');
00106
00107 $order = Mage::getModel('sales/order');
00108
00109
00110 if ($this->getQuote()->isVirtual()) {
00111 $order = $convertQuote->addressToOrder($billing);
00112 } else {
00113 $order = $convertQuote->addressToOrder($shipping);
00114 }
00115
00116 $order->setBillingAddress($convertQuote->addressToOrderAddress($billing));
00117 $order->setShippingAddress($convertQuote->addressToOrderAddress($shipping));
00118 $order->setPayment($convertQuote->paymentToOrderPayment($this->getQuote()->getPayment()));
00119
00120 foreach ($this->getQuote()->getAllItems() as $item) {
00121 $order->addItem($convertQuote->itemToOrderItem($item));
00122 }
00123
00124
00125
00126
00127 Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$this->getQuote()));
00128 #$order->save();
00129 $order->place();
00130 $order->save();
00131
00132 $this->getQuote()->setIsActive(false);
00133 $this->getQuote()->save();
00134
00135 $orderId = $order->getIncrementId();
00136 $this->getCheckout()->setLastQuoteId($this->getQuote()->getId());
00137 $this->getCheckout()->setLastSuccessQuoteId($this->getQuote()->getId());
00138 $this->getCheckout()->setLastOrderId($order->getId());
00139 $this->getCheckout()->setLastRealOrderId($order->getIncrementId());
00140
00141 $order->sendNewOrderEmail();
00142
00143 $res['success'] = true;
00144 $res['error'] = false;
00145
00146 }
00147 catch (Exception $e){
00148 $res['success'] = false;
00149 $res['error'] = true;
00150 $res['error_messages'] = $order->getErrors();
00151 }
00152
00153 return $res;
00154 }
00155 }