1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
6
|
|
|
|
7
|
|
|
use SM\Factory\FactoryInterface; |
8
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
9
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
10
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
11
|
|
|
use Sylius\Component\Core\OrderCheckoutTransitions; |
12
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
13
|
|
|
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; |
14
|
|
|
use Sylius\ShopApiPlugin\Command\ChoosePaymentMethod; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
|
18
|
|
|
final class ChoosePaymentMethodHandler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var OrderRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $orderRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PaymentMethodRepositoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $paymentMethodRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $stateMachineFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EventDispatcherInterface |
37
|
|
|
*/ |
38
|
|
|
private $eventDispatcher; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OrderRepositoryInterface $orderRepository |
42
|
|
|
* @param PaymentMethodRepositoryInterface $paymentMethodRepository |
43
|
|
|
* @param FactoryInterface $stateMachineFactory |
44
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
OrderRepositoryInterface $orderRepository, |
48
|
|
|
PaymentMethodRepositoryInterface $paymentMethodRepository, |
49
|
|
|
FactoryInterface $stateMachineFactory, |
50
|
|
|
EventDispatcherInterface $eventDispatcher |
51
|
|
|
) { |
52
|
|
|
$this->orderRepository = $orderRepository; |
53
|
|
|
$this->paymentMethodRepository = $paymentMethodRepository; |
54
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
55
|
|
|
$this->eventDispatcher = $eventDispatcher; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ChoosePaymentMethod $choosePaymentMethod |
60
|
|
|
*/ |
61
|
|
|
public function handle(ChoosePaymentMethod $choosePaymentMethod) |
62
|
|
|
{ |
63
|
|
|
/** @var OrderInterface $cart */ |
64
|
|
|
$cart = $this->orderRepository->findOneBy(['tokenValue' => $choosePaymentMethod->orderToken()]); |
65
|
|
|
|
66
|
|
|
Assert::notNull($cart, 'Cart has not been found.'); |
67
|
|
|
|
68
|
|
|
$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH); |
69
|
|
|
|
70
|
|
|
Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT), 'Order cannot have payment method assigned.'); |
71
|
|
|
|
72
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
73
|
|
|
$paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $choosePaymentMethod->paymentMethod()]); |
74
|
|
|
|
75
|
|
|
Assert::notNull($paymentMethod, 'Payment method has not been found'); |
76
|
|
|
Assert::true(isset($cart->getPayments()[$choosePaymentMethod->paymentIdentifier()]), 'Can not find payment with given identifier.'); |
77
|
|
|
|
78
|
|
|
$payment = $cart->getPayments()[$choosePaymentMethod->paymentIdentifier()]; |
79
|
|
|
|
80
|
|
|
$payment->setMethod($paymentMethod); |
81
|
|
|
|
82
|
|
|
$this->eventDispatcher->dispatch('sylius.order.pre_payment', new ResourceControllerEvent($cart)); |
83
|
|
|
|
84
|
|
|
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT); |
85
|
|
|
|
86
|
|
|
$this->eventDispatcher->dispatch('sylius.order.post_payment', new ResourceControllerEvent($cart)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|