|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
7
|
|
|
* an email on [email protected]. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Creator; |
|
13
|
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface; |
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Entity\OrderInterface; |
|
16
|
|
|
use BitBag\SyliusMolliePlugin\Entity\TemplateMollieEmailInterface; |
|
17
|
|
|
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory; |
|
18
|
|
|
use BitBag\SyliusMolliePlugin\Preparer\PaymentLinkEmailPreparerInterface; |
|
19
|
|
|
use BitBag\SyliusMolliePlugin\Repository\OrderRepositoryInterface; |
|
20
|
|
|
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface; |
|
21
|
|
|
use BitBag\SyliusMolliePlugin\Resolver\PaymentlinkResolverInterface; |
|
22
|
|
|
use Sylius\Component\Channel\Context\ChannelContextInterface; |
|
23
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
|
24
|
|
|
use Sylius\Component\Payment\Model\PaymentMethodInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class AbandonedPaymentLinkCreator implements AbandonedPaymentLinkCreatorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var PaymentlinkResolverInterface */ |
|
29
|
|
|
private $paymentLinkResolver; |
|
30
|
|
|
|
|
31
|
|
|
/** @var OrderRepositoryInterface */ |
|
32
|
|
|
private $orderRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** @var PaymentLinkEmailPreparerInterface */ |
|
35
|
|
|
private $emailPreparer; |
|
36
|
|
|
|
|
37
|
|
|
/** @var PaymentMethodRepositoryInterface */ |
|
38
|
|
|
private $paymentMethodRepository; |
|
39
|
|
|
|
|
40
|
|
|
/** @var ChannelContextInterface */ |
|
41
|
|
|
private $channelContext; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
PaymentlinkResolverInterface $paymentLinkResolver, |
|
45
|
|
|
OrderRepositoryInterface $orderRepository, |
|
46
|
|
|
PaymentLinkEmailPreparerInterface $emailPreparer, |
|
47
|
|
|
PaymentMethodRepositoryInterface $paymentMethodRepository, |
|
48
|
|
|
ChannelContextInterface $channelContext |
|
49
|
|
|
) { |
|
50
|
|
|
$this->paymentLinkResolver = $paymentLinkResolver; |
|
51
|
|
|
$this->orderRepository = $orderRepository; |
|
52
|
|
|
$this->emailPreparer = $emailPreparer; |
|
53
|
|
|
$this->paymentMethodRepository = $paymentMethodRepository; |
|
54
|
|
|
$this->channelContext = $channelContext; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function create(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName( |
|
60
|
|
|
$this->channelContext->getChannel(), |
|
61
|
|
|
MollieGatewayFactory::FACTORY_NAME |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
if (null === $paymentMethod) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @var GatewayConfigInterface $gateway */ |
|
69
|
|
|
$gateway = $paymentMethod->getGatewayConfig(); |
|
70
|
|
|
|
|
71
|
|
|
if (null === $gateway) { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$abandonedEnabled = $gateway->getConfig()['abandoned_email_enabled'] ?? false; |
|
76
|
|
|
|
|
77
|
|
|
if (false === $abandonedEnabled) { |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$abandonedDuration = $gateway->getConfig()['abandoned_hours'] ?? 4; |
|
82
|
|
|
|
|
83
|
|
|
$dateTime = new \DateTime('now'); |
|
84
|
|
|
$duration = new \DateInterval(\sprintf('PT%sH', $abandonedDuration)); |
|
85
|
|
|
$dateTime->sub($duration); |
|
86
|
|
|
|
|
87
|
|
|
$orders = $this->orderRepository->findAbandonedByDateTime($dateTime); |
|
88
|
|
|
|
|
89
|
|
|
/** @var OrderInterface $order */ |
|
90
|
|
|
foreach ($orders as $order) { |
|
91
|
|
|
/** @var PaymentInterface $payment */ |
|
92
|
|
|
$payment = $order->getPayments()->first(); |
|
93
|
|
|
|
|
94
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
|
95
|
|
|
$paymentMethod = $payment->getMethod(); |
|
96
|
|
|
|
|
97
|
|
|
/** @var \Payum\Core\Model\GatewayConfigInterface $gatewayConfig */ |
|
98
|
|
|
$gatewayConfig = $paymentMethod->getGatewayConfig(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
if ($gatewayConfig->getFactoryName() === MollieGatewayFactory::FACTORY_NAME) { |
|
|
|
|
|
|
101
|
|
|
$this->paymentLinkResolver->resolve($order, [], TemplateMollieEmailInterface::PAYMENT_LINK_ABANDONED); |
|
102
|
|
|
$order->setAbandonedEmail(true); |
|
103
|
|
|
$this->orderRepository->add($order); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|