-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathMollieOrderPaymentFlow.php
121 lines (103 loc) · 4.96 KB
/
MollieOrderPaymentFlow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php declare(strict_types=1);
namespace Kiener\MolliePayments\Facade;
use Kiener\MolliePayments\Service\Mollie\MolliePaymentStatus;
use Kiener\MolliePayments\Service\Mollie\OrderStatusConverter;
use Kiener\MolliePayments\Service\Order\OrderStatusUpdater;
use Kiener\MolliePayments\Service\PaymentMethodService;
use Kiener\MolliePayments\Service\SettingsService;
use Mollie\Api\Resources\Order;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
class MollieOrderPaymentFlow
{
/** @var OrderStatusConverter */
private $orderStatusConverter;
/** @var OrderStatusUpdater */
private $orderStatusUpdater;
/** @var SettingsService */
private $settingsService;
/** @var PaymentMethodService */
private $paymentMethodService;
/** @var EntityRepositoryInterface */
private $paymentMethodRepository;
/** @var EntityRepositoryInterface */
private $orderTransactionRepository;
public function __construct(
OrderStatusConverter $orderStatusConverter,
OrderStatusUpdater $orderStatusUpdater,
SettingsService $settingsService,
PaymentMethodService $paymentMethodService,
EntityRepositoryInterface $paymentMethodRepository,
EntityRepositoryInterface $orderTransactionRepository
) {
$this->orderStatusConverter = $orderStatusConverter;
$this->orderStatusUpdater = $orderStatusUpdater;
$this->settingsService = $settingsService;
$this->paymentMethodService = $paymentMethodService;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->orderTransactionRepository = $orderTransactionRepository;
}
/**
* @param OrderTransactionEntity $transaction
* @param OrderEntity $order
* @param Order $mollieOrder
* @param string $salesChannelId
* @param Context $context
* @throws \Mollie\Api\Exceptions\ApiException
* @return bool
*/
public function process(OrderTransactionEntity $transaction, OrderEntity $order, Order $mollieOrder, string $salesChannelId, Context $context): bool
{
$paymentStatus = $this->orderStatusConverter->getMollieOrderStatus($mollieOrder);
$settings = $this->settingsService->getSettings($salesChannelId);
// this is only mollie payment flow here we are doing failed management here
$this->orderStatusUpdater->updatePaymentStatus($transaction, $paymentStatus, $context);
$this->orderStatusUpdater->updateOrderStatus($order, $paymentStatus, $settings, $context);
//now check if payment method has changed, but only in case that it is no paid apple pay (apple pay returns credit card as method)
if (!$this->paymentMethodService->isPaidApplePayTransaction($transaction, $mollieOrder)) {
$currentCustomerSelectedPaymentMethod = $mollieOrder->method;
// check if it is mollie payment method
// ensure that we may only fetch mollie payment methods
$molliePaymentMethodId = $this->paymentMethodRepository->searchIds(
(new Criteria())
->addFilter(
new MultiFilter(
'AND',
[
new ContainsFilter('handlerIdentifier', 'Kiener\MolliePayments\Handler\Method'),
new EqualsFilter('customFields.mollie_payment_method_name', $currentCustomerSelectedPaymentMethod)
]
)
),
$context
)->firstId();
// if payment method has changed, update it
if (!is_null($molliePaymentMethodId) && $molliePaymentMethodId !== $transaction->getPaymentMethodId()) {
$transaction->setPaymentMethodId($molliePaymentMethodId);
$this->orderTransactionRepository->update(
[
[
'id' => $transaction->getUniqueIdentifier(),
'paymentMethodId' => $molliePaymentMethodId
]
],
$context
);
}
}
# our transaction has no payment method here?
# but it's also done in the finalize...this should be refactored
if (MolliePaymentStatus::isFailedStatus('', $paymentStatus)) {
$mollieOrder->createPayment([]);
return false;
}
return true;
}
}