Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Plugin/OrderStatusHistoryCommentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) . '"',
Expand Down Expand Up @@ -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
Expand Down
42 changes: 19 additions & 23 deletions Test/Unit/Plugin/OrderStatusHistoryCommentPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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()
Expand Down
Loading