TokenStruct example

if ($paymentHandler instanceof AsynchronousPaymentHandlerInterface) {
            $paymentFinalizeTransactionTime = $this->systemConfigService->get('core.cart.paymentFinalizeTransactionTime', $salesChannelContext->getSalesChannelId());

            if (\is_numeric($paymentFinalizeTransactionTime)) {
                $paymentFinalizeTransactionTime = (int) $paymentFinalizeTransactionTime;
                // setting is in minutes, token holds in seconds                 $paymentFinalizeTransactionTime *= 60;
            } else {
                $paymentFinalizeTransactionTime = null;
            }

            $tokenStruct = new TokenStruct(
                null,
                null,
                $transaction->getPaymentMethodId(),
                $transaction->getId(),
                $finishUrl,
                $paymentFinalizeTransactionTime,
                $errorUrl
            );

            $token = $this->tokenFactory->generateToken($tokenStruct);

            
$configuration->setValidationConstraints(new NoopConstraint());
        $connection = $this->createMock(Connection::class);
        $this->tokenFactory = new JWTFactoryV2($configuration$connection);
    }

    /** * @dataProvider dataProviderExpiration */
    public function testGenerateAndGetToken(int $expiration, bool $expired): void
    {
        $transaction = self::createTransaction();
        $tokenStruct = new TokenStruct(null, null, $transaction->getPaymentMethodId()$transaction->getId(), null, $expiration);
        $token = $this->tokenFactory->generateToken($tokenStruct);
        $tokenStruct = $this->tokenFactory->parseToken($token);

        static::assertEquals($transaction->getId()$tokenStruct->getTransactionId());
        static::assertEquals($transaction->getPaymentMethodId()$tokenStruct->getPaymentMethodId());
        static::assertEquals($token$tokenStruct->getToken());
        static::assertEqualsWithDelta(time() + $expiration$tokenStruct->getExpires(), 1);
        static::assertSame($expired$tokenStruct->isExpired());
    }

    public function testGetInvalidFormattedToken(): void
    {

        $transactionId = Uuid::randomHex();
        $transaction = new OrderTransactionEntity();
        $transaction->setId($transactionId);
        $order = new OrderEntity();
        $order->setId(Uuid::randomHex());
        $transaction->setOrder($order);
        $context = Generator::createSalesChannelContext();
        $request = new Request();

        $tokenFactory = $this->createMock(JWTFactoryV2::class);
        $tokenFactory->expects(static::once())->method('parseToken')->with('paymentToken')->willReturn(new TokenStruct('id', 'token', 'paymentMethodId', $transactionId, 'finishUrl', \PHP_INT_MAX));
        $tokenFactory->expects(static::once())->method('invalidateToken')->with('token');

        $eventDispatcher = $this->createMock(EventDispatcherInterface::class);
        $eventDispatcher->expects(static::once())->method('dispatch')->with(static::isInstanceOf(FinalizePaymentOrderTransactionCriteriaEvent::class));

        $transactionRepository = $this->createMock(EntityRepository::class);
        $transactionRepository->expects(static::once())->method('search')->with(static::callback(function DCriteria $criteria) use ($transactionId) {
            static::assertEquals($transactionId$criteria->getIds()[0]);
            static::assertSame('payment-service::load-transaction', $criteria->getTitle());
            static::assertTrue($criteria->hasAssociation('order'));
            static::assertTrue($criteria->hasAssociation('paymentMethod'));

            
static::assertIsString($client->getResponse()->getContent());
        $response = json_decode($client->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);
        static::assertArrayHasKey('errors', $response);
        static::assertSame('CHECKOUT__INVALID_PAYMENT_TOKEN', $response['errors'][0]['code']);
    }

    public function testValidTokenWithInvalidOrder(): void
    {
        $client = $this->getBrowser();

        $tokenStruct = new TokenStruct(null, null, Uuid::randomHex(), Uuid::randomHex(), 'testFinishUrl');
        $token = $this->tokenFactory->generateToken($tokenStruct);

        $client->request('GET', '/payment/finalize-transaction?_sw_payment_token=' . $token);

        static::assertIsString($client->getResponse()->getContent());
        $response = json_decode($client->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);
        static::assertArrayHasKey('errors', $response);
        static::assertSame('CHECKOUT__INVALID_PAYMENT_TOKEN', $response['errors'][0]['code']);
    }

    public function testValid(): void
    {
$response = $this->paymentService->handlePaymentByOrder($orderIdnew RequestDataBag()$salesChannelContext);

        static::assertNotNull($response);
        static::assertEquals(AsyncTestPaymentHandler::REDIRECT_URL, $response->getTargetUrl());

        $transaction = new OrderTransactionEntity();
        $transaction->setId($transactionId);
        $transaction->setPaymentMethodId($paymentMethodId);
        $transaction->setOrderId($orderId);
        $transaction->setStateId(Uuid::randomHex());
        $tokenStruct = new TokenStruct(null, null, $transaction->getPaymentMethodId()$transaction->getId(), 'testFinishUrl');
        $token = $this->tokenFactory->generateToken($tokenStruct);
        $request = new Request();
        $tokenStruct = $this->paymentService->finalizeTransaction($token$request$salesChannelContext);

        static::assertSame('testFinishUrl', $tokenStruct->getFinishUrl());
        $criteria = new Criteria([$transactionId]);
        $criteria->addAssociation('stateMachineState');
        $transactionEntity = $this->orderTransactionRepository->search($criteria$this->context)->first();

        static::assertNotNull($transactionEntity);
        static::assertInstanceOf(OrderTransactionEntity::class$transactionEntity);
        


        if (!$this->has($token)) {
            throw PaymentException::tokenInvalidated($token);
        }

        $errorUrl = $jwtToken->claims()->get('eul');

        /** @var \DateTimeImmutable $expires */
        $expires = $jwtToken->claims()->get('exp');

        return new TokenStruct(
            $jwtToken->claims()->get('jti'),
            $token,
            $jwtToken->claims()->get('pmi'),
            $jwtToken->claims()->get('sub'),
            $jwtToken->claims()->get('ful'),
            $expires->getTimestamp(),
            $errorUrl
        );
    }

    public function invalidateToken(string $token): bool
    {
Home | Imprint | This part of the site doesn't use cookies.