|
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\Component\Core\OrderProcessing; |
|
13
|
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
15
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
|
16
|
|
|
use Sylius\Component\Core\Payment\Exception\NotProvidedOrderPaymentException; |
|
17
|
|
|
use Sylius\Component\Core\Payment\Provider\OrderPaymentProviderInterface; |
|
18
|
|
|
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface; |
|
19
|
|
|
use Sylius\Component\Order\Processor\OrderProcessorInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
24
|
|
|
* @author Mateusz Zalewski <[email protected]> |
|
25
|
|
|
* @author Anna Walasek <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class OrderPaymentProcessor implements OrderProcessorInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var OrderPaymentProviderInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $orderPaymentProvider; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $targetState; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param OrderPaymentProviderInterface $orderPaymentProvider |
|
41
|
|
|
* @param string $targetState |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
OrderPaymentProviderInterface $orderPaymentProvider, |
|
45
|
|
|
$targetState = PaymentInterface::STATE_CART |
|
46
|
|
|
) { |
|
47
|
|
|
$this->orderPaymentProvider = $orderPaymentProvider; |
|
48
|
|
|
$this->targetState = $targetState; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function process(BaseOrderInterface $order) |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var OrderInterface $order */ |
|
57
|
|
|
Assert::isInstanceOf($order, OrderInterface::class); |
|
58
|
|
|
|
|
59
|
|
|
if (OrderInterface::STATE_CANCELLED === $order->getState()) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$lastPayment = $order->getLastPayment($this->targetState); |
|
64
|
|
|
if (null !== $lastPayment) { |
|
65
|
|
|
$lastPayment->setCurrencyCode($order->getCurrencyCode()); |
|
66
|
|
|
$lastPayment->setAmount($order->getTotal()); |
|
67
|
|
|
|
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$newPayment = $this->orderPaymentProvider->provideOrderPayment($order, $this->targetState); |
|
73
|
|
|
$order->addPayment($newPayment); |
|
|
|
|
|
|
74
|
|
|
} catch (NotProvidedOrderPaymentException $exception) { |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: