Пример #1
0
 public function completeCartPurchase()
 {
     $gateway_id = $this->inputfilter->clean($this->app->get('PARAMS.gateway_id'), 'cmd');
     $cart_id = $this->inputfilter->clean($this->app->get('PARAMS.cart_id'), 'alnum');
     // 1. Verify the cart ID (and update the cart with checkout data from the form?)
     $cart = (new \Shop\Models\Carts())->setState('filter.id', $cart_id)->getItem();
     if (empty($cart->id) || (string) $cart->id != (string) $cart_id) {
         \Dsc\System::addMessage('Checkout unable to complete due to an invalid Cart ID.', '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;
     }
     // 2. Get \Shop\Models\Checkout
     // and Bind the cart and payment data to the checkout model
     $payment_data = (array) $this->app->get('REQUEST');
     $payment_data['payment_method'] = $gateway_id;
     $checkout = \Shop\Models\Checkout::instance();
     $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;
 }
Пример #2
0
 /**
  * POST target for creating an order from an existing wishlist_id
  */
 public function createOrder()
 {
     $wishlist_id = $this->inputfilter->clean($this->app->get('PARAMS.id'), 'alnum');
     $redirect = '/admin/shop/wishlists';
     if ($custom_redirect = \Dsc\System::instance()->get('session')->get('shop.checkout.redirect')) {
         $redirect = $custom_redirect;
     }
     \Dsc\System::instance()->get('session')->set('shop.checkout.redirect', null);
     // -----------------------------------------------------
     // Start: validation
     // -----------------------------------------------------
     try {
         $wishlist = $this->getItem();
         if (empty($wishlist->id)) {
             throw new \Exception('Invalid Wishlist ID');
         }
     } catch (\Exception $e) {
         if ($this->app->get('AJAX')) {
             return $this->outputJson($this->getJsonResponse(array('result' => false)));
         } else {
             \Dsc\System::addMessage($e->getMessage(), 'error');
             $this->app->reroute($redirect);
             return;
         }
     }
     // -----------------------------------------------------
     // End: validation
     // -----------------------------------------------------
     // -----------------------------------------------------
     // Start: create the order
     // -----------------------------------------------------
     try {
         // Update the wishlist 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;
         }
         $wishlist_checkout = array_merge((array) $wishlist->{'checkout'}, $checkout_inputs);
         $wishlist->checkout = $wishlist_checkout;
         $wishlist->save();
         // Bind the wishlist and payment data to the checkout model
         $checkout = \Shop\Models\Checkout::instance();
         $checkout->addWishlist($wishlist)->addPaymentData($this->app->get('POST'));
         $order_inputs = $this->input->get('order', array(), 'array');
         $checkout->order()->bind($order_inputs);
     } catch (\Exception $e) {
         if ($this->app->get('AJAX')) {
             return $this->outputJson($this->getJsonResponse(array('result' => false)));
         } else {
             \Dsc\System::addMessage($e->getMessage(), 'error');
             $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.', '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 = '/admin/shop/wishlists';
         if ($custom_redirect = \Dsc\System::instance()->get('session')->get('shop.checkout.redirect_fail')) {
             $redirect = $custom_redirect;
         }
         \Dsc\System::instance()->get('session')->set('shop.checkout.redirect_fail', null);
         $this->app->reroute($redirect);
         return;
     }
     // the order WAS accepted
     // Fire an afterShopCheckout event
     try {
         $event_after = \Dsc\System::instance()->trigger('afterShopCheckout', array('checkout' => $checkout));
     } catch (\Exception $e) {
         \Dsc\System::addMessage($e->getMessage(), 'warning');
     }
     // -----------------------------------------------------
     // End: create the order
     // -----------------------------------------------------
     if ($this->app->get('AJAX')) {
         return $this->outputJson($this->getJsonResponse(array('result' => true)));
     } else {
         \Dsc\System::addMessage('Order created');
         $this->app->reroute($redirect);
     }
 }
Пример #3
0
 /**
  * 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;
 }