protected function preSite() { if (class_exists('\\Search\\Factory')) { \Search\Factory::registerSource(new \Search\Models\Source(array('id' => 'shop.products', 'title' => 'Products', 'class' => '\\Shop\\Models\\Products'))); } if (class_exists('\\Minify\\Factory')) { \Minify\Factory::registerPath($this->dir . "/src/"); $files = array('Shop/Assets/js/class.js', 'Shop/Assets/js/validation.js', 'Shop/Assets/js/site.js', 'Shop/Assets/js/jquery.popupoverlay.js', 'Shop/Assets/js/jquery.scrollTo.js', 'Shop/Assets/js/jquery.payment.js', 'Shop/Assets/js/jquery.star-rating.js'); if ($check_campaigns = \Dsc\System::instance()->get('session')->get('shop.check_campaigns')) { $files[] = 'Shop/Assets/js/check_campaigns.js'; } foreach ($files as $file) { \Minify\Factory::js($file); } $files = array('Shop/Assets/css/jquery.star-rating.css'); foreach ($files as $file) { \Minify\Factory::css($file); } } $app = \Base::instance(); $request_kmi = \Dsc\System::instance()->get('input')->get('kmi', null, 'string'); $cookie_kmi = $app->get('COOKIE.kmi'); if (!empty($request_kmi)) { if ($cookie_kmi != $request_kmi) { $app->set('COOKIE.kmi', $request_kmi); } $cart = \Shop\Models\Carts::fetch(); if (empty($cart->user_email)) { $cart->user_email = $request_kmi; $cart->store(); } } // symlink to the public folder if necessary if (!is_dir($this->app->get('PATH_ROOT') . 'public/ShopAssets')) { $public_assets = $this->app->get('PATH_ROOT') . 'public/ShopAssets'; $app_assets = realpath(__DIR__ . '/src/Shop/Assets'); $res = symlink($app_assets, $public_assets); } static::diagnostics(); }
/** * */ public function moveToCart() { $f3 = \Base::instance(); $wishlist_id = $this->inputfilter->clean($f3->get('PARAMS.id'), 'alnum'); $wishlistitem_hash = $this->inputfilter->clean($f3->get('PARAMS.hash'), 'cmd'); $identity = \Dsc\System::instance()->get('auth')->getIdentity(); $session_id = \Dsc\System::instance()->get('session')->id(); $wishlist = (new \Shop\Models\Wishlists())->load(array('_id' => new \MongoId((string) $wishlist_id))); if (empty($wishlist->id)) { if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false, 'message' => 'Invalid wishlist'))); } else { \Dsc\System::addMessage('Invalid Wishlist', 'error'); $f3->reroute('/shop/wishlist'); return; } } // Validate that this wishlist belongs to the current user if ($identity->id != $wishlist->user_id && $session_id != $wishlist->session_id) { if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false, 'message' => 'Not your wishlist'))); } else { \Dsc\System::addMessage('Not your wishlist', 'error'); $f3->reroute('/shop/wishlist'); return; } } $cart = \Shop\Models\Carts::fetch(); try { $wishlist->moveToCart($wishlistitem_hash, $cart); } catch (\Exception $e) { if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false, 'message' => 'Item could not be moved to cart'))); } else { \Dsc\System::addMessage('Item could not be moved to cart', 'error'); \Dsc\System::addMessage($e->getMessage(), 'error'); $f3->reroute('/shop/wishlist/' . $wishlist->id); return; } } if ($f3->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => true, 'message' => 'Item moved to cart'))); } else { \Dsc\System::addMessage('Item moved to cart'); $f3->reroute('/shop/wishlist/' . $wishlist->id); } }
public function completePurchase() { $gateway_id = $this->inputfilter->clean($this->app->get('PARAMS.gateway_id'), 'cmd'); // 1. Get \Shop\Models\Checkout // and the payment data to the checkout model $payment_data = (array) $this->app->get('REQUEST'); $payment_data['payment_method'] = $gateway_id; $checkout = \Shop\Models\Checkout::instance(); // the cart->id is in the $payment_data, so let the PaymentMethod load the cart $cart = \Shop\Models\Carts::fetch(); $checkout->addCart($cart)->addPaymentData($payment_data); // 3. validate the payment data against the cart try { $checkout->validatePayment(); } catch (\Exception $e) { // Log this error message $order = $checkout->order(); $order->setError($e->getMessage())->set('errors', $order->getErrors())->fail(); \Dsc\System::addMessage('Checkout could not complete for the following reason:', 'error'); \Dsc\System::addMessage($e->getMessage(), 'error'); // redirect to the ./shop/checkout/payment page unless a failure redirect has been set in the session (site.shop.checkout.redirect.fail) $redirect = '/shop/checkout/payment'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect.fail')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect.fail', null); $this->app->reroute($redirect); return; } // 4. since payment was validated, accept the order try { $checkout->acceptOrder(); } catch (\Exception $e) { $checkout->setError($e->getMessage()); } // if the order acceptance fails, let the user know if (!$checkout->orderAccepted() || !empty($checkout->getErrors())) { \Dsc\System::addMessage('Checkout could not be completed. Please try again or contact us if you have further difficulty.', 'error'); // Add the errors to the stack and redirect foreach ($checkout->getErrors() as $exception) { \Dsc\System::addMessage($exception->getMessage(), 'error'); } // redirect to the ./shop/checkout/payment page unless a failure redirect has been set in the session (site.shop.checkout.redirect.fail) $redirect = '/shop/checkout/payment'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect.fail')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect.fail', null); $this->app->reroute($redirect); return; } // if the order acceptance succeeds, trigger completion event try { // Fire an afterShopCheckout event $event_after = \Dsc\System::instance()->trigger('afterShopCheckout', array('checkout' => $checkout)); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'warning'); } // Redirect to ./shop/checkout/confirmation unless a site.shop.checkout.redirect has been set $redirect = '/shop/checkout/confirmation'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect', null); $this->app->reroute($redirect); return; }
/** * Submits a completed cart checkout processing * */ public function submit() { $cart = \Shop\Models\Carts::fetch(); if ($cart->quantity() <= 0) { $this->app->reroute('/shop/cart'); } $identity = $this->getIdentity(); if (empty($identity->id)) { $flash = \Dsc\Flash::instance(); \Base::instance()->set('flash', $flash); $this->app->set('meta.title', 'Login or Register | Checkout'); $view = \Dsc\System::instance()->get('theme'); echo $view->render('Shop/Site/Views::checkout/identity.php'); return; } $f3 = \Base::instance(); // Update the cart with checkout data from the form $checkout_inputs = $this->input->get('checkout', array(), 'array'); if (!empty($checkout_inputs['billing_address']['same_as_shipping'])) { $checkout_inputs['billing_address']['same_as_shipping'] = true; } else { $checkout_inputs['billing_address']['same_as_shipping'] = false; } $cart_checkout = array_merge((array) $cart->{'checkout'}, $checkout_inputs); $cart->checkout = $cart_checkout; $cart->save(); // Get \Shop\Models\Checkout // Bind the cart and payment data to the checkout model $checkout = \Shop\Models\Checkout::instance(); $checkout->addCart($cart)->addPaymentData($f3->get('POST')); // Fire a beforeShopCheckout event that allows Listeners to hijack the checkout process // Payment processing & authorization could occur at this event, and the Listener would update the checkout object // Add the checkout model to the event $event = new \Dsc\Event\Event('beforeShopCheckout'); $event->addArgument('checkout', $checkout); try { $event = \Dsc\System::instance()->getDispatcher()->triggerEvent($event); } catch (\Exception $e) { $checkout->setError($e->getMessage()); $event->setArgument('checkout', $checkout); } $checkout = $event->getArgument('checkout'); // option 1: ERRORS in checkout from beforeShopCheckout if (!empty($checkout->getErrors())) { // Add the errors to the stack and redirect foreach ($checkout->getErrors() as $exception) { \Dsc\System::addMessage($exception->getMessage(), 'error'); } // redirect to the ./shop/checkout/payment page unless a failure redirect has been set in the session (site.shop.checkout.redirect.fail) $redirect = '/shop/checkout/payment'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect.fail')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect.fail', null); $f3->reroute($redirect); return; } // option 2: NO ERROR in checkout from beforeShopCheckout // If checkout is not completed, do the standard checkout process // If checkout was completed by a Listener during the beforeShopCheckout process, skip the standard checkout process and go to the afterShopCheckout event if (!$checkout->orderAccepted()) { // the standard checkout process try { // failed payment processing should throw an exception $checkout->processPayment(); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'error'); // redirect to the ./shop/checkout/payment page unless a failure redirect has been set in the session (site.shop.checkout.redirect.fail) $redirect = '/shop/checkout/payment'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect.fail')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect.fail', null); $this->app->reroute($redirect); return; } try { $checkout->acceptOrder(); } catch (\Exception $e) { $checkout->setError($e->getMessage()); } if (!$checkout->orderAccepted() || !empty($checkout->getErrors())) { \Dsc\System::addMessage('Checkout could not be completed. Please try again or contact us if you have further difficulty.', 'error'); // Add the errors to the stack and redirect foreach ($checkout->getErrors() as $exception) { \Dsc\System::addMessage($exception->getMessage(), 'error'); } // redirect to the ./shop/checkout/payment page unless a failure redirect has been set in the session (site.shop.checkout.redirect.fail) $redirect = '/shop/checkout/payment'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect.fail')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect.fail', null); $f3->reroute($redirect); return; } } // the order WAS accepted // Fire an afterShopCheckout event $event_after = new \Dsc\Event\Event('afterShopCheckout'); $event_after->addArgument('checkout', $checkout); try { $event_after = \Dsc\System::instance()->getDispatcher()->triggerEvent($event_after); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'warning'); } // Redirect to ./shop/checkout/confirmation unless a site.shop.checkout.redirect has been set $redirect = '/shop/checkout/confirmation'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.shop.checkout.redirect')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.shop.checkout.redirect', null); $f3->reroute($redirect); return; }
/** * Remove an item from the cart */ public function removeGiftCard() { $redirect = '/shop/cart'; if ($custom_redirect = \Dsc\System::instance()->get('session')->get('site.removegiftcard.redirect')) { $redirect = $custom_redirect; } \Dsc\System::instance()->get('session')->set('site.removegiftcard.redirect', null); // ----------------------------------------------------- // Start: validation // ----------------------------------------------------- // validate the POST values if (!($code = $this->inputfilter->clean($this->app->get('PARAMS.code'), 'alnum'))) { // if validation fails, respond appropriately if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage('Invalid Gift Card', 'error'); $this->app->reroute('/shop/cart'); } } // ----------------------------------------------------- // End: validation // ----------------------------------------------------- // get the current user's cart, either based on session_id (visitor) or user_id (logged-in) $cart = \Shop\Models\Carts::fetch(); // remove the item try { $cart->removeGiftCard($code); if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => true))); } else { \Dsc\System::addMessage('Gift card removed from cart'); $this->app->reroute($redirect); } } catch (\Exception $e) { if ($this->app->get('AJAX')) { return $this->outputJson($this->getJsonResponse(array('result' => false))); } else { \Dsc\System::addMessage($e->getMessage()); $this->app->reroute($redirect); } } }