-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
OrderController.php
143 lines (117 loc) · 4.86 KB
/
OrderController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/
namespace CoreShop\Bundle\FrontendBundle\Controller;
use CoreShop\Bundle\CoreBundle\Form\Type\Order\PaymentType;
use CoreShop\Bundle\WorkflowBundle\History\HistoryLogger;
use CoreShop\Component\Core\Model\OrderInterface;
use CoreShop\Component\Order\Model\CartInterface;
use CoreShop\Component\Order\OrderTransitions;
use CoreShop\Component\Order\Repository\OrderRepositoryInterface;
use CoreShop\Component\Payment\Model\PaymentInterface;
use CoreShop\Component\Payment\Repository\PaymentRepositoryInterface;
use CoreShop\Component\Resource\Repository\RepositoryInterface;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\Form\ClickableInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class OrderController extends FrontendController
{
/**
* @param Request $request
*
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function reviseAction(Request $request)
{
$token = $request->get('token');
$payment = null;
/** @var OrderInterface $order */
$order = $this->getOrderRepository()->findOneBy(['token' => $token]);
if (!$order instanceof OrderInterface) {
throw new NotFoundHttpException();
}
if ($request->query->has('paymentId')) {
$paymentObject = $this->getPaymentRepository()->find($request->query->get('paymentId'));
if ($paymentObject instanceof PaymentInterface) {
$payment = $paymentObject;
}
}
foreach ($this->getPaymentRepository()->findForPayable($order) as $payment) {
if ($payment->getState() === PaymentInterface::STATE_COMPLETED) {
$this->addFlash('error', $this->get('translator')->trans('coreshop.ui.error.order_already_paid'));
return $this->redirectToRoute('coreshop_index');
}
}
$form = $this->getFormFactory()->createNamed('coreshop', PaymentType::class, $order, [
'payment_subject' => $order,
]);
if ($request->isMethod('post')) {
$form = $form->handleRequest($request);
$cancelButton = $form->get('cancel');
if ($cancelButton instanceof ClickableInterface && $form->isSubmitted() && $cancelButton->isClicked()) {
$this->get('coreshop.state_machine_applier')->apply($order, OrderTransitions::IDENTIFIER, OrderTransitions::TRANSITION_CANCEL);
if ($order instanceof Concrete) {
$this->get(HistoryLogger::class)->log(
$order,
'User Cart Revise Cancellation'
);
}
$cart = $this->get('coreshop.repository.cart')->findCartByOrder($order);
if ($cart instanceof CartInterface) {
$cart->setOrder(null);
$this->get('coreshop.cart.manager')->persistCart($cart);
$session = $request->getSession();
$session->set(
sprintf('%s.%s', $this->getParameter('coreshop.session.cart'), $cart->getStore()->getId()),
$cart->getId()
);
return $this->redirectToRoute('coreshop_cart_summary');
}
return $this->redirectToRoute('coreshop_index');
}
if ($form->isValid()) {
$order = $form->getData();
$order->save();
return $this->redirectToRoute('coreshop_order_revise_pay', ['token' => $token]);
}
}
$args = [
'order' => $order,
'payment' => $payment,
'form' => $form->createView(),
];
return $this->renderTemplate($this->templateConfigurator->findTemplate('Order/revise.html'), $args);
}
/**
* @return OrderRepositoryInterface
*/
protected function getOrderRepository()
{
return $this->get('coreshop.repository.order');
}
/**
* @return PaymentRepositoryInterface
*/
private function getPaymentRepository()
{
return $this->get('coreshop.repository.payment');
}
/**
* @return FormFactoryInterface
*/
protected function getFormFactory()
{
return $this->get('form.factory');
}
}