/** * Run the widget. Renders the shipping estimator lines on the page. */ public function run() { // Required assets. $assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets', false, -1, true); Yii::app()->clientScript->registerScriptFile($assets . '/js/WsShippingEstimator.js'); $pathToZippo = Yii::getPathOfAlias('ext') . DIRECTORY_SEPARATOR . 'wsadvcheckout' . DIRECTORY_SEPARATOR . 'assets'; $assets = Yii::app()->getAssetManager()->publish($pathToZippo, false, -1, true); Yii::app()->clientScript->registerScriptFile($assets . '/zippo.js'); $checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); // We may wish to update the shipping options right away if we know the // cart has changed. $updateOnLoad = false; if ($this->updateShippingOptions) { // This check for shippingCountry being null is a workaround to fix // WS-3180. When shippingCountry is null, we need to update the // shipping estimates *now* because they will not be updated by the // JavaScript (since the JavaScript in WsShippingEstimator requires // country to be set). The reason we need to do this is because // shippingCountry may be null when in-store pickup has been // chosen. // TODO: Fix this in WsShippingEstimator and remove this workaround. if (empty($checkoutForm->shippingCountry) || empty($checkoutForm->shippingPostal)) { Shipping::updateCartScenariosInSession(); } else { $updateOnLoad = true; } } // Use the shipping scenarios and shipping address in the session. $arrCartScenario = Shipping::loadCartScenariosFromSession(); $wsShippingEstimatorOptions = self::getShippingEstimatorOptions($arrCartScenario, $checkoutForm->shippingProvider, $checkoutForm->shippingPriority, $checkoutForm->shippingCity, $checkoutForm->shippingStateCode, $checkoutForm->shippingCountryCode, $updateOnLoad); $selectedCartScenario = Shipping::getSelectedCartScenarioFromSession(); if ($selectedCartScenario !== null) { $formattedShippingPrice = $selectedCartScenario['formattedShippingPrice']; $formattedCartTax = $selectedCartScenario['formattedCartTax']; } else { $formattedShippingPrice = null; $formattedCartTax = null; } $this->render('_shippingestimator', array('countries' => CHtml::listData(Country::getShippingCountries(), 'code', 'country'), 'formattedShippingPrice' => $formattedShippingPrice, 'formattedCartTax' => $formattedCartTax, 'shippingCountryCode' => $wsShippingEstimatorOptions['shippingCountryCode'], 'shippingCountryName' => $wsShippingEstimatorOptions['shippingCountryName'], 'shippingPostal' => $checkoutForm->shippingPostal, 'wsShippingEstimatorOptions' => CJSON::encode($wsShippingEstimatorOptions), 'cssClass' => self::CSS_CLASS)); }
/** * Update cart payment with returned results and complete sale or return an error * * @param $arrPaymentResult * @return bool */ protected function finalizeOrder($arrPaymentResult) { $objCart = Yii::app()->shoppingcart; $objPayment = $objCart->payment; $objPayment->payment_data = $arrPaymentResult['result']; $objPayment->payment_amount = $arrPaymentResult['amount_paid']; $objPayment->datetime_posted = isset($retVal['payment_date']) ? date("Y-m-d H:i:s", strtotime($retVal['payment_date'])) : new CDbExpression('NOW()'); $objPayment->save(); if (isset($arrPaymentResult['success']) && $arrPaymentResult['success']) { Yii::log("Payment Success! Wrapping up processing", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); // We have successful payment, so close out the order and show the receipt. $objCart->printed_notes .= $this->checkoutForm->orderNotes; $objCart->completeUpdatePromoCode(); Checkout::emailReceipts($objCart); Checkout::finalizeCheckout($objCart, true, true); return true; } else { $error = isset($arrPaymentResult['result']) ? $arrPaymentResult['result'] : "UNKNOWN ERROR"; $this->checkoutForm->addErrors(array('payment' => $error)); Yii::log("Error executing payment:\n" . $error, 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__); return false; } }
/** * Cache needed information: checkoutform, shipping rates * * @return void */ public function saveFormToSession() { MultiCheckoutForm::saveToSession($this); }
/** * Updates the cart scenarios stored in the session. * * @return void * @see Shipping::getCartScenarios. */ public static function updateCartScenariosInSession() { // If we already have shipping details in the session we can try to // update the cart scenarios. $checkoutForm = MultiCheckoutForm::loadFromSession(); if ($checkoutForm === null) { $arrCartScenario = null; } else { // Save shipping options and rates to session. try { $arrCartScenario = Shipping::getCartScenarios($checkoutForm); } catch (Exception $e) { // TODO: We should probably execute this block if $arrCartScenario is an empty array as well. Yii::log('Unable to get cart scenarios: ' . $e->getMessage(), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); // If there are no valid cart scenarios we can deselect whatever // was previously selected. // TODO: We should possibly do this if the newly update cart // scenarios don't include the previously selected one. $checkoutForm->shippingProvider = null; $checkoutForm->shippingPriority = null; MultiCheckoutForm::saveToSession($checkoutForm); // Remove any previously stored cart scenarios. $arrCartScenario = null; } } // Save the updated rates back to the session. Shipping::saveCartScenariosToSession($arrCartScenario); }
/** * Did the payment have a subform? * * @return bool */ public function hasSubForm() { $form = MultiCheckoutForm::loadFromSessionOrNew(); $objModule = Modules::LoadByName($this->payment_module); return array_key_exists($objModule->id, $form->getAlternativePaymentMethodsThatUseSubForms()); }
/** * Apply a promo code to the cart and return an array indicating what happened. * @param string $strPromoCode The promocode. * @return array An array indicating what happened. * ['success'] boolean Whether the promo code was applied. * ['action'] string Recommended action: alert|error|triggerCalc|success. * ['message'] string A message to display. */ protected function applyPromoCodeModal($strPromoCode) { if (Yii::app()->shoppingcart->PromoCode !== null) { return array('success' => false, 'action' => 'alert', 'message' => Yii::t('global', 'Only one promo code can be applied')); } $objPromoCode = new PromoCode(); $objPromoCode->code = $strPromoCode; $objPromoCode->setScenario('checkout'); if ($objPromoCode->validate() === false) { $arrErrors = $objPromoCode->getErrors(); return array('success' => false, 'action' => 'error', 'message' => $arrErrors['code'][0]); } $objPromoCode = PromoCode::LoadByCode($strPromoCode); Yii::app()->shoppingcart->applyPromoCode($objPromoCode); // See if this promo code is supposed to turn on free shipping. // This runs AFTER validate() so if we get here, it means that any // criteria have passed. So just apply and refresh the shipping list. if ($objPromoCode->Shipping) { // Update the shipping selection to use the free shipping module. $objFreeShipping = Modules::model()->freeshipping()->find(); if ($objFreeShipping !== null) { $checkoutForm = MultiCheckoutForm::loadFromSession(); if ($checkoutForm !== null) { try { $arrCartScenario = Shipping::getCartScenarios($checkoutForm); } catch (Exception $e) { $arrCartScenario = null; } if ($arrCartScenario !== null) { $freeShippingScenario = findWhere($arrCartScenario, array('providerId' => $objFreeShipping->id)); $checkoutForm = MultiCheckoutForm::loadFromSessionOrNew(); $checkoutForm->shippingProvider = $freeShippingScenario['providerId']; $checkoutForm->shippingPriority = $freeShippingScenario['priorityLabel']; MultiCheckoutForm::saveToSession($checkoutForm); } } } return array('success' => true, 'action' => 'triggerCalc', 'message' => Yii::t('global', 'Congratulations! This order qualifies for Free Shipping!')); } return array('success' => true, 'action' => 'success'); }