getConfig('type') == self::TYPE_BILLING_SHIPPING_INFO) { if (!Qs_Request::getRequestValue('action')) { $this->_action = 'orderInfo'; } else { $this->_action = null;// якщо ми в режимі виводу білінг/шіпінг інфи і в реквесті є action, то ніяких дій виконувати не треба } } else { if ($this->getConfig('type') == self::TYPE_CONFIRMATION && !Qs_Request::getRequestValue('action')) { $this->_action = 'previewOrder'; } else { parent::_initAction(); } } return $this; } protected function _callAction() { if (!Qs_Request::isXmlHttpRequest()) { if ($this->_action !== 'verifyOrder' && $this->_action !== 'cancelOrder' && $this->_action !== 'completeOrder') { if (!(bool)$this->_getCart()->getItemsCount()) { Qs_Http::redirect($this->_getCartUrl()); } try { $cartValid = $this->_getCart()->validateCartItems(); } catch (ReloadCartException $e) { if (($msg = $e->getMessage())) { Zend_Registry::get('doc')->setBackMessage($msg, Qs_Doc::MESSAGE_BOX_ATTENTION); } Qs_Http::redirect(Qs_Request::getUrl()); exit; } if (!$cartValid) { $this->_setBackUrl($this->_getCartUrl()); $this->_setBackError($this->_errorMap[self::ERROR_VALIDATE_ITEMS]); $this->_doBack(); } } } return parent::_callAction(); } protected function _getFormData4Validation(Qs_Form $form) { $formData = ('GET' == strtoupper($form->getAttrib('method'))) ? $_GET : $_POST; switch (Qs_Array::get($formData['servicePayment'], 'type')) { case 'authorizeNet': break; default: $formData['servicePayment']['authorizeNetCardNumber'] = ''; $formData['servicePayment']['authorizeNetCardCode'] = ''; $formData['servicePayment']['authorizeNetExpirationDate'] = array(); break; } $discountElements = array('usePromoCode', 'usePromoCodeValue', 'useGiftCard', 'useGiftCardValue'); foreach ($discountElements as $name) { if (array_key_exists($name, $formData['servicePayment'])) { unset($formData['servicePayment'][$name]); } } return $formData; } protected function _doInitOrder() { $form = $this->_getNewForm(); $form->setDefaults(); $this->_log(); $formData = $this->_getFormData4Validation($form); if ($form->validate($formData)) { $data = $form->getValues(); $this->_prepareFormData($data); $this->_saveUserDataToSession($data); Qs_Http::redirect($this->_getCartUrl('preview')); } else { $this->_addFormItem($form); } return $this; } protected function _doInitOrderAjax() { $form = $this->_getNewForm(); $this->_displayJson($form->validateAjax()); } protected function _doOrderInfo() { $this->_setBackUrl($this->_getCartUrl()); $userData = $this->_getUserDataFromSession(); if (empty($userData)) { $this->_doBack(); } $paymentMethods = Model::getPaymentTypes(); $userData['servicePayment']['typeTitle'] = $paymentMethods[$userData['servicePayment']['type']]; $item['userData'] = $userData; $item['tpl'] = $this->getTemplate('info.tpl'); $this->_addItem($item); return $this; } protected function _doPreviewOrder() { $userData = $this->_getUserDataFromSession(); $isServiceProductPayment = in_array( $userData['servicePayment']['type'], $this->_getDataObj()->getConfig('serviceProductPayments')->toArray() ); if ($isServiceProductPayment && 0 == $this->_getCart()->getTotal()) { $this->_getCart()->addServiceProduct(); Qs_Http::redirect(BASE_URL_LANGUAGE . '/'. CURRENT_PAGE); } if (empty($userData)) { $this->_setBackUrl($this->_getCartUrl()); $this->_doBack(); } if ($this->_getDataObj()->getConfig('shipping')) { $shippingRate = $this->_calculateShippingRate($userData); if ($shippingRate && !$shippingRate->isSuccess()) { $url = Qs_SiteMap::findFirst( null, array('type' => 'ECommerce_Checkout_'), array('type' => 'default'), 'url' ); $this->_setBackUrl($url); $this->_setBackError($this->_errorMap[self::ERROR_RATE_CALCULATION]); $this->_setBackError($shippingRate->getDataField('errorText')); $this->_doBack(); } } $cartData = $this->_getCart()->getData(); $cartData = array_merge($cartData, $this->_prepareOrderData($userData)); $submitForm = $this->_getFormInstance('process'); $submitForm->setAttrib('id', $submitForm->getAttrib('id') . '-proccess'); $cartData['submitOrderForm'] = $submitForm->setDefaults()->render(); $cartData['tpl'] = $this->getTemplate('confirmation.tpl'); $this->_addItem($cartData); $this->_log(); return $this; } protected function _doNew() { $options = array(); $formDefaults = array(); if (!$this->_doc->getAuth()->getIdentity()) { App_ECommerce_Checkout_LoginBlock_View::setSessionValue('skipLogin', true); } if (null !== ($sessionUserData = $this->_getUserDataFromSession())) { $formDefaults = $sessionUserData; } else if ($this->_doc->getAuthIdentity()) { $userObj = new UserObj(); $formDefaults = $userObj->setPrimaryKey($this->_doc->getAuth()->getData('id'))->getData(); } // TaxCloud: calculate taxes if address is filled $address = Model::getDestinationAddress($formDefaults); $taxCloudErrors = []; if (Model::isAddressFilled($address)) { $taxCloudErrors = $this->updateCartTaxes($address); } if (!empty($formDefaults)) { if (!$this->_getCart()->hasShippableItems() && isset($formDefaults['serviceShipping'])) { unset($formDefaults['serviceShipping']); } $options['defaults'] = $formDefaults; } $form = $this->_getNewForm($options); $form->setDefaults(); if ($taxCloudErrors) { $form->getDestinationAddressForm()->setErrors($taxCloudErrors); } $this->_addFormItem($form); $this->_postNew(); return $this; } protected function _getNewForm(array $options = array()) { $options['orderSummary'] = $this->_getSummaryItem(Qs_Array::get($options, 'defaults', array())); /** @var App_ECommerce_Checkout_Form_New $form */ $form = parent::_getNewForm($options); $form->getDestinationAddressForm()->setTaxCloudCallback([$this, 'updateCartTaxes']); return $form; } /** * Cancel started payment */ protected function _doCancelOrder() { $id = Qs_Request::getRequestValue('id'); $cart = new App_ECommerce_Cart_Obj(); $cart->setPrimaryKey($id); $cartTransactionId = $cart->getData('transactionId'); $transactionId = $this->_getDataObj()->getTransactionId(); $this->_getDataObj()->saveTransactionId($transactionId); if ((!empty($cartTransactionId) && $cartTransactionId == $transactionId) || (!empty($id) && $this->_getCart()->getData('id') == $id) ) { $cart->update(array('transactionId' => null)); $cart->getSessionCartObject()->id = $id; $this->_getDataObj()->cancelTransaction(); } $this->_setBackUrl($this->_getCartUrl()); $this->_doBack(); } protected function _doCompleteOrder() { $type = Qs_Request::getRequestValue('type'); $paymentAdapter = $this->_getPaymentAdapter($type); if (!$paymentAdapter->verifyOrder()) { $this->_setBackUrl($this->_getCartUrl()); $this->_setBackError($paymentAdapter->getErrors()); $this->_getDataObj()->cancelTransaction(); $this->_clearSessionPaymentData(); } else { $this->completeOrder(); } $this->_doBack(); } protected function _doCreateOrder() { $orderData = $this->_getOrderData(); if (0 == $orderData['total']) { $orderData['paid'] = 'y'; } if (false === $this->_getDataObj()->insert($orderData)) { $this->_setBackErrors($this->_getDataObj()->getErrors()); } else { /** @var Qs_Form $form */ $form = $this->_getFormInstance('process')->setDefaults(); if ($form->validate()) { $this->_initFromForm($form); $this->_getDataObj()->update(); } else { $this->_setBackUrl($this->_getCartUrl()); $this->_setBackError($this->_errorMap[self::ERROR_OTHER]); $this->_doBack(); } $this->_getDataObj()->startTransaction(); if (Model::getPaymentTypes() && 0 != $orderData['total']) { $this->_processPayment($orderData); } $this->completeOrder(); $this->_log(); } $this->_doBack(); } protected function _getLog() { if (null === $this->_log) { parent::_getLog(); $this->_log->setAction('new', 'Headed to %itemName% Checkout'); $this->_log->setAction('previewOrder', 'Headed to %itemName% Confirmation'); $this->_log->setAction('createOrder', 'Placed new %itemName% #%id%'); $this->_log->setAction('initOrder', 'Edited Payment & Shipping Information'); } return $this->_log; } protected function _doValidateTaxCloudDestinationAddress() { exit; } protected function _doValidateTaxCloudDestinationAddressAjax() { /** @var App_ECommerce_Checkout_Form_New $form */ $form = $this->_getNewForm(['defaults' => Qs_Request::getPost()]); $addressForm = $form->getDestinationAddressForm(); /** @var Zend_Form_Element $element */ foreach ($addressForm->getElements() as $element) { $element->setRequired(in_array($element->getName(), ['address', 'city', 'state', 'zip'])); } $addressForm->getElement('email')->setRequired(false); $response = $addressForm->validateAjax(); $response['cartSummaryHtml'] = $this->renderCartSummary(Qs_Request::getPost()); $this->_displayJson($response); } /** * @return TaxCloud */ public function getTaxCloudService() { return $this->getService('TaxCloud'); } public function updateCartTaxes(array $destinationAddress) { $service = $this->getTaxCloudService(); $service->updateCartTaxes($this->_getCart()->getPrimaryKey(), $destinationAddress); $this->_getCart()->clearListCache(); if (($errors = $service->getErrors())) { return $errors; } return true; } protected function renderCartSummary(array $data) { $item = $this->_getSummaryItem($data); $this->_doc->assign('item', new Qs_Doc_Item($item)); return $this->_doc->fetch($item['tpl']); } }