Skip to content

Commit

Permalink
Store Payment details directly in notify action without any async mes…
Browse files Browse the repository at this point in the history
…sage
  • Loading branch information
lruozzi9 committed Jun 11, 2024
1 parent d590c94 commit 30247d1
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 176 deletions.
19 changes: 0 additions & 19 deletions config/packages/messenger.php

This file was deleted.

1 change: 0 additions & 1 deletion config/services/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
->public()
->args([
service('webgriffe_sylius_klarna.logger'),
service('webgriffe_sylius_klarna_plugin.command_bus'),
])
->tag('payum.action', ['factory' => KlarnaPaymentsApi::CODE, 'alias' => 'payum.action.notify'])
;
Expand Down
15 changes: 0 additions & 15 deletions config/services/message_handler.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Webgriffe\SyliusKlarnaPlugin\Helper\PaymentDetailsHelper;
use Webgriffe\SyliusKlarnaPlugin\Model\PaymentDetails;
use Webgriffe\SyliusKlarnaPlugin\PaymentDetailsHelper;
use Webgriffe\SyliusKlarnaPlugin\Payum\KlarnaPaymentsApi;
use Webmozart\Assert\Assert;

Expand Down
44 changes: 44 additions & 0 deletions src/Helper/PaymentDetailsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusKlarnaPlugin\Helper;

use Webgriffe\SyliusKlarnaPlugin\Model\PaymentDetails;
use Webmozart\Assert\Assert;
use Webmozart\Assert\InvalidArgumentException;

/**
* @psalm-import-type StoredPaymentDetails from PaymentDetails
*/
final class PaymentDetailsHelper
{
/**
* @phpstan-assert-if-true StoredPaymentDetails $storedPaymentDetails
*/
public static function areValid(array $storedPaymentDetails): bool
{
try {
self::assertStoredPaymentDetailsAreValid($storedPaymentDetails);
} catch (InvalidArgumentException) {
return false;
}

return true;
}

/**
* @TODO Da fare ancora
*
* @phpstan-assert StoredPaymentDetails $storedPaymentDetails
*
* @throws InvalidArgumentException
*/
public static function assertStoredPaymentDetailsAreValid(array $storedPaymentDetails): void
{
Assert::keyExists($storedPaymentDetails, PaymentDetails::PAYMENT_SESSION_ID_KEY);
Assert::stringNotEmpty($storedPaymentDetails[PaymentDetails::PAYMENT_SESSION_ID_KEY]);
Assert::keyExists($storedPaymentDetails, PaymentDetails::PAYMENT_CLIENT_TOKEN_KEY);
Assert::stringNotEmpty($storedPaymentDetails[PaymentDetails::PAYMENT_CLIENT_TOKEN_KEY]);
}
}
18 changes: 0 additions & 18 deletions src/Message/UpdatePaymentDetails.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/MessageHandler/UpdatePaymentDetailsHandler.php

This file was deleted.

122 changes: 85 additions & 37 deletions src/Model/PaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,48 @@

namespace Webgriffe\SyliusKlarnaPlugin\Model;

use Webgriffe\SyliusKlarnaPlugin\Client\Enum\HostedPaymentPageSessionStatus;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderStatus;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\PaymentSessionStatus;
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Response\PaymentSession;

/**
* @psalm-type KlarnaOrderDetails array{id: string, fraud_status: string, status: string}
* @psalm-type KlarnaPaymentDetails array{session_id: string, client_token: string}
* @psalm-type KlarnaHostedPaymentPageDetails array{session_id: string, session_url: string, distribution_url: string, expires_at: string, qr_code_url: string, redirect_url: string, distribution_module: array{generation_url: string, standalone_url: string, token: string}}
* @psalm-type StoredPaymentDetails array{payment: KlarnaPaymentDetails, hosted_payment_page?: KlarnaHostedPaymentPageDetails, order?: KlarnaOrderDetails}
* @psalm-type StoredPaymentDetails array{payment_session_id: string, payment_client_token: string, payment_status: ?string, hosted_payment_page_session_id: ?string, hosted_payment_page_redirect_url: ?string, hosted_payment_page_status: ?string, order_id: ?string, order_status: ?string, klarna_reference: ?string}
*/
final class PaymentDetails
{
public const PAYMENT_KEY = 'payment';
public const PAYMENT_SESSION_ID_KEY = 'payment_session_id';

public const PAYMENT_SESSION_ID_KEY = 'session_id';
public const PAYMENT_CLIENT_TOKEN_KEY = 'payment_client_token';

public const PAYMENT_CLIENT_TOKEN_KEY = 'client_token';
public const PAYMENT_STATUS_KEY = 'payment_status';

public const HOSTED_PAYMENT_PAGE_KEY = 'hosted_payment_page';
public const HOSTED_PAYMENT_PAGE_SESSION_ID_KEY = 'hosted_payment_page_session_id';

public const HOSTED_PAYMENT_PAGE_SESSION_ID_KEY = 'session_id';
public const HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY = 'hosted_payment_page_redirect_url';

public const ORDER_KEY = 'order';
public const HOSTED_PAYMENT_PAGE_STATUS_KEY = 'hosted_payment_page_status';

public const ORDER_ID_KEY = 'id';
public const ORDER_ID_KEY = 'order_id';

public const ORDER_FRAUD_STATUS_KEY = 'fraud_status';
public const ORDER_STATUS_KEY = 'order_status';

public const ORDER_STATUS_KEY = 'status';
public const KLARNA_REFERENCE_KEY = 'klarna_reference';

public const HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY = 'redirect_url';

private ?PaymentSessionStatus $paymentSessionStatus = null;
private ?PaymentSessionStatus $paymentStatus = null;

private ?string $hostedPaymentPageId = null;

private ?string $hostedPaymentPageRedirectUrl = null;

private ?HostedPaymentPageSessionStatus $hostedPaymentPageStatus = null;

private ?string $orderId = null;

private ?OrderStatus $orderStatus = null;

private ?string $klarnaReference = null;

private function __construct(
private readonly string $paymentSessionId,
private readonly string $paymentSessionClientToken,
Expand All @@ -59,12 +64,12 @@ public function getPaymentSessionClientToken(): string

public function getPaymentSessionStatus(): ?PaymentSessionStatus
{
return $this->paymentSessionStatus;
return $this->paymentStatus;
}

public function setPaymentSessionStatus(?PaymentSessionStatus $paymentSessionStatus): void
{
$this->paymentSessionStatus = $paymentSessionStatus;
$this->paymentStatus = $paymentSessionStatus;
}

public function getHostedPaymentPageId(): ?string
Expand All @@ -87,6 +92,46 @@ public function setHostedPaymentPageRedirectUrl(?string $hostedPaymentPageRedire
$this->hostedPaymentPageRedirectUrl = $hostedPaymentPageRedirectUrl;
}

public function getHostedPaymentPageStatus(): ?HostedPaymentPageSessionStatus
{
return $this->hostedPaymentPageStatus;
}

public function setHostedPaymentPageStatus(?HostedPaymentPageSessionStatus $hostedPaymentPageSessionStatus): void
{
$this->hostedPaymentPageStatus = $hostedPaymentPageSessionStatus;
}

public function getOrderId(): ?string
{
return $this->orderId;
}

public function setOrderId(?string $orderId): void
{
$this->orderId = $orderId;
}

public function getOrderStatus(): ?OrderStatus
{
return $this->orderStatus;
}

public function setOrderStatus(?OrderStatus $orderStatus): void
{
$this->orderStatus = $orderStatus;
}

public function getKlarnaReference(): ?string
{
return $this->klarnaReference;
}

public function setKlarnaReference(?string $klarnaReference): void
{
$this->klarnaReference = $klarnaReference;
}

/**
* @TODO
*/
Expand All @@ -109,13 +154,19 @@ public static function createFromPaymentSession(PaymentSession $paymentSession):
public static function createFromStoredPaymentDetails(array $storedPaymentDetails): self
{
$paymentDetails = new self(
$storedPaymentDetails[self::PAYMENT_KEY][self::PAYMENT_SESSION_ID_KEY],
$storedPaymentDetails[self::PAYMENT_KEY][self::PAYMENT_CLIENT_TOKEN_KEY],
$storedPaymentDetails[self::PAYMENT_SESSION_ID_KEY],
$storedPaymentDetails[self::PAYMENT_CLIENT_TOKEN_KEY],
);
if (isset($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_KEY])) {
$paymentDetails->setHostedPaymentPageId($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_KEY][self::HOSTED_PAYMENT_PAGE_SESSION_ID_KEY]);
$paymentDetails->setHostedPaymentPageRedirectUrl($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_KEY][self::HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY]);
}
$paymentDetails->setPaymentSessionStatus($storedPaymentDetails[self::PAYMENT_STATUS_KEY] !== null ? PaymentSessionStatus::from($storedPaymentDetails[self::PAYMENT_STATUS_KEY]) : null);

$paymentDetails->setHostedPaymentPageId($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_SESSION_ID_KEY]);
$paymentDetails->setHostedPaymentPageRedirectUrl($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY]);
$paymentDetails->setHostedPaymentPageStatus($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_STATUS_KEY] !== null ? HostedPaymentPageSessionStatus::from($storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_STATUS_KEY]) : null);

$paymentDetails->setOrderId($storedPaymentDetails[self::ORDER_ID_KEY]);
$paymentDetails->setOrderStatus($storedPaymentDetails[self::ORDER_STATUS_KEY] !== null ? OrderStatus::from($storedPaymentDetails[self::ORDER_STATUS_KEY]) : null);

$paymentDetails->setKlarnaReference($storedPaymentDetails[self::KLARNA_REFERENCE_KEY] ?? null);

return $paymentDetails;
}
Expand All @@ -125,19 +176,16 @@ public static function createFromStoredPaymentDetails(array $storedPaymentDetail
*/
public function toStoredPaymentDetails(): array
{
$storedPaymentDetails = [
self::PAYMENT_KEY => [
self::PAYMENT_SESSION_ID_KEY => $this->getPaymentSessionId(),
self::PAYMENT_CLIENT_TOKEN_KEY => $this->getPaymentSessionClientToken(),
],
return [
self::PAYMENT_SESSION_ID_KEY => $this->getPaymentSessionId(),
self::PAYMENT_CLIENT_TOKEN_KEY => $this->getPaymentSessionClientToken(),
self::PAYMENT_STATUS_KEY => $this->getPaymentSessionStatus()?->value,
self::HOSTED_PAYMENT_PAGE_SESSION_ID_KEY => $this->getHostedPaymentPageId(),
self::HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY => $this->getHostedPaymentPageRedirectUrl(),
self::HOSTED_PAYMENT_PAGE_STATUS_KEY => $this->getHostedPaymentPageStatus()?->value,
self::ORDER_ID_KEY => $this->getOrderId(),
self::ORDER_STATUS_KEY => $this->getOrderStatus()?->value,
self::KLARNA_REFERENCE_KEY => $this->getKlarnaReference(),
];
if ($this->getHostedPaymentPageId() !== null) {
$storedPaymentDetails[self::HOSTED_PAYMENT_PAGE_KEY] = [
self::HOSTED_PAYMENT_PAGE_SESSION_ID_KEY => $this->getHostedPaymentPageId(),
self::HOSTED_PAYMENT_PAGE_REDIRECT_URL_KEY => $this->getHostedPaymentPageRedirectUrl(),
];
}

return $storedPaymentDetails;
}
}
59 changes: 0 additions & 59 deletions src/PaymentDetailsHelper.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Payum/Action/CancelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Payum\Core\Request\Cancel;
use Psr\Log\LoggerInterface;
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface;
use Webgriffe\SyliusKlarnaPlugin\Helper\PaymentDetailsHelper;
use Webgriffe\SyliusKlarnaPlugin\Model\PaymentDetails;
use Webgriffe\SyliusKlarnaPlugin\PaymentDetailsHelper;
use Webmozart\Assert\Assert;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Response\HostedPaymentPageSession;
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Response\PaymentSession;
use Webgriffe\SyliusKlarnaPlugin\Controller\PaymentController;
use Webgriffe\SyliusKlarnaPlugin\Helper\PaymentDetailsHelper;
use Webgriffe\SyliusKlarnaPlugin\Model\PaymentDetails;
use Webgriffe\SyliusKlarnaPlugin\PaymentDetailsHelper;
use Webgriffe\SyliusKlarnaPlugin\Payum\KlarnaPaymentsApi;
use Webgriffe\SyliusKlarnaPlugin\Payum\Request\Api\CreateHostedPaymentPageSession;
use Webgriffe\SyliusKlarnaPlugin\Payum\Request\Api\CreatePaymentSession;
Expand Down
Loading

0 comments on commit 30247d1

Please sign in to comment.