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_PaypalUk_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 $order->setBillingAddress($convertQuote->addressToOrderAddress($billing));
00116 $order->setShippingAddress($convertQuote->addressToOrderAddress($shipping));
00117 $order->setPayment($convertQuote->paymentToOrderPayment($this->getQuote()->getPayment()));
00118
00119 foreach ($this->getQuote()->getAllItems() as $item) {
00120 $order->addItem($convertQuote->itemToOrderItem($item));
00121 }
00122
00123
00124
00125
00126 Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$this->getQuote()));
00127 #$order->save();
00128 $order->place();
00129 $order->save();
00130
00131 $this->getQuote()->setIsActive(false);
00132 $this->getQuote()->save();
00133
00134 $orderId = $order->getIncrementId();
00135 $this->getCheckout()->setLastQuoteId($this->getQuote()->getId());
00136 $this->getCheckout()->setLastSuccessQuoteId($this->getQuote()->getId());
00137 $this->getCheckout()->setLastOrderId($order->getId());
00138 $this->getCheckout()->setLastRealOrderId($order->getIncrementId());
00139
00140 $order->sendNewOrderEmail();
00141
00142 $res['success'] = true;
00143 $res['error'] = false;
00144
00145 }
00146 catch (Exception $e){
00147 $res['success'] = false;
00148 $res['error'] = true;
00149 $res['error_messages'] = $order->getErrors();
00150 }
00151
00152 return $res;
00153 }
00154 }