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\Payment\Provider; |
13
|
|
|
|
14
|
|
|
use SM\Factory\FactoryInterface as StateMachineFactoryInterface; |
15
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
16
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
17
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
18
|
|
|
use Sylius\Component\Core\Payment\Exception\NotProvidedOrderPaymentException; |
19
|
|
|
use Sylius\Component\Payment\Exception\UnresolvedDefaultPaymentMethodException; |
20
|
|
|
use Sylius\Component\Payment\Factory\PaymentFactoryInterface; |
21
|
|
|
use Sylius\Component\Payment\PaymentTransitions; |
22
|
|
|
use Sylius\Component\Payment\Resolver\DefaultPaymentMethodResolverInterface; |
23
|
|
|
use Sylius\Component\Resource\StateMachine\StateMachineInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Mateusz Zalewski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class OrderPaymentProvider implements OrderPaymentProviderInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DefaultPaymentMethodResolverInterface |
32
|
|
|
*/ |
33
|
|
|
private $defaultPaymentMethodResolver; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PaymentFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $paymentFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var StateMachineFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
private $stateMachineFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver |
47
|
|
|
* @param PaymentFactoryInterface $paymentFactory |
48
|
|
|
* @param StateMachineFactoryInterface $stateMachineFactory |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver, |
|
|
|
|
52
|
|
|
PaymentFactoryInterface $paymentFactory, |
53
|
|
|
StateMachineFactoryInterface $stateMachineFactory |
54
|
|
|
) { |
55
|
|
|
$this->defaultPaymentMethodResolver = $defaultPaymentMethodResolver; |
56
|
|
|
$this->paymentFactory = $paymentFactory; |
57
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function provideOrderPayment(OrderInterface $order, $targetState) |
64
|
|
|
{ |
65
|
|
|
/** @var PaymentInterface $payment */ |
66
|
|
|
$payment = $this->paymentFactory->createWithAmountAndCurrencyCode($order->getTotal(), $order->getCurrencyCode()); |
67
|
|
|
|
68
|
|
|
$paymentMethod = $this->getDefaultPaymentMethod($payment, $order); |
69
|
|
|
$lastPayment = $this->getLastPayment($order); |
70
|
|
|
|
71
|
|
|
if (null !== $lastPayment) { |
72
|
|
|
$paymentMethod = $lastPayment->getMethod(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (null === $paymentMethod) { |
76
|
|
|
throw new NotProvidedOrderPaymentException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$payment->setMethod($paymentMethod); |
80
|
|
|
$this->applyRequiredTransition($payment, $targetState); |
81
|
|
|
|
82
|
|
|
return $payment; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param OrderInterface $order |
87
|
|
|
* |
88
|
|
|
* @return PaymentInterface|null |
89
|
|
|
*/ |
90
|
|
|
private function getLastPayment(OrderInterface $order) |
91
|
|
|
{ |
92
|
|
|
$lastCancelledPayment = $order->getLastPayment(PaymentInterface::STATE_CANCELLED); |
93
|
|
|
if (null !== $lastCancelledPayment) { |
94
|
|
|
return $lastCancelledPayment; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$lastFailedPayment = $order->getLastPayment(PaymentInterface::STATE_FAILED); |
98
|
|
|
if (null !== $lastFailedPayment) { |
99
|
|
|
return $lastFailedPayment; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param PaymentInterface $payment |
107
|
|
|
* @param OrderInterface $order |
108
|
|
|
* |
109
|
|
|
* @return PaymentMethodInterface|null |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
private function getDefaultPaymentMethod(PaymentInterface $payment, OrderInterface $order) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$payment->setOrder($order); |
115
|
|
|
$paymentMethod = $this->defaultPaymentMethodResolver->getDefaultPaymentMethod($payment); |
116
|
|
|
|
117
|
|
|
return $paymentMethod; |
118
|
|
|
} catch (UnresolvedDefaultPaymentMethodException $exception) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param PaymentInterface $payment |
125
|
|
|
* @param string $targetState |
126
|
|
|
*/ |
127
|
|
|
private function applyRequiredTransition(PaymentInterface $payment, $targetState) |
128
|
|
|
{ |
129
|
|
|
if ($targetState === $payment->getState()) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @var StateMachineInterface $stateMachine */ |
134
|
|
|
$stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); |
135
|
|
|
|
136
|
|
|
$targetTransition = $stateMachine->getTransitionToState($targetState); |
137
|
|
|
if (null !== $targetTransition) { |
138
|
|
|
$stateMachine->apply($targetTransition); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.