diff --git a/Plugin/OrderStatusHistoryCommentPlugin.php b/Plugin/OrderStatusHistoryCommentPlugin.php index cf2979c7a..0b87bfaf4 100644 --- a/Plugin/OrderStatusHistoryCommentPlugin.php +++ b/Plugin/OrderStatusHistoryCommentPlugin.php @@ -45,9 +45,10 @@ public function __construct(CheckPaymentType $checkPaymentType) } /** - * Normalize Buckaroo refund notes. + * Normalize Buckaroo Plaza refund notes. * * Plaza refunds should always be shown as "online" and include the Plaza Transaction ID. + * Magento Admin offline refunds (no Plaza key) are left unchanged. * * @param Order $subject * @param string|\Magento\Framework\Phrase $comment @@ -65,19 +66,18 @@ public function beforeAddStatusHistoryComment(Order $subject, $comment, $status return [$comment, $status]; } - $updatedComment = preg_replace('/\boffline\b/ui', 'online', $commentText, 1) ?: $commentText; - $transactionId = $this->getRefundTransactionId($subject); if (empty($transactionId)) { - return [$updatedComment, $status]; + return [$comment, $status]; } + $updatedComment = preg_replace('/\boffline\b/ui', 'online', $commentText, 1) ?: $commentText; + $hasTransactionId = preg_match('/Transaction\s*ID\s*:\s*"/ui', $updatedComment) === 1; if (!$hasTransactionId) { return [$this->appendTransactionId($updatedComment, $transactionId), $status]; } - // If Transaction ID exists but differs, override it with the latest credit memo transaction id. $updatedComment = preg_replace( '/Transaction\s*ID\s*:\s*"[^"]*"/ui', 'Transaction ID: "' . $this->buildTransactionIdLink($transactionId) . '"', @@ -126,7 +126,6 @@ private function appendTransactionId(string $commentText, string $transactionId) */ private function buildTransactionIdLink(string $transactionId): string { - // Keep consistent with HtmlTransactionIdObserver's plaza link structure. $url = sprintf( 'https://plaza.buckaroo.nl/Transaction/Transactions/Details?transactionKey=%s', $transactionId diff --git a/Test/Unit/Plugin/OrderStatusHistoryCommentPluginTest.php b/Test/Unit/Plugin/OrderStatusHistoryCommentPluginTest.php index ae26e0aa2..be926d61e 100644 --- a/Test/Unit/Plugin/OrderStatusHistoryCommentPluginTest.php +++ b/Test/Unit/Plugin/OrderStatusHistoryCommentPluginTest.php @@ -6,38 +6,40 @@ use Buckaroo\Magento2\Service\CheckPaymentType; use Buckaroo\Magento2\Test\BaseTest; use Magento\Sales\Model\Order; +use Magento\Sales\Model\Order\Payment; class OrderStatusHistoryCommentPluginTest extends BaseTest { protected $instanceClass = OrderStatusHistoryCommentPlugin::class; - public function testOfflineRefundCommentIsCleanedForBuckarooPayment(): void + public function testPlazaOfflineRefundCommentIsNormalizedToOnline(): void { - $orderMock = $this->getOrderMock(); + $orderMock = $this->getOrderMock('A7BDD18D052843098E3461FE3EDA423B'); $comment = 'We refunded $59.00 offline. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-capture"'; $result = $this->getInstance([ 'checkPaymentType' => $this->getCheckPaymentTypeMock(true), ])->beforeAddStatusHistoryComment($orderMock, $comment, 'closed'); - $this->assertSame(['We refunded $59.00 online. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-capture"', 'closed'], $result); + $this->assertStringContainsString('online', $result[0]); + $this->assertStringNotContainsString('offline', $result[0]); } - public function testOnlineRefundCommentIsLeftUntouched(): void + public function testAdminOfflineRefundCommentIsLeftUntouched(): void { - $orderMock = $this->getOrderMock(); - $comment = 'We refunded $59.00 online. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-refund"'; + $orderMock = $this->getOrderMock(null); + $comment = 'We refunded $59.00 offline. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-capture"'; $result = $this->getInstance([ 'checkPaymentType' => $this->getCheckPaymentTypeMock(true), - ])->beforeAddStatusHistoryComment($orderMock, $comment, 'processing'); + ])->beforeAddStatusHistoryComment($orderMock, $comment, 'closed'); - $this->assertSame([$comment, 'processing'], $result); + $this->assertSame([$comment, 'closed'], $result); } public function testNonBuckarooOfflineRefundCommentIsLeftUntouched(): void { - $orderMock = $this->getOrderMock(); + $orderMock = $this->getOrderMock(null); $comment = 'We refunded $59.00 offline. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-capture"'; $result = $this->getInstance([ @@ -47,21 +49,15 @@ public function testNonBuckarooOfflineRefundCommentIsLeftUntouched(): void $this->assertSame([$comment, false], $result); } - public function testBuckarooNonRefundCommentContainingTransactionIdIsLeftUntouched(): void + private function getOrderMock(?string $plazaRefundTransactionKey): Order { - $orderMock = $this->getOrderMock(); - $comment = 'Registered notification about captured amount of $59.00. Transaction ID: "A7BDD18D052843098E3461FE3EDA423B-capture"'; - - $result = $this->getInstance([ - 'checkPaymentType' => $this->getCheckPaymentTypeMock(true), - ])->beforeAddStatusHistoryComment($orderMock, $comment, 'processing'); - - $this->assertSame([$comment, 'processing'], $result); - } - - private function getOrderMock(): Order - { - $paymentMock = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class); + $paymentMock = $this->getMockBuilder(Payment::class) + ->disableOriginalConstructor() + ->onlyMethods(['getAdditionalInformation']) + ->getMock(); + $paymentMock->method('getAdditionalInformation') + ->with('buckaroo_refund_transaction_key') + ->willReturn($plazaRefundTransactionKey); $orderMock = $this->getMockBuilder(Order::class) ->disableOriginalConstructor()