From 39b7ea40a37526df91ab8364fa8dd71f5f9a57b7 Mon Sep 17 00:00:00 2001 From: Matt75 <5262628+Matt75@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:11:47 +0100 Subject: [PATCH] Interfaces --- src/Cart/CartInterface.php | 45 ++++++++++ src/Cart/CartRepositoryInterface.php | 36 ++++++++ src/DTO/Orders/CreatePayPalOrderRequest.php | 85 ++++++++++++++++++ src/DTO/Orders/CreatePayPalOrderResponse.php | 34 ++++++++ .../Command/CreatePayPalOrderCommand.php | 62 +++++++++++++ .../CreatePayPalOrderCommandHandler.php | 87 +++++++++++++++++++ ...eatePayPalOrderPayloadBuilderInterface.php | 35 ++++++++ .../Order/PayPalOrderHttpClientInterface.php | 37 ++++++++ 8 files changed, 421 insertions(+) create mode 100644 src/Cart/CartInterface.php create mode 100644 src/Cart/CartRepositoryInterface.php create mode 100644 src/DTO/Orders/CreatePayPalOrderRequest.php create mode 100644 src/DTO/Orders/CreatePayPalOrderResponse.php create mode 100644 src/PayPal/Order/Command/CreatePayPalOrderCommand.php create mode 100644 src/PayPal/Order/CommandHandler/CreatePayPalOrderCommandHandler.php create mode 100644 src/PayPal/Order/CreatePayPalOrderPayloadBuilderInterface.php create mode 100644 src/PayPal/Order/PayPalOrderHttpClientInterface.php diff --git a/src/Cart/CartInterface.php b/src/Cart/CartInterface.php new file mode 100644 index 000000000..1b1c81a31 --- /dev/null +++ b/src/Cart/CartInterface.php @@ -0,0 +1,45 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\Cart; + +use PrestaShop\Module\PrestashopCheckout\DTO\Orders\PurchaseUnitRequest; + +interface CartInterface +{ + /** + * @return int + */ + public function getId(); + + /** + * @return string + */ + public function getCurrencyCode(); + + public function getCustomer(); + + public function getBillingAddress(); + + /** + * @return PurchaseUnitRequest[] + */ + public function getPurchaseUnits(); +} diff --git a/src/Cart/CartRepositoryInterface.php b/src/Cart/CartRepositoryInterface.php new file mode 100644 index 000000000..f38d7e206 --- /dev/null +++ b/src/Cart/CartRepositoryInterface.php @@ -0,0 +1,36 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\Cart; + +use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; +use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId; + +interface CartRepositoryInterface +{ + /** + * @param CartId $cartId + * + * @return CartInterface + * + * @throws CartNotFoundException + */ + public function getCartById(CartId $cartId); +} diff --git a/src/DTO/Orders/CreatePayPalOrderRequest.php b/src/DTO/Orders/CreatePayPalOrderRequest.php new file mode 100644 index 000000000..a6de5c3e6 --- /dev/null +++ b/src/DTO/Orders/CreatePayPalOrderRequest.php @@ -0,0 +1,85 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\DTO\Orders; + +class CreatePayPalOrderRequest +{ + /** + * @var string + */ + private $intent; + /** + * @var PurchaseUnitRequest[] + */ + private $purchase_units; + /** + * @var PaymentSourceRequest + */ + private $payment_source; + + /** + * @return string + */ + public function getIntent() + { + return $this->intent; + } + + /** + * @param string $intent + */ + public function setIntent($intent) + { + $this->intent = $intent; + } + + /** + * @return PurchaseUnitRequest[] + */ + public function getPurchaseUnits() + { + return $this->purchase_units; + } + + /** + * @param PurchaseUnitRequest[] $purchase_units + */ + public function setPurchaseUnits(array $purchase_units) + { + $this->purchase_units = $purchase_units; + } + + /** + * @return PaymentSourceRequest + */ + public function getPaymentSource() + { + return $this->payment_source; + } + + /** + * @param PaymentSourceRequest $payment_source + */ + public function setPaymentSource(PaymentSourceRequest $payment_source) + { + $this->payment_source = $payment_source; + } +} diff --git a/src/DTO/Orders/CreatePayPalOrderResponse.php b/src/DTO/Orders/CreatePayPalOrderResponse.php new file mode 100644 index 000000000..ab8d77025 --- /dev/null +++ b/src/DTO/Orders/CreatePayPalOrderResponse.php @@ -0,0 +1,34 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\DTO\Orders; + +class CreatePayPalOrderResponse +{ + public function getId() + { + return ''; + } + + public function toArray() + { + return []; + } +} diff --git a/src/PayPal/Order/Command/CreatePayPalOrderCommand.php b/src/PayPal/Order/Command/CreatePayPalOrderCommand.php new file mode 100644 index 000000000..b33c389f5 --- /dev/null +++ b/src/PayPal/Order/Command/CreatePayPalOrderCommand.php @@ -0,0 +1,62 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command; + +use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId; + +class CreatePayPalOrderCommand +{ + /** + * @var CartId + */ + private $cartId; + + /** + * @var string + */ + private $fundingSource; + + /** + * @param CartId $cartId + * @param string $fundingSource + */ + public function __construct(CartId $cartId, $fundingSource) + { + $this->cartId = $cartId; + $this->fundingSource = $fundingSource; + } + + /** + * @return CartId + */ + public function getCartId() + { + return $this->cartId; + } + + /** + * @return string + */ + public function getFundingSource() + { + return $this->fundingSource; + } +} diff --git a/src/PayPal/Order/CommandHandler/CreatePayPalOrderCommandHandler.php b/src/PayPal/Order/CommandHandler/CreatePayPalOrderCommandHandler.php new file mode 100644 index 000000000..506aa89f6 --- /dev/null +++ b/src/PayPal/Order/CommandHandler/CreatePayPalOrderCommandHandler.php @@ -0,0 +1,87 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order\CommandHandler; + +use PrestaShop\Module\PrestashopCheckout\Cart\CartRepositoryInterface; +use PrestaShop\Module\PrestashopCheckout\Cart\Exception\CartNotFoundException; +use PrestaShop\Module\PrestashopCheckout\Event\EventDispatcherInterface; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\CreatePayPalOrderCommand; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\CreatePayPalOrderPayloadBuilderInterface; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Event\PayPalOrderCreatedEvent; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Exception\PayPalOrderException; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\PayPalOrderHttpClientInterface; + +class CreatePayPalOrderCommandHandler +{ + /** + * @var CartRepositoryInterface + */ + private $cartRepository; + + /** + * @var CreatePayPalOrderPayloadBuilderInterface + */ + private $createPayPalOrderPayloadBuilder; + + /** + * @var PayPalOrderHttpClientInterface + */ + private $orderHttpClient; + + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + + /** + * @param CartRepositoryInterface $cartRepository + * @param CreatePayPalOrderPayloadBuilderInterface $createPayPalOrderPayloadBuilder + * @param PayPalOrderHttpClientInterface $orderHttpClient + * @param EventDispatcherInterface $eventDispatcher + */ + public function __construct( + CartRepositoryInterface $cartRepository, + CreatePayPalOrderPayloadBuilderInterface $createPayPalOrderPayloadBuilder, + PayPalOrderHttpClientInterface $orderHttpClient, + EventDispatcherInterface $eventDispatcher + ) { + $this->cartRepository = $cartRepository; + $this->createPayPalOrderPayloadBuilder = $createPayPalOrderPayloadBuilder; + $this->orderHttpClient = $orderHttpClient; + $this->eventDispatcher = $eventDispatcher; + } + + /** + * @param CreatePayPalOrderCommand $command + * + * @return void + * + * @throws PayPalOrderException + * @throws CartNotFoundException + */ + public function handle(CreatePayPalOrderCommand $command) + { + $cart = $this->cartRepository->getCartById($command->getCartId()); + $payload = $this->createPayPalOrderPayloadBuilder->build($cart, $command->getFundingSource()); + $order = $this->orderHttpClient->createOrder($payload); + $this->eventDispatcher->dispatch(new PayPalOrderCreatedEvent($order->getId(), $order->toArray())); + } +} diff --git a/src/PayPal/Order/CreatePayPalOrderPayloadBuilderInterface.php b/src/PayPal/Order/CreatePayPalOrderPayloadBuilderInterface.php new file mode 100644 index 000000000..d83ff80ba --- /dev/null +++ b/src/PayPal/Order/CreatePayPalOrderPayloadBuilderInterface.php @@ -0,0 +1,35 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order; + +use PrestaShop\Module\PrestashopCheckout\Cart\CartInterface; +use PrestaShop\Module\PrestashopCheckout\DTO\Orders\CreatePayPalOrderRequest; + +interface CreatePayPalOrderPayloadBuilderInterface +{ + /** + * @param CartInterface $cart + * @param string $fundingSource + * + * @return CreatePayPalOrderRequest + */ + public function build(CartInterface $cart, $fundingSource); +} diff --git a/src/PayPal/Order/PayPalOrderHttpClientInterface.php b/src/PayPal/Order/PayPalOrderHttpClientInterface.php new file mode 100644 index 000000000..372c98b6b --- /dev/null +++ b/src/PayPal/Order/PayPalOrderHttpClientInterface.php @@ -0,0 +1,37 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + +namespace PrestaShop\Module\PrestashopCheckout\PayPal\Order; + +use PrestaShop\Module\PrestashopCheckout\DTO\Orders\CreatePayPalOrderRequest; +use PrestaShop\Module\PrestashopCheckout\DTO\Orders\CreatePayPalOrderResponse; +use PrestaShop\Module\PrestashopCheckout\PayPal\Order\Exception\PayPalOrderException; + +interface PayPalOrderHttpClientInterface +{ + /** + * @param CreatePayPalOrderRequest $payload + * + * @return CreatePayPalOrderResponse + * + * @throws PayPalOrderException + */ + public function createOrder(CreatePayPalOrderRequest $payload); +}