-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added customer vault token route
- Loading branch information
Showing
13 changed files
with
267 additions
and
89 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
34 changes: 34 additions & 0 deletions
34
src/Checkout/Exception/MissingCustomerVaultTokenException.php
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,34 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* (c) shopware AG <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Swag\PayPal\Checkout\Exception; | ||
|
||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\Framework\ShopwareHttpException; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
#[Package('checkout')] | ||
class MissingCustomerVaultTokenException extends ShopwareHttpException | ||
{ | ||
public function __construct(string $customerId) | ||
{ | ||
parent::__construct( | ||
'Missing vault token for customer "{{ customerId }}"', | ||
['customerId' => $customerId] | ||
); | ||
} | ||
|
||
public function getStatusCode(): int | ||
{ | ||
return Response::HTTP_BAD_REQUEST; | ||
} | ||
|
||
public function getErrorCode(): string | ||
{ | ||
return 'SWAG_PAYPAL__MISSING_CUSTOMER_VAULT_TOKEN'; | ||
} | ||
} |
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,74 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* (c) shopware AG <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Swag\PayPal\Checkout\SalesChannel; | ||
|
||
use OpenApi\Attributes as OA; | ||
use Shopware\Core\Checkout\Customer\CustomerException; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; | ||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\System\SalesChannel\SalesChannelContext; | ||
use Swag\PayPal\Checkout\Exception\MissingCustomerVaultTokenException; | ||
use Swag\PayPal\Checkout\TokenResponse; | ||
use Swag\PayPal\DataAbstractionLayer\VaultToken\VaultTokenEntity; | ||
use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
#[Package('checkout')] | ||
#[Route(defaults: ['_routeScope' => ['store-api']])] | ||
class CustomerVaultTokenRoute | ||
{ | ||
/** | ||
* @internal | ||
*/ | ||
public function __construct( | ||
private EntityRepository $vaultRepository, | ||
private TokenResourceInterface $tokenResource, | ||
) { | ||
} | ||
|
||
#[OA\Get( | ||
path: '/paypal/vault-token', | ||
operationId: 'getPayPalCustomerVaultToken', | ||
description: 'Tries to get the customer vault token', | ||
tags: ['Store API', 'PayPal'], | ||
responses: [new OA\Response( | ||
response: Response::HTTP_OK, | ||
description: 'The customer vault token', | ||
content: new OA\JsonContent(properties: [new OA\Property( | ||
property: 'token', | ||
type: 'string' | ||
)]) | ||
)], | ||
)] | ||
#[Route(path: '/store-api/paypal/vault-token', name: 'store-api.paypal.vault.token', defaults: ['_loginRequired' => true, '_loginRequiredAllowGuest' => false], methods: ['GET'])] | ||
public function getVaultToken(SalesChannelContext $context): TokenResponse | ||
{ | ||
$customer = $context->getCustomer(); | ||
if (!$customer || $customer->getGuest()) { | ||
throw CustomerException::customerNotLoggedIn(); | ||
} | ||
|
||
$criteria = new Criteria(); | ||
$criteria->addFilter(new EqualsFilter('mainMapping.customerId', $customer->getId())); | ||
$criteria->addFilter(new EqualsFilter('mainMapping.paymentMethodId', $context->getPaymentMethod()->getId())); | ||
|
||
/** @var VaultTokenEntity|null $vault */ | ||
$vault = $this->vaultRepository->search($criteria, $context->getContext())->first(); | ||
|
||
$token = $this->tokenResource->getUserIdToken($context->getSalesChannelId(), $vault?->getTokenCustomer())->getIdToken(); | ||
|
||
if ($token === null) { | ||
throw new MissingCustomerVaultTokenException($customer->getId()); | ||
} | ||
|
||
return new TokenResponse($token); | ||
} | ||
} |
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
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
103 changes: 103 additions & 0 deletions
103
tests/Checkout/SalesChannel/CustomerVaultTokenRouteTest.php
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,103 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* (c) shopware AG <[email protected]> | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Swag\PayPal\Test\Checkout\SalesChannel; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Core\Checkout\Customer\CustomerException; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult; | ||
use Shopware\Core\Framework\Log\Package; | ||
use Shopware\Core\Test\Generator; | ||
use Swag\PayPal\Checkout\Exception\MissingCustomerVaultTokenException; | ||
use Swag\PayPal\Checkout\SalesChannel\CustomerVaultTokenRoute; | ||
use Swag\PayPal\DataAbstractionLayer\VaultToken\VaultTokenEntity; | ||
use Swag\PayPal\RestApi\V1\Api\Token; | ||
use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Package('checkout')] | ||
class CustomerVaultTokenRouteTest extends TestCase | ||
{ | ||
private EntityRepository&MockObject $repository; | ||
|
||
private TokenResourceInterface&MockObject $tokenResource; | ||
|
||
private CustomerVaultTokenRoute $route; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->repository = $this->createMock(EntityRepository::class); | ||
$this->tokenResource = $this->createMock(TokenResourceInterface::class); | ||
|
||
$this->route = new CustomerVaultTokenRoute($this->repository, $this->tokenResource); | ||
} | ||
|
||
public function testGetVaultTokenWithoutCustomer(): void | ||
{ | ||
$salesChannelContext = Generator::generateSalesChannelContext(); | ||
$salesChannelContext->assign(['customer' => null]); | ||
|
||
$this->expectException(CustomerException::class); | ||
|
||
$this->route->getVaultToken($salesChannelContext); | ||
} | ||
|
||
public function testGetVaultTokenWithGuestCustomer(): void | ||
{ | ||
$salesChannelContext = Generator::generateSalesChannelContext(); | ||
$salesChannelContext->getCustomer()?->setGuest(true); | ||
|
||
$this->expectException(CustomerException::class); | ||
|
||
$this->route->getVaultToken($salesChannelContext); | ||
} | ||
|
||
public function testGetVaultToken(): void | ||
{ | ||
$salesChannelContext = Generator::generateSalesChannelContext(); | ||
$salesChannelContext->getCustomer()?->setGuest(false); | ||
|
||
$entitySearchResult = $this->createMock(EntitySearchResult::class); | ||
$this->repository->expects(static::once())->method('search')->willReturn($entitySearchResult); | ||
$entitySearchResult->expects(static::once())->method('first')->willReturn(new VaultTokenEntity()); | ||
|
||
$token = new Token(); | ||
$token->assign(['idToken' => 'dummy-token', 'expiresIn' => 45000]); | ||
|
||
$this->tokenResource->expects(static::once())->method('getUserIdToken')->willReturn($token); | ||
|
||
$response = $this->route->getVaultToken($salesChannelContext); | ||
|
||
static::assertSame( | ||
$token->getIdToken(), | ||
$response->getToken() | ||
); | ||
} | ||
|
||
public function testVaultTokenIsNull(): void | ||
{ | ||
$salesChannelContext = Generator::generateSalesChannelContext(); | ||
$salesChannelContext->getCustomer()?->setGuest(false); | ||
|
||
$entitySearchResult = $this->createMock(EntitySearchResult::class); | ||
$this->repository->expects(static::once())->method('search')->willReturn($entitySearchResult); | ||
$entitySearchResult->expects(static::once())->method('first')->willReturn(new VaultTokenEntity()); | ||
|
||
$token = new Token(); | ||
$token->assign(['idToken' => null, 'expiresIn' => 45000]); | ||
|
||
$this->tokenResource->expects(static::once())->method('getUserIdToken')->willReturn($token); | ||
|
||
$this->expectException(MissingCustomerVaultTokenException::class); | ||
|
||
$this->route->getVaultToken($salesChannelContext); | ||
} | ||
} |
Oops, something went wrong.