diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index f74fecdfc..7142a35a1 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -93,6 +93,8 @@
+ + diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 53a0408a0..917ce6084 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -135,6 +135,11 @@ order-invoice + + Thelia\Controller\Front\CouponController::consume + order-invoice + + Thelia\Controller\Front\OrderController::pay diff --git a/core/lib/Thelia/Controller/Front/CouponController.php b/core/lib/Thelia/Controller/Front/CouponController.php new file mode 100755 index 000000000..d28513455 --- /dev/null +++ b/core/lib/Thelia/Controller/Front/CouponController.php @@ -0,0 +1,94 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Controller\Front; + +use Propel\Runtime\Exception\PropelException; +use Thelia\Core\Event\Coupon\CouponConsumeEvent; +use Thelia\Exception\TheliaProcessException; +use Thelia\Form\CouponCode; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Core\Event\Order\OrderEvent; +use Thelia\Core\Event\TheliaEvents; +use Symfony\Component\HttpFoundation\Request; +use Thelia\Form\OrderDelivery; +use Thelia\Form\OrderPayment; +use Thelia\Log\Tlog; +use Thelia\Model\AddressQuery; +use Thelia\Model\AreaDeliveryModuleQuery; +use Thelia\Model\Base\OrderQuery; +use Thelia\Model\ModuleQuery; +use Thelia\Model\Order; +use Thelia\Tools\URL; + +/** + * Class CouponController + * @package Thelia\Controller\Front + * @author Guillaume MOREL + */ +class CouponController extends BaseFrontController +{ + + /** + * Test Coupon consuming + */ + public function consumeAction() + { + $this->checkAuth(); + $this->checkCartNotEmpty(); + + $message = false; + $couponCodeForm = new CouponCode($this->getRequest()); + + try { + $form = $this->validateForm($couponCodeForm, 'post'); + + $couponCode = $form->get('coupon-code')->getData(); + + if (null === $couponCode || empty($couponCode)) { + $message = true; + throw new \Exception('Coupon code can\'t be empty'); + } + + $couponConsumeEvent = new CouponConsumeEvent($couponCode); + + // Dispatch Event to the Action + $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent); + } catch (FormValidationException $e) { + $message = sprintf('Please check your coupon code: %s', $e->getMessage()); + } catch (PropelException $e) { + $this->getParserContext()->setGeneralError($e->getMessage()); + } catch (\Exception $e) { + $message = sprintf('Sorry, an error occurred: %s', $e->getMessage()); + } + + if ($message !== false) { + Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())); + + $couponCodeForm->setErrorMessage($message); + + $this->getParserContext() + ->addForm($couponCodeForm) + ->setGeneralError($message); + } + } +} diff --git a/core/lib/Thelia/Form/CouponCode.php b/core/lib/Thelia/Form/CouponCode.php new file mode 100755 index 000000000..06dceb177 --- /dev/null +++ b/core/lib/Thelia/Form/CouponCode.php @@ -0,0 +1,63 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints; +use Thelia\Model\ModuleQuery; +use Thelia\Module\BaseModule; + +/** + * Class CouponCode + * + * Manage how a coupon is entered by a customer + * + * @package Thelia\Form + * @author Guillaume MOREL + */ +class CouponCode extends BaseForm +{ + /** + * Build form + */ + protected function buildForm() + { + $this->formBuilder + ->add("coupon-code", "text", array( + "required" => true, + "constraints" => array( + new Constraints\NotBlank(), + ) + ) + ); + } + + /** + * Form name + * + * @return string + */ + public function getName() + { + return "thelia_coupon_code"; + } +} diff --git a/templates/default/order-invoice.html b/templates/default/order-invoice.html index 6f10d3029..b07e4d94c 100644 --- a/templates/default/order-invoice.html +++ b/templates/default/order-invoice.html @@ -30,9 +30,9 @@ 4 {intl l="Secure payment"} - {form name="thelia.order.payment"} + {form name="thelia.order.coupon"} - + {form_hidden_fields form=$form} @@ -137,12 +137,17 @@ -
- - - - -
+ {form_field form=$form field='coupon-code'} +
+ + + + + + {if $error}{$message}{/if} +
+ {/form_field} + @@ -155,7 +160,15 @@ + + {/form} + {form name="thelia.order.payment"} +
+ + {form_hidden_fields form=$form} + + {if $form_error}
{$form_error_message}
{/if}
{loop type="address" name="delivery-address" id={order attr="delivery_address"}}