|
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\ApiBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\View\View; |
|
15
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
16
|
|
|
use SM\Factory\FactoryInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
18
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
|
19
|
|
|
use Sylius\Component\Core\OrderCheckoutTransitions; |
|
20
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
21
|
|
|
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Łukasz Chruściel <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class ShowAvailablePaymentMethodsController |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var FactoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $stateMachineFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var OrderRepositoryInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $orderRepository; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var PaymentMethodsResolverInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $paymentMethodResolver; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ViewHandlerInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $restViewHandler; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param FactoryInterface $stateMachineFactory |
|
54
|
|
|
* @param OrderRepositoryInterface $orderRepository |
|
55
|
|
|
* @param PaymentMethodsResolverInterface $paymentMethodResolver |
|
56
|
|
|
* @param ViewHandlerInterface $restViewHandler |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( |
|
59
|
|
|
FactoryInterface $stateMachineFactory, |
|
60
|
|
|
OrderRepositoryInterface $orderRepository, |
|
61
|
|
|
PaymentMethodsResolverInterface $paymentMethodResolver, |
|
|
|
|
|
|
62
|
|
|
ViewHandlerInterface $restViewHandler |
|
63
|
|
|
) { |
|
64
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
|
65
|
|
|
$this->orderRepository = $orderRepository; |
|
66
|
|
|
$this->paymentMethodResolver = $paymentMethodResolver; |
|
67
|
|
|
$this->restViewHandler = $restViewHandler; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param Request $request |
|
72
|
|
|
* |
|
73
|
|
|
* @return Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function showAction(Request $request) |
|
76
|
|
|
{ |
|
77
|
|
|
/** @var OrderInterface $cart */ |
|
78
|
|
|
$cart = $this->getCartOr404($request->attributes->get('orderId')); |
|
79
|
|
|
|
|
80
|
|
|
if (!$this->isCheckoutTransitionPossible($cart, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)) { |
|
81
|
|
|
throw new BadRequestHttpException('The payment methods cannot be resolved in the current state of cart!'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$payments = []; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($cart->getPayments() as $payment) { |
|
87
|
|
|
$payments['payments'][] = [ |
|
88
|
|
|
'methods' => $this->getPaymentMethods($payment, $cart->getLocaleCode()), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->restViewHandler->handle(View::create($payments)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param mixed $cartId |
|
97
|
|
|
* |
|
98
|
|
|
* @return OrderInterface |
|
99
|
|
|
*/ |
|
100
|
|
|
private function getCartOr404($cartId) |
|
101
|
|
|
{ |
|
102
|
|
|
$cart = $this->orderRepository->findCartById($cartId); |
|
103
|
|
|
|
|
104
|
|
|
if (null === $cart) { |
|
105
|
|
|
throw new NotFoundHttpException(sprintf("The cart with %s id could not be found!", $cartId)); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $cart; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param OrderInterface $cart |
|
113
|
|
|
* @param string $transition |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
private function isCheckoutTransitionPossible(OrderInterface $cart, $transition) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->can($transition); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param PaymentInterface $payment |
|
124
|
|
|
* @param string $locale |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
private function getPaymentMethods(PaymentInterface $payment, $locale) |
|
129
|
|
|
{ |
|
130
|
|
|
$paymentMethods = $this->paymentMethodResolver->getSupportedMethods($payment); |
|
131
|
|
|
|
|
132
|
|
|
$rawPaymentMethods = []; |
|
133
|
|
|
|
|
134
|
|
|
foreach ($paymentMethods as $paymentMethod) { |
|
135
|
|
|
$rawPaymentMethods[] = [ |
|
136
|
|
|
'id' => $paymentMethod->getId(), |
|
137
|
|
|
'code' => $paymentMethod->getCode(), |
|
138
|
|
|
'name' => $paymentMethod->getTranslation($locale)->getName(), |
|
|
|
|
|
|
139
|
|
|
'description' => $paymentMethod->getTranslation($locale)->getDescription(), |
|
|
|
|
|
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $rawPaymentMethods; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.