|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
15
|
|
|
use FOS\RestBundle\View\View; |
|
16
|
|
|
use Sylius\Bundle\OrderBundle\Controller\OrderController as BaseOrderController; |
|
17
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
|
18
|
|
|
use Sylius\Component\Resource\ResourceActions; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
22
|
|
|
use Webmozart\Assert\Assert; |
|
23
|
|
|
|
|
24
|
|
|
class OrderController extends BaseOrderController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* |
|
29
|
|
|
* @return Response |
|
30
|
|
|
*/ |
|
31
|
|
|
public function completeAction(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
34
|
|
|
|
|
35
|
|
|
$this->isGrantedOr403($configuration, ResourceActions::UPDATE); |
|
36
|
|
|
$order = $this->findOr404($configuration); |
|
37
|
|
|
|
|
38
|
|
|
$form = $this->resourceFormFactory->create($configuration, $order); |
|
39
|
|
|
|
|
40
|
|
|
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) { |
|
41
|
|
|
$order = $form->getData(); |
|
42
|
|
|
|
|
43
|
|
|
/** @var ResourceControllerEvent $event */ |
|
44
|
|
|
$event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $order); |
|
45
|
|
|
|
|
46
|
|
|
if ($event->isStopped() && !$configuration->isHtmlRequest()) { |
|
47
|
|
|
throw new HttpException($event->getErrorCode(), $event->getMessage()); |
|
48
|
|
|
} |
|
49
|
|
|
if ($event->isStopped()) { |
|
50
|
|
|
$this->flashHelper->addFlashFromEvent($configuration, $event); |
|
51
|
|
|
|
|
52
|
|
|
if ($event->hasResponse()) { |
|
53
|
|
|
return $event->getResponse(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirectHandler->redirectToResource($configuration, $order); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
|
|
if ($configuration->hasStateMachine()) { |
|
61
|
|
|
$this->stateMachine->apply($configuration, $order); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->manager->flush(); |
|
65
|
|
|
} catch (OptimisticLockException $exception) { |
|
66
|
|
|
$this->addFlash('error', 'sylius.checkout.complete_error'); |
|
67
|
|
|
|
|
68
|
|
|
return $this->redirectHandler->redirectToRoute($configuration, 'sylius_shop_checkout_complete'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $order); |
|
72
|
|
|
|
|
73
|
|
|
if (!$configuration->isHtmlRequest()) { |
|
74
|
|
|
return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $order); |
|
78
|
|
|
|
|
79
|
|
|
return $this->redirectHandler->redirectToResource($configuration, $order); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!$configuration->isHtmlRequest()) { |
|
83
|
|
|
return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$view = View::create() |
|
87
|
|
|
->setData([ |
|
88
|
|
|
'configuration' => $configuration, |
|
89
|
|
|
'metadata' => $this->metadata, |
|
90
|
|
|
'order' => $order, |
|
91
|
|
|
'form' => $form->createView(), |
|
92
|
|
|
]) |
|
93
|
|
|
->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html')) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
return $this->viewHandler->handle($configuration, $view); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Request $request |
|
101
|
|
|
* |
|
102
|
|
|
* @return Response |
|
103
|
|
|
*/ |
|
104
|
|
|
public function thankYouAction(Request $request) |
|
105
|
|
|
{ |
|
106
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
|
107
|
|
|
|
|
108
|
|
|
$orderId = $request->getSession()->get('sylius_order_id', null); |
|
109
|
|
|
|
|
110
|
|
|
if (null === $orderId) { |
|
111
|
|
|
$options = $configuration->getParameters()->get('after_failure'); |
|
112
|
|
|
|
|
113
|
|
|
return $this->redirectHandler->redirectToRoute( |
|
114
|
|
|
$configuration, |
|
115
|
|
|
isset($options['route']) ? $options['route'] : 'sylius_shop_homepage', |
|
116
|
|
|
isset($options['parameters']) ? $options['parameters'] : [] |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$request->getSession()->remove('sylius_order_id'); |
|
121
|
|
|
$order = $this->repository->find($orderId); |
|
122
|
|
|
Assert::notNull($order); |
|
123
|
|
|
|
|
124
|
|
|
$view = View::create() |
|
125
|
|
|
->setData([ |
|
126
|
|
|
'order' => $order |
|
127
|
|
|
]) |
|
128
|
|
|
->setTemplate($configuration->getParameters()->get('template')) |
|
129
|
|
|
; |
|
130
|
|
|
|
|
131
|
|
|
return $this->viewHandler->handle($configuration, $view); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|