|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * (c) shopware AG <[email protected]> |
| 4 | + * For the full copyright and license information, please view the LICENSE |
| 5 | + * file that was distributed with this source code. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Swag\PayPal\Checkout\SalesChannel; |
| 9 | + |
| 10 | +use OpenApi\Attributes as OA; |
| 11 | +use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
| 12 | +use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
| 13 | +use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; |
| 14 | +use Shopware\Core\Framework\Log\Package; |
| 15 | +use Shopware\Core\System\SalesChannel\SalesChannelContext; |
| 16 | +use Swag\PayPal\DataAbstractionLayer\VaultToken\VaultTokenEntity; |
| 17 | +use Swag\PayPal\RestApi\V1\Resource\TokenResourceInterface; |
| 18 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | +use Symfony\Component\Routing\Attribute\Route; |
| 21 | + |
| 22 | +#[Package('checkout')] |
| 23 | +#[Route(defaults: ['_routeScope' => ['store-api']])] |
| 24 | +readonly class CustomerVaultTokenRoute |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private EntityRepository $vaultRepository, |
| 31 | + private TokenResourceInterface $tokenResource, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + #[OA\Get( |
| 36 | + path: '/paypal/vault-token', |
| 37 | + operationId: 'getPayPalCustomerVaultToken', |
| 38 | + description: 'Tries to get the customer vault token', |
| 39 | + tags: ['Store API', 'PayPal'], |
| 40 | + responses: [new OA\Response( |
| 41 | + response: Response::HTTP_OK, |
| 42 | + description: 'The customer vault token or null', |
| 43 | + content: new OA\JsonContent(properties: [new OA\Property( |
| 44 | + property: 'token', |
| 45 | + type: 'string' |
| 46 | + )]) |
| 47 | + )], |
| 48 | + )] |
| 49 | + #[Route(path: '/store-api/paypal/vault-token', name: 'store-api.paypal.vault.token', defaults: ['_loginRequired' => true], methods: ['GET'])] |
| 50 | + public function getVaultToken(SalesChannelContext $context): JsonResponse |
| 51 | + { |
| 52 | + $customer = $context->getCustomer(); |
| 53 | + if ($customer === null || $customer->getGuest() === true) { |
| 54 | + return new JsonResponse(['token' => null]); |
| 55 | + } |
| 56 | + |
| 57 | + $criteria = new Criteria(); |
| 58 | + $criteria->addFilter(new EqualsFilter('mainMapping.customerId', $customer->getId())); |
| 59 | + $criteria->addFilter(new EqualsFilter('mainMapping.paymentMethodId', $context->getPaymentMethod()->getId())); |
| 60 | + |
| 61 | + /** @var VaultTokenEntity|null $vault */ |
| 62 | + $vault = $this->vaultRepository->search($criteria, $context->getContext())->first(); |
| 63 | + |
| 64 | + if ($vault !== null) { |
| 65 | + return new JsonResponse( |
| 66 | + ['token' => $this->tokenResource->getUserIdToken($context->getSalesChannelId(), $vault->getTokenCustomer())->getIdToken()] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return new JsonResponse( |
| 71 | + ['token' => $this->tokenResource->getUserIdToken($context->getSalesChannelId())->getIdToken()] |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments