-
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
11 changed files
with
224 additions
and
85 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
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\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\DataAbstractionLayer\VaultToken\VaultTokenEntity; | ||
use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
#[Package('checkout')] | ||
#[Route(defaults: ['_routeScope' => ['store-api']])] | ||
readonly 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 or null', | ||
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], methods: ['GET'])] | ||
public function getVaultToken(SalesChannelContext $context): JsonResponse | ||
{ | ||
$customer = $context->getCustomer(); | ||
if ($customer === null || $customer->getGuest() === true) { | ||
return new JsonResponse(['token' => null]); | ||
} | ||
|
||
$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(); | ||
|
||
if ($vault !== null) { | ||
return new JsonResponse( | ||
['token' => $this->tokenResource->getUserIdToken($context->getSalesChannelId(), $vault->getTokenCustomer())->getIdToken()] | ||
); | ||
} | ||
|
||
return new JsonResponse( | ||
['token' => $this->tokenResource->getUserIdToken($context->getSalesChannelId())->getIdToken()] | ||
); | ||
} | ||
} |
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
91 changes: 91 additions & 0 deletions
91
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,91 @@ | ||
<?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\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\SalesChannel\CustomerVaultTokenRoute; | ||
use Swag\PayPal\DataAbstractionLayer\VaultToken\VaultTokenEntity; | ||
use Swag\PayPal\RestApi\V1\Api\Token; | ||
use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; | ||
use Swag\PayPal\Test\Helper\SalesChannelContextTrait; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[Package('checkout')] | ||
class CustomerVaultTokenRouteTest extends TestCase | ||
{ | ||
use SalesChannelContextTrait; | ||
|
||
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]); | ||
|
||
$response = $this->route->getVaultToken($salesChannelContext); | ||
|
||
static::assertSame( | ||
'{"token":null}', | ||
(string) $response->getContent() | ||
); | ||
} | ||
|
||
public function testGetVaultTokenWithGuestCustomer(): void | ||
{ | ||
$salesChannelContext = Generator::generateSalesChannelContext(); | ||
$salesChannelContext->getCustomer()?->setGuest(true); | ||
|
||
$response = $this->route->getVaultToken($salesChannelContext); | ||
|
||
static::assertSame( | ||
'{"token":null}', | ||
(string) $response->getContent() | ||
); | ||
} | ||
|
||
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(), | ||
\json_decode((string) $response->getContent(), true, 512, \JSON_THROW_ON_ERROR)['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