Skip to content

Commit 02d0e27

Browse files
PLGMAG2V2-522: Remove payment link from frontend order (#532)
* PLGMAG2V2-522: Remove payment link from frontend order * PLGMAG2V2-522: Improve isAreaCodeAdminHtml method * PLGMAG2V2-522: Add CHANGELOG entry * PLGMAG2V2-522: PHPCS tuning * Fix integration test * Small code and doc improvements * doc fix * PLGMAG2V2-522: Ignore PHP mess detector warning Co-authored-by: Vinod MultiSafepay <vinod.sowdagar@multisafepay.com>
1 parent 543cdb4 commit 02d0e27

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ 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+
### Fixed
9+
- Fixed an issue where the order state switch to 'Pending payment', after 'Completed' in rare cases
10+
11+
### Removed
12+
- Removed the payment link order comment for frontend orders, since it was causing an issue in rare cases because of saving the order
813

914
## [2.16.0] - 2022-06-29
1015
### Added

Observer/OrderPlaceAfterObserver.php

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function execute(Observer $observer): void
8282
try {
8383
$paymentUrl = $this->paymentLink->getPaymentLinkByOrder($order);
8484
$this->paymentLink->addPaymentLink($order, $paymentUrl);
85+
$this->logger->logInfoForOrder($orderId, 'Payment URL is: ' . $paymentUrl);
8586
} catch (InvalidApiKeyException $invalidApiKeyException) {
8687
$this->logger->logInvalidApiKeyException($invalidApiKeyException);
8788
$this->messageManager->addErrorMessage(__('The order can not be created, because the

Service/PaymentLink.php

+31-11
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717

1818
namespace MultiSafepay\ConnectCore\Service;
1919

20+
use Exception;
2021
use Magento\Framework\App\Area;
2122
use Magento\Framework\App\State;
2223
use Magento\Framework\Exception\LocalizedException;
2324
use Magento\Framework\Exception\NoSuchEntityException;
24-
use Magento\Framework\Phrase;
2525
use Magento\Sales\Api\Data\OrderInterface;
2626
use Magento\Sales\Api\OrderRepositoryInterface;
2727
use Magento\Sales\Model\Order\Payment;
2828
use Magento\Sales\Model\Order\Payment\Transaction;
2929
use MultiSafepay\ConnectCore\Model\Api\Initializer\OrderRequestInitializer;
3030
use MultiSafepay\Exception\ApiException;
3131
use Psr\Http\Client\ClientExceptionInterface;
32+
use MultiSafepay\ConnectCore\Logger\Logger;
3233

34+
/**
35+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36+
*/
3337
class PaymentLink
3438
{
3539
public const MULTISAFEPAY_PAYMENT_LINK_PARAM_NAME = 'payment_link';
@@ -49,21 +53,29 @@ class PaymentLink
4953
*/
5054
private $state;
5155

56+
/**
57+
* @var Logger
58+
*/
59+
private $logger;
60+
5261
/**
5362
* PaymentLink constructor.
5463
*
5564
* @param OrderRequestInitializer $orderRequestInitializer
5665
* @param OrderRepositoryInterface $orderRepository
5766
* @param State $state
67+
* @param Logger $logger
5868
*/
5969
public function __construct(
6070
OrderRequestInitializer $orderRequestInitializer,
6171
OrderRepositoryInterface $orderRepository,
62-
State $state
72+
State $state,
73+
Logger $logger
6374
) {
6475
$this->orderRequestInitializer = $orderRequestInitializer;
6576
$this->orderRepository = $orderRepository;
6677
$this->state = $state;
78+
$this->logger = $logger;
6779
}
6880

6981
/**
@@ -85,10 +97,13 @@ public function getPaymentLinkByOrder(OrderInterface $order): string
8597
}
8698

8799
/**
100+
* Add the payment link to the order
101+
*
88102
* @param OrderInterface $order
89103
* @param string $paymentLink
90104
* @return void
91105
* @throws LocalizedException
106+
* @throws Exception
92107
*/
93108
public function addPaymentLink(OrderInterface $order, string $paymentLink): void
94109
{
@@ -128,30 +143,35 @@ private function addToAdditionalInformation(Payment $payment, string $paymentUrl
128143
}
129144

130145
/**
146+
* Add the Payment link to the order comments if the order was placed in the admin backend
147+
*
131148
* @param OrderInterface $order
132149
* @param string $paymentUrl
133150
* @return void
151+
* @throws Exception
134152
*/
135153
private function addPaymentLinkToOrderComments(OrderInterface $order, string $paymentUrl): void
136154
{
137-
$order->addCommentToStatusHistory($this->getOrderCommentByAreaCode($paymentUrl));
138-
$this->orderRepository->save($order);
155+
if ($this->isAreaCodeAdminHtml()) {
156+
$order->addCommentToStatusHistory(__('Payment link for this transaction: %1', $paymentUrl)->render());
157+
$this->orderRepository->save($order);
158+
}
139159
}
140160

141161
/**
142-
* @param string $paymentUrl
143-
* @return Phrase
162+
* Check if this is being executed from the backend
163+
*
164+
* @return bool
144165
*/
145-
private function getOrderCommentByAreaCode(string $paymentUrl): Phrase
166+
private function isAreaCodeAdminHtml(): bool
146167
{
147168
try {
148169
$areaCode = $this->state->getAreaCode();
149170
} catch (LocalizedException $localizedException) {
150-
$areaCode = Area::AREA_ADMINHTML;
171+
$this->logger->logException($localizedException);
172+
return false;
151173
}
152174

153-
return $areaCode === Area::AREA_ADMINHTML
154-
? __('Payment link for this transaction: %1', $paymentUrl)
155-
: __('The user has been redirected to the following page: %1', $paymentUrl);
175+
return $areaCode === Area::AREA_ADMINHTML;
156176
}
157177
}

Test/Integration/Service/PaymentLinkTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class PaymentLinkTest extends AbstractPaymentTestCase
3838
protected function setUp(): void
3939
{
4040
$this->paymentLinkService = $this->getObjectManager()->create(PaymentLink::class);
41-
$this->getObjectManager()->get(State::class)->setAreaCode(Area::AREA_FRONTEND);
41+
$this->getObjectManager()->get(State::class)->setAreaCode(Area::AREA_ADMINHTML);
4242
}
4343

4444
/**
@@ -74,7 +74,7 @@ public function testAddPaymentLink(): void
7474
$payment->getAdditionalInformation(PaymentLink::MULTISAFEPAY_PAYMENT_LINK_PARAM_NAME)
7575
);
7676
self::assertEquals(
77-
__('The user has been redirected to the following page: %1', $fakePaymentLink)->render(),
77+
__('Payment link for this transaction: %1', $fakePaymentLink)->render(),
7878
$order->getStatusHistoryCollection()->getFirstItem()->getComment()
7979
);
8080
}

0 commit comments

Comments
 (0)