Complex classes like EbayEnterprise_PayPal_Model_Express_Checkout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EbayEnterprise_PayPal_Model_Express_Checkout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class EbayEnterprise_PayPal_Model_Express_Checkout |
||
| 25 | { |
||
| 26 | const EBAYENTERPRISE_PAYPAL_ZERO_CHECKOUT_NOT_SUPPORTED = 'EBAYENTERPRISE_PAYPAL_ZERO_CHECKOUT_NOT_SUPPORTED'; |
||
| 27 | /** |
||
| 28 | * Cache ID prefix for "pal" lookup |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | const EBAYENTERPRISE_PAL_CACHE_ID = 'ebayenterprise_paypal_express_checkout_pal'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Keys for passthrough variables in sales/quote_payment and sales/order_payment |
||
| 36 | * Stored in additional_information |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | const PAYMENT_INFO_TOKEN = 'paypal_express_checkout_token'; |
||
| 41 | const PAYMENT_INFO_SHIPPING_OVERRIDDEN = 'paypal_express_checkout_shipping_overriden'; |
||
| 42 | const PAYMENT_INFO_SHIPPING_METHOD = 'paypal_express_checkout_shipping_method'; |
||
| 43 | const PAYMENT_INFO_PAYER_ID = 'paypal_express_checkout_payer_id'; |
||
| 44 | const PAYMENT_INFO_REDIRECT = 'paypal_express_checkout_redirect_required'; |
||
| 45 | const PAYMENT_INFO_BILLING_AGREEMENT = 'paypal_ec_create_ba'; |
||
| 46 | const PAYMENT_INFO_IS_AUTHORIZED_FLAG = 'is_authorized'; |
||
| 47 | const PAYMENT_INFO_IS_VOIDED_FLAG = 'is_voided'; |
||
| 48 | const PAYMENT_INFO_ADDRESS_STATUS = 'paypal_express_checkout_address_status'; |
||
| 49 | |||
| 50 | /** @var string Flag from the request that indicates checkout was initiated outside normal checkout flow */ |
||
| 51 | const PAYMENT_INFO_BUTTON = 'button'; |
||
| 52 | |||
| 53 | /** @var Mage_Sales_Model_Quote */ |
||
| 54 | protected $_quote; |
||
| 55 | /** @var EbayEnterprise_PayPal_Model_Config */ |
||
| 56 | protected $_config; |
||
| 57 | /** @var EbayEnterprise_PayPal_Helper_Data */ |
||
| 58 | protected $_helper; |
||
| 59 | /** @var EbayEnterprise_Paypal_Model_Express_Api */ |
||
| 60 | protected $_api; |
||
| 61 | /** @var EbayEnterprise_MageLog_Helper_Data */ |
||
| 62 | protected $_logger; |
||
| 63 | /** @var EbayEnterprise_MageLog_Helper_Context */ |
||
| 64 | protected $_context; |
||
| 65 | |||
| 66 | /** @var EbayEnterprise_PayPal_Helper_Region */ |
||
| 67 | protected $regionHelper; |
||
| 68 | /** @var Mage_Customer_Model_Session */ |
||
| 69 | protected $_customerSession; |
||
| 70 | /** @var int */ |
||
| 71 | protected $_customerId; |
||
| 72 | /** @var Mage_Sales_Model_Order */ |
||
| 73 | protected $_order; |
||
| 74 | |||
| 75 | |||
| 76 | public function __construct(array $initParams = array()) |
||
| 77 | { |
||
| 78 | list($this->_helper, $this->_logger, $this->_config, $this->_quote, $this->_context, $this->regionHelper) |
||
| 79 | = $this->_checkTypes( |
||
| 80 | $this->_nullCoalesce( |
||
| 81 | $initParams, |
||
| 82 | 'helper', |
||
| 83 | Mage::helper('ebayenterprise_paypal') |
||
| 84 | ), |
||
| 85 | $this->_nullCoalesce( |
||
| 86 | $initParams, |
||
| 87 | 'logger', |
||
| 88 | Mage::helper('ebayenterprise_magelog') |
||
| 89 | ), |
||
| 90 | $this->_nullCoalesce( |
||
| 91 | $initParams, |
||
| 92 | 'config', |
||
| 93 | Mage::helper('ebayenterprise_paypal')->getConfigModel() |
||
| 94 | ), |
||
| 95 | $this->_nullCoalesce($initParams, 'quote', null), |
||
| 96 | $this->_nullCoalesce( |
||
| 97 | $initParams, |
||
| 98 | 'context', |
||
| 99 | Mage::helper('ebayenterprise_magelog/context') |
||
| 100 | ), |
||
| 101 | $this->_nullCoalesce( |
||
| 102 | $initParams, |
||
| 103 | 'region_helper', |
||
| 104 | Mage::helper('ebayenterprise_paypal/region') |
||
| 105 | ) |
||
| 106 | ); |
||
| 107 | if (!$this->_quote) { |
||
| 108 | throw new Exception('Quote instance is required.'); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Type hinting for self::__construct $initParams |
||
| 114 | * |
||
| 115 | * @param EbayEnterprise_PayPal_Helper_Data, |
||
| 116 | * @param EbayEnterprise_MageLog_Helper_Data, |
||
| 117 | * @param EbayEnterprise_Eb2cCore_Model_Config_Registry, |
||
| 118 | * @param Mage_Sales_Model_Quote, |
||
| 119 | * @param EbayEnterprise_MageLog_Helper_Context, |
||
| 120 | * @param EbayEnterprise_PayPal_Helper_Region |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | protected function _checkTypes( |
||
| 125 | EbayEnterprise_PayPal_Helper_Data $helper, |
||
| 126 | EbayEnterprise_MageLog_Helper_Data $logger, |
||
| 127 | EbayEnterprise_Eb2cCore_Model_Config_Registry $config, |
||
| 128 | Mage_Sales_Model_Quote $quote, |
||
| 129 | EbayEnterprise_MageLog_Helper_Context $context, |
||
| 130 | EbayEnterprise_PayPal_Helper_Region $regionHelper |
||
| 131 | ) { |
||
| 132 | return func_get_args(); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return the value at field in array if it exists. Otherwise, use the |
||
| 137 | * default value. |
||
| 138 | * |
||
| 139 | * @param array $arr |
||
| 140 | * @param string|int $field Valid array key |
||
| 141 | * @param mixed $default |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | protected function _nullCoalesce(array $arr, $field, $default) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Reserve order ID for specified quote and start checkout on PayPal |
||
| 151 | * |
||
| 152 | * @param string |
||
| 153 | * @param string |
||
| 154 | * @param bool|null $button specifies if we came from Checkout Stream or from Product/Cart directly |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | public function start($returnUrl, $cancelUrl, $button = null) |
||
| 158 | { |
||
| 159 | $this->_quote->collectTotals(); |
||
| 160 | if (!$this->_quote->getGrandTotal() |
||
| 161 | && !$this->_quote->hasNominalItems() |
||
| 162 | ) { |
||
| 163 | Mage::throwException( |
||
| 164 | $this->_helper->__( |
||
| 165 | self::EBAYENTERPRISE_PAYPAL_ZERO_CHECKOUT_NOT_SUPPORTED |
||
| 166 | ) |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | $this->_quote->reserveOrderId()->save(); |
||
| 170 | $this->_getApi(); |
||
| 171 | $setExpressCheckoutReply = $this->_api->setExpressCheckout( |
||
| 172 | $returnUrl, |
||
| 173 | $cancelUrl, |
||
| 174 | $this->_quote |
||
| 175 | ); |
||
| 176 | if ($button) { |
||
| 177 | // mark the payment to indicate express checkout was initiated from |
||
| 178 | // outside the normal checkout flow |
||
| 179 | // (e.g. clicked paypal checkout button from product page) |
||
| 180 | $setExpressCheckoutReply[self::PAYMENT_INFO_BUTTON] = 1; |
||
| 181 | } |
||
| 182 | $this->_quote->getPayment()->importData($setExpressCheckoutReply); |
||
| 183 | $this->_quote->getPayment()->save(); |
||
| 184 | return $setExpressCheckoutReply; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Setter for customer |
||
| 189 | * |
||
| 190 | * @param Mage_Customer_Model_Customer $customer |
||
| 191 | * |
||
| 192 | * @return Mage_Paypal_Model_Express_Checkout |
||
| 193 | */ |
||
| 194 | public function setCustomer($customer) |
||
| 195 | { |
||
| 196 | $this->_quote->assignCustomer($customer); |
||
| 197 | $this->_customerId = $customer->getId(); |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Setter for customer with billing and shipping address changing ability |
||
| 203 | * |
||
| 204 | * @param Mage_Customer_Model_Customer $customer |
||
| 205 | * @param Mage_Sales_Model_Quote_Address $billingAddress |
||
| 206 | * @param Mage_Sales_Model_Quote_Address $shippingAddress |
||
| 207 | * |
||
| 208 | * @return Mage_Paypal_Model_Express_Checkout |
||
| 209 | */ |
||
| 210 | public function setCustomerWithAddressChange( |
||
| 211 | $customer, |
||
| 212 | $billingAddress = null, |
||
| 213 | $shippingAddress = null |
||
| 214 | ) { |
||
| 215 | $this->_quote->assignCustomerWithAddressChange( |
||
| 216 | $customer, |
||
| 217 | $billingAddress, |
||
| 218 | $shippingAddress |
||
| 219 | ); |
||
| 220 | $this->_customerId = $customer->getId(); |
||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Update quote when returned from PayPal |
||
| 226 | * rewrite billing address by paypal |
||
| 227 | * save old billing address for new customer |
||
| 228 | * export shipping address in case address absence |
||
| 229 | * |
||
| 230 | * @param string $token |
||
| 231 | */ |
||
| 232 | public function returnFromPaypal($token) |
||
| 233 | { |
||
| 234 | $this->_getApi(); |
||
| 235 | $quote = $this->_quote; |
||
| 236 | $getExpressCheckoutReply = $this->_api->getExpressCheckout( |
||
| 237 | $quote->reserveOrderId()->getReservedOrderId(), |
||
| 238 | $token, |
||
| 239 | Mage::app()->getStore()->getCurrentCurrencyCode() |
||
| 240 | ); |
||
| 241 | |||
| 242 | $this->_ignoreAddressValidation(); |
||
| 243 | /* Always import shipping address. We would have passed the shipping address in to begin with, so they |
||
| 244 | can keep it that way at PayPal if they like - or choose their own registered shipping address at PayPal. */ |
||
| 245 | $paypalShippingAddress = $getExpressCheckoutReply['shipping_address']; |
||
| 246 | if (!$quote->getIsVirtual()) { |
||
| 247 | $shippingAddress = $quote->getShippingAddress(); |
||
| 248 | $shippingAddress->setTelephone($getExpressCheckoutReply['phone']); |
||
| 249 | $shippingAddress->setStreet($paypalShippingAddress['street']); |
||
| 250 | $shippingAddress->setCity($paypalShippingAddress['city']); |
||
| 251 | $shippingAddress->setPostcode($paypalShippingAddress['postcode']); |
||
| 252 | $shippingAddress->setCountryId($paypalShippingAddress['country_id']); |
||
| 253 | // Region must be processed after country id is set for the lookup to work. |
||
| 254 | $this->regionHelper->setQuoteAddressRegion($shippingAddress, $paypalShippingAddress['region_code']); |
||
| 255 | $shippingAddress->setPrefix(null); |
||
| 256 | |||
| 257 | $shippingAddress->setMiddlename( |
||
| 258 | $getExpressCheckoutReply['middlename'] |
||
| 259 | ); |
||
| 260 | $shippingAddress->setLastname($getExpressCheckoutReply['lastname']); |
||
| 261 | $shippingAddress->setFirstname( |
||
| 262 | $getExpressCheckoutReply['firstname'] |
||
| 263 | ); |
||
| 264 | $shippingAddress->setSuffix($getExpressCheckoutReply['suffix']); |
||
| 265 | $shippingAddress->setCollectShippingRates(true); |
||
| 266 | $shippingAddress->setSameAsBilling(0); |
||
| 267 | $quote->setShippingAddress($shippingAddress); |
||
| 268 | $quote->setCustomerFirstname($getExpressCheckoutReply['firstname']); |
||
| 269 | $quote->setCustomerLastname($getExpressCheckoutReply['lastname']); |
||
| 270 | } |
||
| 271 | |||
| 272 | // Import billing address if we are here via Button - which is to say we didn't have a billing address yet: |
||
| 273 | $portBillingFromShipping |
||
| 274 | = $quote->getPayment()->getAdditionalInformation( |
||
| 275 | self::PAYMENT_INFO_BUTTON |
||
| 276 | ) == 1 |
||
| 277 | && !$quote->isVirtual(); |
||
| 278 | if ($portBillingFromShipping) { |
||
| 279 | $billingAddress = clone $shippingAddress; |
||
| 280 | $billingAddress->unsAddressId()->unsAddressType(); |
||
| 281 | $data = $billingAddress->getData(); |
||
| 282 | $data['save_in_address_book'] = 0; |
||
| 283 | $quote->getBillingAddress()->addData($data); |
||
| 284 | $quote->getShippingAddress()->setSameAsBilling(1); |
||
| 285 | } else { |
||
| 286 | $billingAddress = $quote->getBillingAddress(); |
||
| 287 | } |
||
| 288 | $paypalBillingAddress = $getExpressCheckoutReply['billing_address']; |
||
| 289 | $billingAddress->setStreet($paypalBillingAddress['street']); |
||
| 290 | $billingAddress->setCity($paypalBillingAddress['city']); |
||
| 291 | $billingAddress->setPostcode($paypalBillingAddress['postcode']); |
||
| 292 | $billingAddress->setCountryId($paypalBillingAddress['country_id']); |
||
| 293 | $billingAddress->setEmail($getExpressCheckoutReply['email']); |
||
| 294 | // Region must be processed after country id is set for the lookup to work. |
||
| 295 | $this->regionHelper->setQuoteAddressRegion($billingAddress, $paypalBillingAddress['region_code']); |
||
| 296 | $quote->setBillingAddress($billingAddress); |
||
| 297 | |||
| 298 | // import payment info |
||
| 299 | $quote->getPayment() |
||
| 300 | ->setAdditionalInformation(self::PAYMENT_INFO_PAYER_ID, $getExpressCheckoutReply['payer_id']) |
||
| 301 | ->setAdditionalInformation(self::PAYMENT_INFO_TOKEN, $token) |
||
| 302 | ->setAdditionalInformation(self::PAYMENT_INFO_ADDRESS_STATUS, $paypalShippingAddress['status']); |
||
| 303 | $quote->collectTotals()->save(); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Check whether order review has enough data to initialize |
||
| 308 | * |
||
| 309 | * @param $token |
||
| 310 | * |
||
| 311 | * @throws Mage_Core_Exception |
||
| 312 | */ |
||
| 313 | public function prepareOrderReview() |
||
| 314 | { |
||
| 315 | $payment = $this->_quote->getPayment(); |
||
| 316 | if (!$payment |
||
| 317 | || !$payment->getAdditionalInformation( |
||
| 318 | self::PAYMENT_INFO_PAYER_ID |
||
| 319 | ) |
||
| 320 | ) { |
||
| 321 | Mage::throwException( |
||
| 322 | Mage::helper('paypal')->__('Payer is not identified.') |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | $this->_quote->setMayEditShippingAddress( |
||
| 326 | 1 != $this->_quote->getPayment()->getAdditionalInformation( |
||
| 327 | self::PAYMENT_INFO_SHIPPING_OVERRIDDEN |
||
| 328 | ) |
||
| 329 | ); |
||
| 330 | $this->_ignoreAddressValidation(); |
||
| 331 | $this->_quote->collectTotals()->save(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set shipping method to quote, if needed |
||
| 336 | * |
||
| 337 | * @param string $methodCode |
||
| 338 | */ |
||
| 339 | public function updateShippingMethod($methodCode) |
||
| 340 | { |
||
| 341 | $shippingAddress = $this->_quote->getShippingAddress(); |
||
| 342 | if ($methodCode && !$this->_quote->getIsVirtual() && $shippingAddress) { |
||
| 343 | if ($methodCode != $shippingAddress->getShippingMethod()) { |
||
| 344 | $this->_ignoreAddressValidation(); |
||
| 345 | $shippingAddress->setShippingMethod($methodCode) |
||
| 346 | ->setCollectShippingRates(true); |
||
| 347 | // Know shipping method has changed which will likely trigger |
||
| 348 | // total changes. Ensure that totals are collected again by |
||
| 349 | // setting the flag to false before triggering the collect. |
||
| 350 | $this->_quote->setTotalsCollectedFlag(false)->collectTotals()->save(); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Prepare the quote according to the method (guest, registering new customer, or existing customer) |
||
| 357 | * |
||
| 358 | * @return boolean Whether we are a new customer |
||
| 359 | */ |
||
| 360 | protected function _prepareQuote() |
||
| 361 | { |
||
| 362 | $isNewCustomer = false; |
||
| 363 | switch ($this->getCheckoutMethod()) { |
||
| 364 | case Mage_Checkout_Model_Type_Onepage::METHOD_GUEST: |
||
| 365 | $this->_prepareGuestQuote(); |
||
| 366 | break; |
||
| 367 | case Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER: |
||
| 368 | $customerId = $this->_lookupCustomerId(); |
||
| 369 | if ($customerId) { |
||
| 370 | $this->_getCustomerSession()->loginById($customerId); |
||
| 371 | $this->_prepareCustomerQuote(); |
||
| 372 | } else { |
||
| 373 | $this->_prepareNewCustomerQuote(); |
||
| 374 | } |
||
| 375 | $isNewCustomer = true; |
||
| 376 | break; |
||
| 377 | default: |
||
| 378 | $this->_prepareCustomerQuote(); |
||
| 379 | break; |
||
| 380 | } |
||
| 381 | return $isNewCustomer; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Place the order and recurring payment profiles when customer returned from paypal |
||
| 386 | * Until this moment all quote data must be valid |
||
| 387 | * |
||
| 388 | * @param string $token |
||
| 389 | * @param string $shippingMethodCode |
||
| 390 | */ |
||
| 391 | public function place($token, $shippingMethodCode = null) |
||
| 392 | { |
||
| 393 | $this->updateShippingMethod($shippingMethodCode); |
||
| 394 | $isNewCustomer = $this->_prepareQuote(); |
||
| 395 | $this->_ignoreAddressValidation(); |
||
| 396 | $this->_quote->collectTotals(); |
||
| 397 | $this->_getApi(); |
||
| 398 | $payerId = $this->_quote->getPayment()->getAdditionalInformation( |
||
| 399 | self::PAYMENT_INFO_PAYER_ID |
||
| 400 | ); |
||
| 401 | $doExpressReply = $this->_api->doExpressCheckout( |
||
| 402 | $this->_quote, |
||
| 403 | $token, |
||
| 404 | $payerId |
||
| 405 | ); |
||
| 406 | $doAuthorizationReply = $this->_api->doAuthorization($this->_quote); |
||
| 407 | $this->_quote->getPayment() |
||
| 408 | ->importData(array_merge($doExpressReply, $doAuthorizationReply)); |
||
| 409 | $service = Mage::getModel('sales/service_quote', $this->_quote); |
||
| 410 | try { |
||
| 411 | $service->submitAll(); |
||
| 412 | // Any exceptions thrown from submitAll indicate an order that failed |
||
| 413 | // to be created. In any such cases, the payment auth needs to be voided. |
||
| 414 | } catch (Exception $e) { |
||
| 415 | $this->_api->doVoidQuote($this->_quote); |
||
| 416 | // Throw an exception for the controller to handle. Needs to indicate |
||
| 417 | // the failure to complete the PayPal payment as the PayPal process |
||
| 418 | // needs to be restarted once the auth was performed. |
||
| 419 | throw Mage::exception('EbayEnterprise_PayPal', $this->_helper->__(EbayEnterprise_Paypal_Model_Express_Api::EBAYENTERPRISE_PAYPAL_API_FAILED)); |
||
| 420 | } |
||
| 421 | $this->_quote->save(); |
||
| 422 | |||
| 423 | if ($isNewCustomer) { |
||
| 424 | try { |
||
| 425 | $this->_involveNewCustomer(); |
||
| 426 | } catch (Exception $e) { |
||
| 427 | $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | $order = $service->getOrder(); |
||
| 432 | if (!$order) { |
||
| 433 | return; |
||
| 434 | } |
||
| 435 | |||
| 436 | switch ($order->getState()) { |
||
| 437 | // Even after placement, paypal can disallow authorize/capture |
||
| 438 | case Mage_Sales_Model_Order::STATE_PENDING_PAYMENT: |
||
| 439 | break; |
||
| 440 | // regular placement, when everything is ok |
||
| 441 | case Mage_Sales_Model_Order::STATE_PROCESSING: |
||
| 442 | case Mage_Sales_Model_Order::STATE_COMPLETE: |
||
| 443 | case Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW: |
||
| 444 | $order->sendNewOrderEmail(); |
||
| 445 | break; |
||
| 446 | } |
||
| 447 | $this->_order = $order; |
||
| 448 | |||
| 449 | Mage::dispatchEvent( |
||
| 450 | 'checkout_submit_all_after', |
||
| 451 | array('order' => $order, 'quote' => $this->_quote) |
||
| 452 | ); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Make sure addresses will be saved without validation errors |
||
| 457 | */ |
||
| 458 | private function _ignoreAddressValidation() |
||
| 459 | { |
||
| 460 | $this->_quote->getBillingAddress()->setShouldIgnoreValidation(true); |
||
| 461 | if (!$this->_quote->getIsVirtual()) { |
||
| 462 | $this->_quote->getShippingAddress()->setShouldIgnoreValidation( |
||
| 463 | true |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Return order |
||
| 470 | * |
||
| 471 | * @return Mage_Sales_Model_Order |
||
| 472 | */ |
||
| 473 | public function getOrder() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get checkout method |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getCheckoutMethod() |
||
| 484 | { |
||
| 485 | if ($this->_getCustomerSession()->isLoggedIn()) { |
||
| 486 | return Mage_Checkout_Model_Type_Onepage::METHOD_CUSTOMER; |
||
| 487 | } |
||
| 488 | if (!$this->_quote->getCheckoutMethod()) { |
||
| 489 | if (Mage::helper('checkout')->isAllowedGuestCheckout( |
||
| 490 | $this->_quote |
||
| 491 | ) |
||
| 492 | ) { |
||
| 493 | $this->_quote->setCheckoutMethod( |
||
| 494 | Mage_Checkout_Model_Type_Onepage::METHOD_GUEST |
||
| 495 | ); |
||
| 496 | } else { |
||
| 497 | $this->_quote->setCheckoutMethod( |
||
| 498 | Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER |
||
| 499 | ); |
||
| 500 | } |
||
| 501 | } |
||
| 502 | return $this->_quote->getCheckoutMethod(); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return EbayEnterprise_Paypal_Model_Express_Api |
||
| 507 | */ |
||
| 508 | protected function _getApi() |
||
| 509 | { |
||
| 510 | if (!$this->_api) { |
||
| 511 | $this->_api = Mage::getModel('ebayenterprise_paypal/express_api'); |
||
| 512 | } |
||
| 513 | return $this->_api; |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Prepare quote for guest checkout order submit |
||
| 518 | * |
||
| 519 | * @return Mage_Paypal_Model_Express_Checkout |
||
| 520 | */ |
||
| 521 | protected function _prepareGuestQuote() |
||
| 522 | { |
||
| 523 | $quote = $this->_quote; |
||
| 524 | $quote->setCustomerId(null) |
||
| 525 | ->setCustomerEmail($quote->getBillingAddress()->getEmail()) |
||
| 526 | ->setCustomerIsGuest(true) |
||
| 527 | ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID); |
||
| 528 | return $this; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Checks if customer with email coming from Express checkout exists |
||
| 533 | * |
||
| 534 | * @return int |
||
| 535 | */ |
||
| 536 | protected function _lookupCustomerId() |
||
| 537 | { |
||
| 538 | return Mage::getModel('customer/customer') |
||
| 539 | ->setWebsiteId(Mage::app()->getWebsite()->getId()) |
||
| 540 | ->loadByEmail($this->_quote->getCustomerEmail()) |
||
| 541 | ->getId(); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Prepare quote for customer registration and customer order submit |
||
| 546 | * and restore magento customer data from quote |
||
| 547 | * |
||
| 548 | * @return Mage_Paypal_Model_Express_Checkout |
||
| 549 | */ |
||
| 550 | protected function _prepareNewCustomerQuote() |
||
| 551 | { |
||
| 552 | $quote = $this->_quote; |
||
| 553 | $billing = $quote->getBillingAddress(); |
||
| 554 | $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); |
||
| 555 | |||
| 556 | $customer = $quote->getCustomer(); |
||
| 557 | /** @var $customer Mage_Customer_Model_Customer */ |
||
| 558 | $customerBilling = $billing->exportCustomerAddress(); |
||
| 559 | $customer->addAddress($customerBilling); |
||
| 560 | $billing->setCustomerAddress($customerBilling); |
||
| 561 | $customerBilling->setIsDefaultBilling(true); |
||
| 562 | if ($shipping && !$shipping->getSameAsBilling()) { |
||
| 563 | $customerShipping = $shipping->exportCustomerAddress(); |
||
| 564 | $customer->addAddress($customerShipping); |
||
| 565 | $shipping->setCustomerAddress($customerShipping); |
||
| 566 | $customerShipping->setIsDefaultShipping(true); |
||
| 567 | } elseif ($shipping) { |
||
| 568 | $customerBilling->setIsDefaultShipping(true); |
||
| 569 | } |
||
| 570 | $this->_setAdditionalCustomerBillingData(); |
||
| 571 | Mage::helper('core')->copyFieldset( |
||
| 572 | 'checkout_onepage_billing', |
||
| 573 | 'to_customer', |
||
| 574 | $billing, |
||
| 575 | $customer |
||
| 576 | ); |
||
| 577 | $customer->setEmail($quote->getCustomerEmail()); |
||
| 578 | $customer->setPrefix($quote->getCustomerPrefix()); |
||
| 579 | $customer->setFirstname($quote->getCustomerFirstname()); |
||
| 580 | $customer->setMiddlename($quote->getCustomerMiddlename()); |
||
| 581 | $customer->setLastname($quote->getCustomerLastname()); |
||
| 582 | $customer->setSuffix($quote->getCustomerSuffix()); |
||
| 583 | $customer->setPassword( |
||
| 584 | $customer->decryptPassword($quote->getPasswordHash()) |
||
| 585 | ); |
||
| 586 | $customer->setPasswordHash( |
||
| 587 | $customer->hashPassword($customer->getPassword()) |
||
| 588 | ); |
||
| 589 | $customer->save(); |
||
| 590 | $quote->setCustomer($customer); |
||
| 591 | return $this; |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Set additional customer billing data |
||
| 596 | * |
||
| 597 | * @return self |
||
| 598 | */ |
||
| 599 | protected function _setAdditionalCustomerBillingData() |
||
| 600 | { |
||
| 601 | $quote = $this->_quote; |
||
| 602 | $billing = $quote->getBillingAddress(); |
||
| 603 | if ($quote->getCustomerDob() && !$billing->getCustomerDob()) { |
||
| 604 | $billing->setCustomerDob($quote->getCustomerDob()); |
||
| 605 | } |
||
| 606 | if ($quote->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) { |
||
| 607 | $billing->setCustomerTaxvat($quote->getCustomerTaxvat()); |
||
| 608 | } |
||
| 609 | if ($quote->getCustomerGender() && !$billing->getCustomerGender()) { |
||
| 610 | $billing->setCustomerGender($quote->getCustomerGender()); |
||
| 611 | } |
||
| 612 | return $this; |
||
| 613 | } |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Prepare quote customer address information and set the customer on the quote |
||
| 617 | * |
||
| 618 | * @return self |
||
| 619 | */ |
||
| 620 | protected function _prepareCustomerQuote() |
||
| 621 | { |
||
| 622 | $shipping = $this->_quote->isVirtual() ? null |
||
| 623 | : $this->_quote->getShippingAddress(); |
||
| 624 | $customer = $this->_getCustomerSession()->getCustomer(); |
||
| 625 | $customerBilling = $this->_prepareCustomerBilling($customer); |
||
| 626 | if ($shipping) { |
||
| 627 | $customerShipping = $this->_prepareCustomerShipping( |
||
| 628 | $customer, |
||
| 629 | $shipping |
||
| 630 | ); |
||
| 631 | if ($customerBilling && !$customer->getDefaultShipping() |
||
| 632 | && $shipping->getSameAsBilling() |
||
| 633 | ) { |
||
| 634 | $customerBilling->setIsDefaultShipping(true); |
||
| 635 | } elseif (isset($customerShipping) |
||
| 636 | && !$customer->getDefaultShipping() |
||
| 637 | ) { |
||
| 638 | $customerShipping->setIsDefaultShipping(true); |
||
| 639 | } |
||
| 640 | } |
||
| 641 | $this->_quote->setCustomer($customer); |
||
| 642 | return $this; |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Set up the billing address for the quote and on the customer, and set the customer's |
||
| 647 | * default billing address. |
||
| 648 | * |
||
| 649 | * @param $customer Mage_Customer_Model_Customer |
||
| 650 | * |
||
| 651 | * @return Mage_Sales_Model_Quote_Address $billingAddress | null |
||
| 652 | */ |
||
| 653 | protected function _prepareCustomerBilling( |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Setup shipping address and set as customer default if indicated. |
||
| 671 | * |
||
| 672 | * @param $customer Mage_Customer_Model_Customer |
||
| 673 | * @param Mage_Sales_Model_Quote_Address $shipping |
||
| 674 | * |
||
| 675 | * @return Mage_Sales_Model_Quote_Address $shipping | null |
||
| 676 | */ |
||
| 677 | protected function _prepareCustomerShipping( |
||
| 678 | Mage_Customer_Model_Customer $customer, |
||
| 679 | Mage_Sales_Model_Quote_Address $shipping |
||
| 680 | ) { |
||
| 681 | if ($shipping |
||
| 682 | && ((!$shipping->getCustomerId() && !$shipping->getSameAsBilling()) |
||
| 683 | || (!$shipping->getSameAsBilling() |
||
| 684 | && $shipping->getSaveInAddressBook())) |
||
| 685 | ) { |
||
| 686 | $customerShipping = $shipping->exportCustomerAddress(); |
||
| 687 | $customer->addAddress($customerShipping); |
||
| 688 | $shipping->setCustomerAddress($customerShipping); |
||
| 689 | return $customerShipping; |
||
| 690 | } |
||
| 691 | return null; |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Involve new customer to system |
||
| 696 | * |
||
| 697 | * @return self |
||
| 698 | */ |
||
| 699 | protected function _involveNewCustomer() |
||
| 700 | { |
||
| 701 | $customer = $this->_quote->getCustomer(); |
||
| 702 | if ($customer->isConfirmationRequired()) { |
||
| 703 | $customer->sendNewAccountEmail('confirmation'); |
||
| 704 | $url = Mage::helper('customer')->getEmailConfirmationUrl( |
||
| 705 | $customer->getEmail() |
||
| 706 | ); |
||
| 707 | $this->_getCustomerSession()->addSuccess( |
||
| 708 | Mage::helper('customer')->__( |
||
| 709 | 'Account confirmation is required. Please, check your e-mail for confirmation link. To resend confirmation email please <a href="%s">click here</a>.', |
||
| 710 | $url |
||
| 711 | ) |
||
| 712 | ); |
||
| 713 | } else { |
||
| 714 | $customer->sendNewAccountEmail(); |
||
| 715 | $this->_getCustomerSession()->loginById($customer->getId()); |
||
| 716 | } |
||
| 717 | return $this; |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Set customer session object |
||
| 722 | * |
||
| 723 | * @return self |
||
| 724 | */ |
||
| 725 | public function setCustomerSession( |
||
| 726 | Mage_Customer_Model_Session $customerSession |
||
| 727 | ) { |
||
| 728 | $this->_customerSession = $customerSession; |
||
| 729 | return $this; |
||
| 730 | } |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Get customer session object |
||
| 734 | * |
||
| 735 | * @return Mage_Customer_Model_Session |
||
| 736 | */ |
||
| 737 | protected function _getCustomerSession() |
||
| 741 | } |
||
| 742 |