-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAYOSWXP-156: webhooks: add sort-order for handlers to prevent wrong …
…order
- Loading branch information
1 parent
b3f1e8f
commit de976c1
Showing
7 changed files
with
133 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\Payone\Webhook\Handler; | ||
|
||
use PayonePayment\Components\AutomaticCaptureService\AutomaticCaptureServiceInterface; | ||
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface; | ||
use PayonePayment\Components\TransactionStatus\TransactionStatusService; | ||
use PayonePayment\Struct\PaymentTransaction; | ||
use Shopware\Core\System\SalesChannel\SalesChannelContext; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class AutoCaptureHandler implements WebhookHandlerInterface | ||
{ | ||
public function __construct( | ||
private readonly TransactionDataHandlerInterface $transactionDataHandler, | ||
private readonly AutomaticCaptureServiceInterface $automaticCaptureService | ||
) { | ||
} | ||
|
||
public function process(SalesChannelContext $salesChannelContext, Request $request): void | ||
{ | ||
$paymentTransaction = $this->transactionDataHandler->getPaymentTransactionByPayoneTransactionId( | ||
$salesChannelContext->getContext(), | ||
$request->request->getInt('txid') | ||
); | ||
|
||
if (!$paymentTransaction instanceof PaymentTransaction) { | ||
return; | ||
} | ||
|
||
$this->automaticCaptureService->captureIfPossible($paymentTransaction, $salesChannelContext); | ||
} | ||
|
||
public function supports(SalesChannelContext $salesChannelContext, array $data): bool | ||
{ | ||
return isset($data['txid']) && ($data['txaction'] ?? null) === TransactionStatusService::ACTION_APPOINTED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\Payone\Webhook\Handler; | ||
|
||
use PayonePayment\Components\AutomaticCaptureService\AutomaticCaptureServiceInterface; | ||
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface; | ||
use PayonePayment\Components\TransactionStatus\TransactionStatusService; | ||
use PayonePayment\Struct\PaymentTransaction; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Core\System\SalesChannel\SalesChannelContext; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @covers \PayonePayment\Payone\Webhook\Handler\AutoCaptureHandler | ||
*/ | ||
class AutoCaptureHandlerTest extends TestCase | ||
{ | ||
public function testSupports(): void | ||
{ | ||
$handler = new AutoCaptureHandler( | ||
$this->createMock(TransactionDataHandlerInterface::class), | ||
$this->createMock(AutomaticCaptureServiceInterface::class), | ||
); | ||
|
||
$salesChannelMock = $this->createMock(SalesChannelContext::class); | ||
|
||
static::assertTrue($handler->supports($salesChannelMock, ['txid' => 1, 'txaction' => 'appointed']), 'supports should return true, cause of valid txid nd correct status'); | ||
|
||
static::assertFalse($handler->supports($salesChannelMock, ['txaction' => 'appointed']), 'supports should return false, cause of missing txid'); | ||
static::assertFalse($handler->supports($salesChannelMock, ['txid' => 1]), 'supports should return false, cause of missing status'); | ||
static::assertFalse($handler->supports($salesChannelMock, []), 'supports should return false, cause of missing data'); | ||
static::assertFalse($handler->supports($salesChannelMock, []), 'supports should return false, cause of missing data'); | ||
|
||
static::assertFalse($handler->supports($salesChannelMock, ['txid' => 1, 'txaction' => TransactionStatusService::ACTION_CAPTURE]), 'supports should return false, cause of wrong status'); | ||
static::assertFalse($handler->supports($salesChannelMock, ['txid' => 1, 'txaction' => TransactionStatusService::ACTION_COMPLETED]), 'supports should return false, cause of wrong status'); | ||
static::assertFalse($handler->supports($salesChannelMock, ['txid' => 1, 'txaction' => TransactionStatusService::ACTION_FAILED]), 'supports should return false, cause of wrong status'); | ||
} | ||
|
||
public function testIsCaptureGotExecuted(): void | ||
{ | ||
$dataHandler = $this->createMock(TransactionDataHandlerInterface::class); | ||
$dataHandler->method('getPaymentTransactionByPayoneTransactionId')->willReturn($this->createMock(PaymentTransaction::class)); | ||
$captureService = $this->createMock(AutomaticCaptureServiceInterface::class); | ||
$captureService->expects(static::once())->method('captureIfPossible'); | ||
|
||
$request = new Request(); | ||
$request->request->set('txid', 123); | ||
|
||
(new AutoCaptureHandler($dataHandler, $captureService)) | ||
->process($this->createMock(SalesChannelContext::class), $request); | ||
} | ||
|
||
public function testIsCaptureGotNotExecutedIfTransactionIsNotFound(): void | ||
{ | ||
$dataHandler = $this->createMock(TransactionDataHandlerInterface::class); | ||
$dataHandler->method('getPaymentTransactionByPayoneTransactionId')->willReturn(null); | ||
$captureService = $this->createMock(AutomaticCaptureServiceInterface::class); | ||
$captureService->expects(static::never())->method('captureIfPossible'); | ||
|
||
$request = new Request(); | ||
$request->request->set('txid', 123); | ||
|
||
(new AutoCaptureHandler($dataHandler, $captureService)) | ||
->process($this->createMock(SalesChannelContext::class), $request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters