Skip to content

Commit ab44fb2

Browse files
PLGMAG2V2-575: Bring back the payment link in order history (#552)
* PLGMAG2V2-575: Bring back the payment link in order history * Fix feedback * Clarify changelog * Remove unused private method * Fix feedback
1 parent 1acb240 commit ab44fb2

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- Payment links will now always be added to the order comment history again. For backend orders it happens immediately, for frontend orders it now happens when the first MultiSafepay notification arrives.
810

911
## [2.18.1] - 2022-09-12
1012
### Fixed

Service/OrderService.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class OrderService
8282
*/
8383
private $jsonHandler;
8484

85+
/**
86+
* @var PaymentLink
87+
*/
88+
private $paymentLink;
89+
8590
/**
8691
* OrderService constructor.
8792
*
@@ -94,6 +99,7 @@ class OrderService
9499
* @param ProcessChangePaymentMethod $processChangePaymentMethod
95100
* @param ProcessOrderByTransactionStatus $processOrderByTransactionStatus
96101
* @param JsonHandler $jsonHandler
102+
* @param PaymentLink $paymentLink
97103
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
98104
*/
99105
public function __construct(
@@ -105,7 +111,8 @@ public function __construct(
105111
ProcessVaultInitialization $processVaultInitialization,
106112
ProcessChangePaymentMethod $processChangePaymentMethod,
107113
ProcessOrderByTransactionStatus $processOrderByTransactionStatus,
108-
JsonHandler $jsonHandler
114+
JsonHandler $jsonHandler,
115+
PaymentLink $paymentLink
109116
) {
110117
$this->orderRepository = $orderRepository;
111118
$this->emailSender = $emailSender;
@@ -116,6 +123,7 @@ public function __construct(
116123
$this->processChangePaymentMethod = $processChangePaymentMethod;
117124
$this->processOrderByTransactionStatus = $processOrderByTransactionStatus;
118125
$this->jsonHandler = $jsonHandler;
126+
$this->paymentLink = $paymentLink;
119127
}
120128

121129
/**
@@ -206,6 +214,12 @@ public function processOrderTransaction(OrderInterface $order, array $transactio
206214
);
207215
}
208216

217+
$this->paymentLink->addPaymentLinkToOrderComments(
218+
$order,
219+
$this->paymentLink->getPaymentLinkFromOrder($order) ?? 'not found',
220+
true
221+
);
222+
209223
$this->orderRepository->save($order);
210224
$this->logger->logInfoForOrder(
211225
$orderId,

Service/PaymentLink.php

+72-5
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,88 @@ private function addToAdditionalInformation(Payment $payment, string $paymentUrl
143143
}
144144

145145
/**
146-
* Add the Payment link to the order comments if the order was placed in the admin backend
146+
* Add the Payment link to the order comments
147+
*
148+
* Saving the order when the order is being placed on the frontend can cause issues with the initialized
149+
* or completed notification, so we want to save the comments only in the notification process in those cases.
150+
* For admin backend orders, a payment link should always be added immediately.
147151
*
148152
* @param OrderInterface $order
149153
* @param string $paymentUrl
154+
* @param bool $isNotification
150155
* @return void
156+
* @throws LocalizedException
151157
* @throws Exception
152158
*/
153-
private function addPaymentLinkToOrderComments(OrderInterface $order, string $paymentUrl): void
154-
{
155-
if ($this->isAreaCodeAdminHtml()) {
156-
$order->addCommentToStatusHistory(__('Payment link for this transaction: %1', $paymentUrl)->render());
159+
public function addPaymentLinkToOrderComments(
160+
OrderInterface $order,
161+
string $paymentUrl,
162+
bool $isNotification = false
163+
): void {
164+
$isAdmin = $this->isAreaCodeAdminHtml();
165+
166+
if (!$isNotification && !$isAdmin) {
167+
return;
168+
}
169+
170+
/** @var Payment $payment */
171+
$payment = $order->getPayment();
172+
$orderId = $order->getIncrementId();
173+
174+
if ($payment === null) {
175+
$this->logger->logInfoForOrder($orderId, 'Payment object could not be found', Logger::DEBUG);
176+
177+
return;
178+
}
179+
180+
if ($this->hasMultiSafepayPaymentLinkComment($payment->getAdditionalInformation())) {
181+
$this->logger->logInfoForOrder(
182+
$orderId,
183+
'Payment link comment already added to the comment history, skipping',
184+
Logger::DEBUG
185+
);
186+
187+
return;
188+
}
189+
190+
$order->addCommentToStatusHistory(__('Payment link for this transaction: %1', $paymentUrl)->render());
191+
192+
$this->logger->logInfoForOrder(
193+
$orderId,
194+
'Payment link comment added to the comment history',
195+
Logger::DEBUG
196+
);
197+
198+
$payment->setAdditionalInformation('has_multisafepay_paymentlink_comment', true);
199+
200+
if ($isAdmin) {
157201
$this->orderRepository->save($order);
158202
}
159203
}
160204

205+
/**
206+
* Check if the order already has a payment link in the order history
207+
*
208+
* @param array $additionalInformation
209+
* @return bool
210+
*/
211+
private function hasMultiSafepayPaymentLinkComment(array $additionalInformation): bool
212+
{
213+
if (isset($additionalInformation['has_multisafepay_paymentlink_comment'])
214+
&& $additionalInformation['has_multisafepay_paymentlink_comment']
215+
) {
216+
return true;
217+
}
218+
219+
if (isset($additionalInformation[Transaction::RAW_DETAILS]['has_multisafepay_paymentlink_comment'])
220+
&& $additionalInformation[Transaction::RAW_DETAILS]['has_multisafepay_paymentlink_comment']
221+
) {
222+
return true;
223+
}
224+
225+
return false;
226+
}
227+
161228
/**
162229
* Check if this is being executed from the backend
163230
*

0 commit comments

Comments
 (0)