diff --git a/config/common.yml b/config/common.yml index 8614104f7..994799fc1 100644 --- a/config/common.yml +++ b/config/common.yml @@ -88,6 +88,7 @@ services: PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\Query\GetPayPalGetUserIdTokenQuery: 'PrestaShop\Module\PrestashopCheckout\PayPal\OAuth\Query\GetPayPalGetUserIdTokenQueryHandler' PrestaShop\Module\PrestashopCheckout\PayPal\Order\Command\SavePayPalOrderCommand: 'PrestaShop\Module\PrestashopCheckout\PayPal\Order\CommandHandler\SavePayPalOrderCommandHandler' PrestaShop\Module\PrestashopCheckout\PayPal\GooglePay\Query\GetGooglePayTransactionInfoQuery: 'PrestaShop\Module\PrestashopCheckout\PayPal\GooglePay\Query\GetGooglePayTransactionInfoQueryHandler' + PrestaShop\Module\PrestashopCheckout\Cart\Query\GetCartForPaymentQuery: 'PrestaShop\Module\PrestashopCheckout\Cart\Query\GetCartForPaymentQueryHandler' PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherFactory: class: 'PrestaShop\Module\PrestashopCheckout\Event\SymfonyEventDispatcherFactory' diff --git a/config/query-handlers.yml b/config/query-handlers.yml index 8997caaa4..a9af35efc 100644 --- a/config/query-handlers.yml +++ b/config/query-handlers.yml @@ -86,3 +86,7 @@ services: public: true arguments: - '@PrestaShop\Module\PrestashopCheckout\PayPal\GooglePay\Builder\GooglePayTransactionInfoBuilder' + + PrestaShop\Module\PrestashopCheckout\Cart\Query\GetCartForPaymentQueryHandler: + class: 'PrestaShop\Module\PrestashopCheckout\Cart\Query\GetCartForPaymentQueryHandler' + public: true diff --git a/src/Cart/Query/GetCartForPaymentQuery.php b/src/Cart/Query/GetCartForPaymentQuery.php new file mode 100644 index 000000000..9f92f11d3 --- /dev/null +++ b/src/Cart/Query/GetCartForPaymentQuery.php @@ -0,0 +1,44 @@ + + * @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\Query; + +use PrestaShop\Module\PrestashopCheckout\Cart\ValueObject\CartId; + +class GetCartForPaymentQuery +{ + /** + * @var CartId + */ + private $cartId; + + public function __construct(CartId $cartId) + { + $this->cartId = $cartId; + } + + /** + * @return CartId + */ + public function getCartId() + { + return $this->cartId; + } +} diff --git a/src/Cart/Query/GetCartForPaymentQueryHandler.php b/src/Cart/Query/GetCartForPaymentQueryHandler.php new file mode 100644 index 000000000..2056e9e1f --- /dev/null +++ b/src/Cart/Query/GetCartForPaymentQueryHandler.php @@ -0,0 +1,69 @@ + + * @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\Query; + +use Cart; +use Currency; +use Customer; + +class GetCartForPaymentQueryHandler +{ + public function handle(GetCartForPaymentQuery $query) + { + $cart = new Cart($query->getCartId()->getValue()); + $currency = new Currency($cart->id_currency); + $language = $cart->getAssociatedLanguage(); + + return new GetCartForPaymentQueryResult($cart, $cart->getProducts()); + } + + /** + * @param Cart $cart + * + * @return CartAddress[] + */ + private function getAddresses(Cart $cart) + { + $customer = new Customer($cart->id_customer); + $cartAddresses = []; + + foreach ($customer->getAddresses($cart->getAssociatedLanguage()->getId()) as $data) { + $addressId = (int) $data['id_address']; + $cartAddresses[$addressId] = $this->buildCartAddress($addressId, $cart); + } + + // Add addresses already assigned to cart if absent (in case they are deleted) + if (0 !== (int) $cart->id_address_delivery && !isset($cartAddresses[$cart->id_address_delivery])) { + $cartAddresses[$cart->id_address_delivery] = $this->buildCartAddress( + $cart->id_address_delivery, + $cart + ); + } + if (0 !== (int) $cart->id_address_invoice && !isset($cartAddresses[$cart->id_address_invoice])) { + $cartAddresses[$cart->id_address_invoice] = $this->buildCartAddress( + $cart->id_address_invoice, + $cart + ); + } + + return array_values($cartAddresses); + } +} diff --git a/src/Cart/Query/GetCartForPaymentQueryResult.php b/src/Cart/Query/GetCartForPaymentQueryResult.php new file mode 100644 index 000000000..e4b0b01dc --- /dev/null +++ b/src/Cart/Query/GetCartForPaymentQueryResult.php @@ -0,0 +1,83 @@ + + * @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\Query; + +use Cart; + +class GetCartForPaymentQueryResult +{ + /** + * @var Cart + */ + private $cart; + /** + * @var array + */ + private $products; + /** + * @var array + */ + private $deliveryAddress; + /** + * @var array + */ + private $invoiceAddress; + + public function __construct(Cart $cart, $products, $deliveryAddress, $invoiceAddress) + { + $this->cart = $cart; + $this->products = $products; + $this->deliveryAddress = $deliveryAddress; + $this->invoiceAddress = $invoiceAddress; + } + + /** + * @return Cart + */ + public function getCart() + { + return $this->cart; + } + + /** + * @return array + */ + public function getProducts() + { + return $this->products; + } + + /** + * @return array + */ + public function getDeliveryAddress() + { + return $this->deliveryAddress; + } + + /** + * @return array + */ + public function getInvoiceAddress() + { + return $this->invoiceAddress; + } +}