1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Model_Paypal_Mep_Checkout
35: {
36: 37: 38: 39:
40: const PAYMENT_INFO_PAYER_EMAIL = 'paypal_payer_email';
41: const PAYMENT_INFO_TRANSACTION_ID = 'paypal_mep_checkout_transaction_id';
42:
43:
44: 45: 46: 47: 48:
49: protected $_methodType = Mage_XmlConnect_Model_Payment_Method_Paypal_Mep::MEP_METHOD_CODE;
50:
51: 52: 53: 54: 55:
56: protected $_quote = null;
57:
58: 59: 60: 61: 62:
63: protected $_checkoutSession;
64:
65: 66: 67: 68: 69:
70: protected $_helper;
71:
72: 73: 74: 75: 76: 77:
78: public function __construct($params = array())
79: {
80: $this->_checkoutSession = Mage::getSingleton('checkout/session');
81: if (isset($params['quote']) && $params['quote'] instanceof Mage_Sales_Model_Quote) {
82: $this->_quote = $params['quote'];
83: } else {
84: Mage::throwException(
85: Mage::helper('xmlconnect')->__('Quote instance is required.')
86: );
87: }
88: }
89:
90: 91: 92: 93: 94:
95: public function initCheckout()
96: {
97: $this->_quote->reserveOrderId()->save();
98:
99: 100: 101: 102:
103: if ($this->_quote->getIsMultiShipping()) {
104: $this->_quote->setIsMultiShipping(false);
105: $this->_quote->save();
106: }
107:
108: 109: 110: 111:
112: $customer = Mage::getSingleton('customer/session')->getCustomer();
113: if ($customer) {
114: $this->_quote->assignCustomer($customer);
115: }
116: if (!Mage::getSingleton('customer/session')->isLoggedIn()
117: && Mage::getSingleton('checkout/session')->getQuote()->isAllowedGuestCheckout()
118: ) {
119: $this->_prepareGuestQuote();
120: }
121: return $this->_quote->getReservedOrderId();
122: }
123:
124: 125: 126: 127: 128: 129:
130: public function saveShipping($data)
131: {
132: if (empty($data)) {
133: return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid data.'));
134: }
135:
136: $address = $this->_quote->getBillingAddress();
137:
138: $this->_applyCountryWorkarounds($data);
139:
140: $model = Mage::helper('xmlconnect')->getApplication();
141:
142: $paypalMepAllowSpecific = $model->getData('config_data[payment:paypalmep/allowspecific]');
143: if ($paypalMepAllowSpecific !== null) {
144: if ((int)$paypalMepAllowSpecific > 0) {
145: $allowedCountries = explode(',', $model->getData('config_data[payment][paypalmep/applicable]'));
146: $allowedCountries = array_map('trim', $allowedCountries);
147: if (!in_array(trim($data['country_id']), $allowedCountries)) {
148: return array(
149: 'error' => 1,
150: 'message' => Mage::helper('xmlconnect')->__('Buyer country is not allowed by store.')
151: );
152: }
153: }
154: }
155:
156: if (empty($data['firstname']) && empty($data['lastname'])) {
157: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
158: $customer = Mage::getSingleton('customer/session')->getCustomer();
159: $data['firstname'] = $customer->getFirstname();
160: $data['lastname'] = $customer->getLastname();
161: } else {
162: $data['firstname'] = Mage::helper('xmlconnect')->__('Guest');
163: $data['lastname'] = Mage::helper('xmlconnect')->__('Guest');
164: }
165: }
166:
167: $address->addData($data);
168:
169: $this->_ignoreAddressValidation();
170:
171: $address->implodeStreetAddress();
172:
173: if (!$this->_quote->isVirtual()) {
174: $billing = clone $address;
175: $billing->unsAddressId()->unsAddressType();
176: $shipping = $this->_quote->getShippingAddress();
177: $shippingMethod = $shipping->getShippingMethod();
178: $shipping->addData($billing->getData())->setSameAsBilling(1)->setShippingMethod($shippingMethod)
179: ->setCollectShippingRates(true);
180: }
181:
182: $this->_quote->collectTotals()->save();
183: return array();
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function saveShippingMethod($shippingMethod)
193: {
194: if (empty($shippingMethod)) {
195: return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid shipping method.'));
196: }
197:
198: $rate = $this->_quote->getShippingAddress()->getShippingRateByCode($shippingMethod);
199: if (!$rate) {
200: return array('error' => 1, 'message' => Mage::helper('xmlconnect')->__('Invalid shipping method.'));
201: }
202:
203: $shippingAddress = $this->_quote->getShippingAddress();
204: if (!$this->_quote->getIsVirtual() && $shippingAddress) {
205: if ($shippingMethod != $shippingAddress->getShippingMethod()) {
206: $this->_ignoreAddressValidation();
207: $this->_quote->getShippingAddress()->setShippingMethod($shippingMethod);
208: $this->_quote->collectTotals()->save();
209: }
210: }
211:
212: return array();
213: }
214:
215: 216: 217: 218: 219: 220:
221: public function savePayment($data)
222: {
223: if ($this->_quote->isVirtual()) {
224: $this->_quote->getBillingAddress()->setPaymentMethod($this->_methodType);
225: } else {
226: $this->_quote->getShippingAddress()->setPaymentMethod($this->_methodType);
227: }
228:
229: $payment = $this->_quote->getPayment();
230: $data['method'] = $this->_methodType;
231: $payment->importData($data);
232:
233: $email = isset($data['payer']) ? $data['payer'] : null;
234: $payment->setAdditionalInformation(self::PAYMENT_INFO_PAYER_EMAIL, $email);
235: $payment->setAdditionalInformation(
236: self::PAYMENT_INFO_TRANSACTION_ID, isset($data['transaction_id']) ? $data['transaction_id'] : null
237: );
238: $this->_quote->setCustomerEmail($email);
239:
240: $this->_quote->collectTotals()->save();
241:
242: return array();
243: }
244:
245: 246: 247: 248: 249: 250:
251: public function saveOrder()
252: {
253: $this->_ignoreAddressValidation();
254:
255: $order = Mage::getModel('sales/service_quote', $this->_quote)->submit();
256: $this->_quote->save();
257:
258: $this->_getCheckoutSession()->clear();
259:
260: 261: 262:
263: $quoteId = $this->_quote->getId();
264: $this->_getCheckoutSession()->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId)
265: ->setLastOrderId($order->getId())->setLastRealOrderId($order->getIncrementId());
266:
267: if ($order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING
268: && Mage::getSingleton('customer/session')->isLoggedIn()
269: ) {
270: try {
271: $order->sendNewOrderEmail();
272: } catch (Exception $e) {
273: Mage::logException($e);
274: }
275: }
276:
277: return array();
278: }
279:
280: 281: 282: 283: 284:
285: public function getLastOrderId()
286: {
287: $lastId = $this->_getCheckoutSession()->getLastOrderId();
288: $orderId = false;
289: if ($lastId) {
290: $order = Mage::getModel('sales/order');
291: $order->load($lastId);
292: $orderId = $order->getIncrementId();
293: }
294: return $orderId;
295: }
296:
297: 298: 299: 300: 301:
302: protected function _ignoreAddressValidation()
303: {
304: $this->_quote->getBillingAddress()->setShouldIgnoreValidation(true);
305: if (!$this->_quote->getIsVirtual()) {
306: $this->_quote->getShippingAddress()->setShouldIgnoreValidation(true);
307: }
308: }
309:
310: 311: 312: 313: 314:
315: protected function _getCheckoutSession()
316: {
317: return $this->_checkoutSession;
318: }
319:
320: 321: 322: 323: 324:
325: protected function _prepareGuestQuote()
326: {
327: $quote = $this->_quote;
328: $quote->setCustomerId(null)->setCustomerIsGuest(true)
329: ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
330: return $this;
331: }
332:
333: 334: 335: 336: 337: 338:
339: protected function _applyCountryWorkarounds(&$request)
340: {
341: $request['country_id'] = isset($request['country_id']) ? trim($request['country_id']) : null;
342: if (empty($request['country_id'])) {
343: $request['country_id'] = strtoupper(Mage::getStoreConfig('general/country/default'));
344: } else {
345: $request['country_id'] = strtoupper($request['country_id']);
346: }
347: }
348: }
349: