From cdee459da859e23efa68026fe54d8c0dd74026b6 Mon Sep 17 00:00:00 2001 From: mandan2 Date: Mon, 25 Sep 2023 17:47:28 +0300 Subject: [PATCH 1/4] PIPRES-261: Get carrier price to create subscription --- src/Handler/Order/OrderCreationHandler.php | 18 +- src/Repository/AddressRepository.php | 9 +- src/Repository/AddressRepositoryInterface.php | 1 + src/Repository/CarrierRepository.php | 5 + src/Repository/CarrierRepositoryInterface.php | 1 + src/Repository/CountryRepository.php | 2 +- src/Repository/CountryRepositoryInterface.php | 7 + src/ServiceProvider/BaseServiceProvider.php | 3 + .../Symfony/SubscriptionFAQController.php | 3 +- .../CouldNotProvideCarrierDeliveryPrice.php | 62 +++++++ subscription/Exception/ExceptionCode.php | 7 + .../Factory/CreateSubscriptionDataFactory.php | 45 +++-- .../Handler/SubscriptionCreationHandler.php | 5 +- .../Provider/CarrierDeliveryPriceProvider.php | 122 ++++++++++++++ tests/Integration/Factory/CarrierFactory.php | 8 + tests/Integration/Factory/ProductFactory.php | 26 +++ .../CarrierDeliveryPriceProviderTest.php | 159 ++++++++++++++++++ 17 files changed, 462 insertions(+), 21 deletions(-) create mode 100644 src/Repository/CountryRepositoryInterface.php create mode 100644 subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php create mode 100644 subscription/Provider/CarrierDeliveryPriceProvider.php create mode 100644 tests/Integration/Factory/ProductFactory.php create mode 100644 tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php diff --git a/src/Handler/Order/OrderCreationHandler.php b/src/Handler/Order/OrderCreationHandler.php index e09f39690..827c1d995 100644 --- a/src/Handler/Order/OrderCreationHandler.php +++ b/src/Handler/Order/OrderCreationHandler.php @@ -49,6 +49,7 @@ use Mollie\DTO\PaymentData; use Mollie\Exception\FailedToProvidePaymentFeeException; use Mollie\Exception\OrderCreationException; +use Mollie\Logger\PrestaLoggerInterface; use Mollie\Provider\PaymentFeeProviderInterface; use Mollie\Repository\PaymentMethodRepositoryInterface; use Mollie\Service\OrderStatusService; @@ -85,6 +86,8 @@ class OrderCreationHandler private $subscriptionOrder; /** @var PaymentFeeProviderInterface */ private $paymentFeeProvider; + /** @var PrestaLoggerInterface */ + private $logger; public function __construct( Mollie $module, @@ -94,7 +97,8 @@ public function __construct( OrderStatusService $orderStatusService, SubscriptionCreationHandler $recurringOrderCreation, SubscriptionOrderValidator $subscriptionOrder, - PaymentFeeProviderInterface $paymentFeeProvider + PaymentFeeProviderInterface $paymentFeeProvider, + PrestaLoggerInterface $logger ) { $this->module = $module; $this->paymentMethodRepository = $paymentMethodRepository; @@ -104,6 +108,7 @@ public function __construct( $this->recurringOrderCreation = $recurringOrderCreation; $this->subscriptionOrder = $subscriptionOrder; $this->paymentFeeProvider = $paymentFeeProvider; + $this->logger = $logger; } /** @@ -286,6 +291,15 @@ private function createRecurringOrderEntity(Order $order, string $method): void return; } - $this->recurringOrderCreation->handle($order, $method); + try { + $this->recurringOrderCreation->handle($order, $method); + } catch (\Throwable $exception) { + $this->logger->error( + 'Failed to create recurring order', + [ + 'Exception message' => $exception->getMessage(), + ] + ); + } } } diff --git a/src/Repository/AddressRepository.php b/src/Repository/AddressRepository.php index 867c90f2d..06696a498 100644 --- a/src/Repository/AddressRepository.php +++ b/src/Repository/AddressRepository.php @@ -2,12 +2,15 @@ namespace Mollie\Repository; -use Address; - class AddressRepository extends AbstractRepository implements AddressRepositoryInterface { public function __construct() { - parent::__construct(Address::class); + parent::__construct(\Address::class); + } + + public function getZoneById(int $id_address_delivery): int + { + return \Address::getZoneById($id_address_delivery); } } diff --git a/src/Repository/AddressRepositoryInterface.php b/src/Repository/AddressRepositoryInterface.php index a25dd6ce4..cab93f85f 100644 --- a/src/Repository/AddressRepositoryInterface.php +++ b/src/Repository/AddressRepositoryInterface.php @@ -4,4 +4,5 @@ interface AddressRepositoryInterface extends ReadOnlyRepositoryInterface { + public function getZoneById(int $id_address_delivery): int; } diff --git a/src/Repository/CarrierRepository.php b/src/Repository/CarrierRepository.php index c35fa9f05..6dc12293d 100644 --- a/src/Repository/CarrierRepository.php +++ b/src/Repository/CarrierRepository.php @@ -8,4 +8,9 @@ public function __construct() { parent::__construct(\Carrier::class); } + + public function getCarriersForOrder(int $id_zone, array $groups = null, \Cart $cart = null, &$error = []): array + { + return \Carrier::getCarriersForOrder($id_zone, $groups, $cart, $error); + } } diff --git a/src/Repository/CarrierRepositoryInterface.php b/src/Repository/CarrierRepositoryInterface.php index e53de599d..a53daf595 100644 --- a/src/Repository/CarrierRepositoryInterface.php +++ b/src/Repository/CarrierRepositoryInterface.php @@ -4,4 +4,5 @@ interface CarrierRepositoryInterface extends ReadOnlyRepositoryInterface { + public function getCarriersForOrder(int $id_zone, array $groups = null, \Cart $cart = null, &$error = []): array; } diff --git a/src/Repository/CountryRepository.php b/src/Repository/CountryRepository.php index f7615e59a..bf6313d8b 100644 --- a/src/Repository/CountryRepository.php +++ b/src/Repository/CountryRepository.php @@ -15,7 +15,7 @@ use Country; use Db; -final class CountryRepository extends AbstractRepository +final class CountryRepository extends AbstractRepository implements CountryRepositoryInterface { public function __construct() { diff --git a/src/Repository/CountryRepositoryInterface.php b/src/Repository/CountryRepositoryInterface.php new file mode 100644 index 000000000..446b142a6 --- /dev/null +++ b/src/Repository/CountryRepositoryInterface.php @@ -0,0 +1,7 @@ +addService($container, RetryHandlerInterface::class, $container->get(RetryHandler::class)); + $this->addService($container, CountryRepositoryInterface::class, $container->get(CountryRepository::class)); $this->addService($container, PaymentMethodRepositoryInterface::class, $container->get(PaymentMethodRepository::class)); $this->addService($container, GenderRepositoryInterface::class, $container->get(GenderRepository::class)); $this->addService($container, MolCustomerRepository::class, MolCustomerRepository::class) diff --git a/subscription/Controller/Symfony/SubscriptionFAQController.php b/subscription/Controller/Symfony/SubscriptionFAQController.php index a99fdce50..9e984b32c 100644 --- a/subscription/Controller/Symfony/SubscriptionFAQController.php +++ b/subscription/Controller/Symfony/SubscriptionFAQController.php @@ -27,7 +27,8 @@ public function indexAction() 'carrierInformation1' => $this->module->l('Make sure to select default carrier for recurring orders in advanced settings.', self::FILE_NAME), 'carrierInformation2' => $this->module->l('Carrier should cover all supported shop regions.', self::FILE_NAME), 'carrierInformation3' => $this->module->l('Carrier cannot be changed after first subscription order is placed.', self::FILE_NAME), - 'carrierInformation4' => $this->module->l('Selected carrier pricing/weight settings or carrier selection in Mollie should not change. If they do, subscription orders must be cancelled.', self::FILE_NAME), + // TODO add text to not use gift wrapping for subscription products + 'carrierInformation4' => $this->module->l('Selected carrier pricing/weight settings or carrier selection in Mollie should not change. If they do, subscription orders must be cancelled and carrier re-selected in module settings.', self::FILE_NAME), 'cartRuleTitle' => $this->module->l('Cart rules', self::FILE_NAME), 'cartRule' => $this->module->l('A customer can\'t add subscription items with different recurring periods to the same shopping cart.', self::FILE_NAME), 'cartRule2' => $this->module->l('Do not use cart rules with subscription products as this will cause errors due to incorrect pricing.', self::FILE_NAME), diff --git a/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php b/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php new file mode 100644 index 000000000..c6f20a9e4 --- /dev/null +++ b/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php @@ -0,0 +1,62 @@ +customerRepository = $customerRepository; $this->subscriptionInterval = $subscriptionInterval; @@ -57,10 +63,16 @@ public function __construct( $this->currencyAdapter = $currencyAdapter; $this->combination = $combination; $this->methodRepository = $methodRepository; - $this->link = $link; $this->module = $module; + $this->context = $context; + $this->carrierDeliveryPriceProvider = $carrierDeliveryPriceProvider; } + /** + * @throws \PrestaShopException + * @throws CouldNotProvideCarrierDeliveryPrice + * @throws SubscriptionIntervalException + */ public function build(Order $order, array $subscriptionProduct): SubscriptionDataDTO { $customer = $order->getCustomer(); @@ -74,13 +86,20 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat $currency = $this->currencyAdapter->getById((int) $order->id_currency); $description = $this->subscriptionDescription->getSubscriptionDescription($order); - $orderTotal = (float) $subscriptionProduct['total_price_tax_incl'] - + (float) $order->total_wrapping_tax_incl - + (float) $order->total_shipping_tax_incl; + try { + $deliveryPrice = $this->carrierDeliveryPriceProvider->getPrice( + (int) $order->id_address_delivery, + (int) $order->id_cart, + (int) $order->id_customer, + $subscriptionProduct + ); + } catch (CouldNotProvideCarrierDeliveryPrice $exception) { + // TODO throw generic error when new logger will be implemented + throw $exception; + } + + $orderTotal = (float) $subscriptionProduct['total_price_tax_incl'] + $deliveryPrice; - /** - * NOTE: we will only send product price as total for subscriptions - */ $orderAmount = new Amount($orderTotal, $currency->iso_code); $subscriptionData = new SubscriptionDataDTO( $molCustomer->customer_id, @@ -89,7 +108,7 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat $description ); - $subscriptionData->setWebhookUrl($this->link->getModuleLink( + $subscriptionData->setWebhookUrl($this->context->getModuleLink( 'mollie', 'subscriptionWebhook' )); diff --git a/subscription/Handler/SubscriptionCreationHandler.php b/subscription/Handler/SubscriptionCreationHandler.php index 89c5c220a..9f5e027cf 100644 --- a/subscription/Handler/SubscriptionCreationHandler.php +++ b/subscription/Handler/SubscriptionCreationHandler.php @@ -38,7 +38,10 @@ public function __construct( $this->subscriptionProductValidator = $subscriptionProductValidator; } - public function handle(Order $order, string $method) + /** + * @throws \Throwable + */ + public function handle(Order $order, string $method): void { $products = $order->getCartProducts(); $subscriptionProduct = []; diff --git a/subscription/Provider/CarrierDeliveryPriceProvider.php b/subscription/Provider/CarrierDeliveryPriceProvider.php new file mode 100644 index 000000000..f22f794e8 --- /dev/null +++ b/subscription/Provider/CarrierDeliveryPriceProvider.php @@ -0,0 +1,122 @@ +configuration = $configuration; + $this->carrierRepository = $carrierRepository; + $this->addressRepository = $addressRepository; + $this->customerRepository = $customerRepository; + $this->cartRepository = $cartRepository; + $this->countryRepository = $countryRepository; + } + + /** + * @throws CouldNotProvideCarrierDeliveryPrice + */ + public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, array $subscriptionProduct): float + { + $subscriptionCarrierId = (int) $this->configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); + + /** @var \Carrier|null $carrier */ + $carrier = $this->carrierRepository->findOneBy([ + 'id_carrier' => $subscriptionCarrierId, + ]); + + if (!$carrier) { + throw CouldNotProvideCarrierDeliveryPrice::failedToFindSelectedCarrierForSubscriptionOrder(); + } + + /** @var \Cart|null $cart */ + $cart = $this->cartRepository->findOneBy([ + 'id_cart' => $cartId, + ]); + + if (!$cart) { + throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCart(); + } + + /** @var \Customer|null $customer */ + $customer = $this->customerRepository->findOneBy([ + 'id_customer' => $customerId, + ]); + + if (!$customer) { + throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCustomer(); + } + + $getAvailableOrderCarriers = $this->carrierRepository->getCarriersForOrder( + $this->addressRepository->getZoneById($addressDeliveryId), + $customer->getGroups(), + $cart + ); + + if (!in_array($subscriptionCarrierId, array_column($getAvailableOrderCarriers, 'id_carrier'), false)) { + throw CouldNotProvideCarrierDeliveryPrice::failedToApplySelectedCarrierForSubscriptionOrder(); + } + + /** @var \Address|bool $address */ + $address = $this->addressRepository->findOneBy([ + 'id_address' => $addressDeliveryId, + ]); + + if (!$address) { + throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryAddress(); + } + + /** @var \Country|bool $country */ + $country = $this->countryRepository->findOneBy([ + 'id_country' => $address->id_country, + ]); + + if (!$country) { + throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryCountry(); + } + + /** @var float|bool $deliveryPrice */ + $deliveryPrice = $cart->getPackageShippingCost( + $subscriptionCarrierId, + true, + $country, + [$subscriptionProduct], + $this->addressRepository->getZoneById($addressDeliveryId) + ); + + if (is_bool($deliveryPrice) && !$deliveryPrice) { + throw CouldNotProvideCarrierDeliveryPrice::failedToGetSelectedCarrierPriceForSubscriptionOrder(); + } + + return (float) $deliveryPrice; + } +} diff --git a/tests/Integration/Factory/CarrierFactory.php b/tests/Integration/Factory/CarrierFactory.php index e77ce0852..ab6aa3ccb 100644 --- a/tests/Integration/Factory/CarrierFactory.php +++ b/tests/Integration/Factory/CarrierFactory.php @@ -37,6 +37,10 @@ public static function create(array $data = []) $prices = []; foreach ($zones as $zone) { + if (in_array($zone['id_zone'], $data['id_zones_to_delete'] ?? [], false)) { + continue; + } + $carrier->addZone($zone['id_zone']); $prices[] = [ @@ -50,6 +54,10 @@ public static function create(array $data = []) // enable all zones $carrier->addDeliveryPrice($prices); + $allGroups = \Group::getGroups(\Context::getContext()->language->id); + + $carrier->setGroups(array_column($allGroups, 'id_group')); + return $carrier; } } diff --git a/tests/Integration/Factory/ProductFactory.php b/tests/Integration/Factory/ProductFactory.php new file mode 100644 index 000000000..e44c0c4f9 --- /dev/null +++ b/tests/Integration/Factory/ProductFactory.php @@ -0,0 +1,26 @@ +id_tax_rules_group = $data['id_tax_rules_group'] ?? 1; + $product->name = $data['name'] ?? 'test-name'; + $product->description_short = $data['description_short'] ?? 'test-description_short'; + $product->price = $data['price'] ?? 0; + $product->link_rewrite = \Tools::link_rewrite($product->name); + + $product->save(); + + \StockAvailable::setQuantity( + (int) $product->id, + 0, + isset($data['quantity']) ? (int) $data['quantity'] : 1 + ); + + return $product; + } +} diff --git a/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php b/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php new file mode 100644 index 000000000..bf8e5416c --- /dev/null +++ b/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php @@ -0,0 +1,159 @@ +getService(ConfigurationAdapter::class); + + $this->subscriptionOrderCarrierId = $configuration->get(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID); + + parent::setUp(); + } + + public function tearDown(): void + { + /** @var ConfigurationAdapter $configuration */ + $configuration = $this->getService(ConfigurationAdapter::class); + + $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $this->subscriptionOrderCarrierId); + + parent::tearDown(); + } + + public function testItSuccessfullyProvidesCarrierDeliveryPrice(): void + { + $address = AddressFactory::create(); + $carrier = CarrierFactory::create([ + 'price' => 999.00, + ]); + $cart = CartFactory::create([ + 'id_carrier' => $carrier->id, + ]); + + /** @var ConfigurationAdapter $configuration */ + $configuration = $this->getService(ConfigurationAdapter::class); + + $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $carrier->id); + + $targetProduct = ProductFactory::create([ + 'quantity' => 10, + ]); + $product1 = ProductFactory::create([ + 'quantity' => 10, + ]); + $product2 = ProductFactory::create([ + 'quantity' => 10, + ]); + + $cart->updateQty(2, $targetProduct->id); + + $targetProductArray = $cart->getProducts()[0]; + + $cart->updateQty(2, $product1->id); + $cart->updateQty(3, $product2->id); + + /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ + $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); + + $result = $carrierDeliveryPriceProvider->getPrice( + $address->id, + $cart->id, + $cart->id_customer, + $targetProductArray + ); + + $this->assertEquals(999.00, $result); + + $this->removeFactories([ + $carrier, + $address, + $cart, + $targetProduct, + $product1, + $product2, + ]); + } + + public function testItUnsuccessfullyProvidesCarrierDeliveryPriceCarrierIsOutOfZone(): void + { + $address = AddressFactory::create(); + $carrier = CarrierFactory::create([ + 'price' => 999.00, + 'id_zones_to_delete' => [ + $address::getZoneById($address->id), + ], + ]); + $cart = CartFactory::create([ + 'id_carrier' => $carrier->id, + ]); + + /** @var ConfigurationAdapter $configuration */ + $configuration = $this->getService(ConfigurationAdapter::class); + + $configuration->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, $carrier->id); + + $targetProduct = ProductFactory::create([ + 'quantity' => 10, + ]); + $product1 = ProductFactory::create([ + 'quantity' => 10, + ]); + $product2 = ProductFactory::create([ + 'quantity' => 10, + ]); + + $cart->updateQty(2, $targetProduct->id); + + $targetProductArray = $cart->getProducts()[0]; + + $cart->updateQty(2, $product1->id); + $cart->updateQty(3, $product2->id); + + $this->expectException(CouldNotProvideCarrierDeliveryPrice::class); + $this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER); + + /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ + $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); + + $carrierDeliveryPriceProvider->getPrice( + $address->id, + $cart->id, + $cart->id_customer, + $targetProductArray + ); + + $this->removeFactories([ + $carrier, + $address, + $cart, + $targetProduct, + $product1, + $product2, + ]); + } + + /** + * @param \ObjectModel[] $objects + */ + private function removeFactories(array $objects): void + { + foreach ($objects as $object) { + $object->delete(); + } + } +} From a0e5822290d1f143bd861b6be05b7f33afdcae24 Mon Sep 17 00:00:00 2001 From: mandan2 Date: Tue, 26 Sep 2023 10:32:29 +0300 Subject: [PATCH 2/4] updated subscription faq --- .../Symfony/SubscriptionFAQController.php | 3 ++- .../Subscription/subscriptions-faq.html.twig | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/subscription/Controller/Symfony/SubscriptionFAQController.php b/subscription/Controller/Symfony/SubscriptionFAQController.php index 9e984b32c..efbfee5de 100644 --- a/subscription/Controller/Symfony/SubscriptionFAQController.php +++ b/subscription/Controller/Symfony/SubscriptionFAQController.php @@ -27,11 +27,12 @@ public function indexAction() 'carrierInformation1' => $this->module->l('Make sure to select default carrier for recurring orders in advanced settings.', self::FILE_NAME), 'carrierInformation2' => $this->module->l('Carrier should cover all supported shop regions.', self::FILE_NAME), 'carrierInformation3' => $this->module->l('Carrier cannot be changed after first subscription order is placed.', self::FILE_NAME), - // TODO add text to not use gift wrapping for subscription products 'carrierInformation4' => $this->module->l('Selected carrier pricing/weight settings or carrier selection in Mollie should not change. If they do, subscription orders must be cancelled and carrier re-selected in module settings.', self::FILE_NAME), 'cartRuleTitle' => $this->module->l('Cart rules', self::FILE_NAME), 'cartRule' => $this->module->l('A customer can\'t add subscription items with different recurring periods to the same shopping cart.', self::FILE_NAME), 'cartRule2' => $this->module->l('Do not use cart rules with subscription products as this will cause errors due to incorrect pricing.', self::FILE_NAME), + 'giftWrappingTitle' => $this->module->l('Gift wrapping', self::FILE_NAME), + 'giftWrapping1' => $this->module->l('Gift wrapping feature is not supported for subscription orders.', self::FILE_NAME), 'subscriptionOrderLogicTitle' => $this->module->l('Recurring order creation', self::FILE_NAME), 'recurringOrderCreation' => $this->module->l('Mollie for Prestashop automatically creates a new order when the previous order is paid for.', self::FILE_NAME), 'recurringOrderPrice' => $this->module->l('Recurring orders always use the product price that was specified when the related subscription was created.', self::FILE_NAME), diff --git a/views/templates/admin/Subscription/subscriptions-faq.html.twig b/views/templates/admin/Subscription/subscriptions-faq.html.twig index 6e9dab85e..07bcc9659 100644 --- a/views/templates/admin/Subscription/subscriptions-faq.html.twig +++ b/views/templates/admin/Subscription/subscriptions-faq.html.twig @@ -92,6 +92,21 @@ +
+
+
+

+ info_outline {{ giftWrappingTitle }} +

+
+
+

{{giftWrapping1}}

+
+
+
+
+
+
From 0092f2b2c6e2b562c43a8637d26a557a5b880f5f Mon Sep 17 00:00:00 2001 From: mandan2 Date: Tue, 26 Sep 2023 13:45:46 +0300 Subject: [PATCH 3/4] renamed some services and added additional conditions for carrier retrieve --- src/Handler/Order/OrderCreationHandler.php | 1 + ...ovideSubscriptionCarrierDeliveryPrice.php} | 28 +++++++++---------- subscription/Exception/ExceptionCode.php | 16 +++++------ .../Factory/CreateSubscriptionDataFactory.php | 18 ++++++------ ...scriptionCarrierDeliveryPriceProvider.php} | 22 ++++++++------- ...ptionCarrierDeliveryPriceProviderTest.php} | 22 +++++++-------- 6 files changed, 54 insertions(+), 53 deletions(-) rename subscription/Exception/{CouldNotProvideCarrierDeliveryPrice.php => CouldNotProvideSubscriptionCarrierDeliveryPrice.php} (60%) rename subscription/Provider/{CarrierDeliveryPriceProvider.php => SubscriptionCarrierDeliveryPriceProvider.php} (78%) rename tests/Integration/Subscription/Provider/{CarrierDeliveryPriceProviderTest.php => SubscriptionCarrierDeliveryPriceProviderTest.php} (81%) diff --git a/src/Handler/Order/OrderCreationHandler.php b/src/Handler/Order/OrderCreationHandler.php index 827c1d995..7a3316cf4 100644 --- a/src/Handler/Order/OrderCreationHandler.php +++ b/src/Handler/Order/OrderCreationHandler.php @@ -298,6 +298,7 @@ private function createRecurringOrderEntity(Order $order, string $method): void 'Failed to create recurring order', [ 'Exception message' => $exception->getMessage(), + 'Exception code' => $exception->getCode(), ] ); } diff --git a/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php b/subscription/Exception/CouldNotProvideSubscriptionCarrierDeliveryPrice.php similarity index 60% rename from subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php rename to subscription/Exception/CouldNotProvideSubscriptionCarrierDeliveryPrice.php index c6f20a9e4..6c9709b7f 100644 --- a/subscription/Exception/CouldNotProvideCarrierDeliveryPrice.php +++ b/subscription/Exception/CouldNotProvideSubscriptionCarrierDeliveryPrice.php @@ -2,17 +2,17 @@ namespace Mollie\Subscription\Exception; -class CouldNotProvideCarrierDeliveryPrice extends MollieSubscriptionException +class CouldNotProvideSubscriptionCarrierDeliveryPrice extends MollieSubscriptionException { - public static function failedToFindSelectedCarrierForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice + public static function failedToFindSelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( - 'Failed to find selected carrier for subscription order', - ExceptionCode::ORDER_FAILED_TO_FIND_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER + 'Failed to find selected carrier', + ExceptionCode::ORDER_FAILED_TO_FIND_SELECTED_CARRIER ); } - public static function failedToFindOrderCart(): CouldNotProvideCarrierDeliveryPrice + public static function failedToFindOrderCart(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( 'Failed to find order cart', @@ -20,7 +20,7 @@ public static function failedToFindOrderCart(): CouldNotProvideCarrierDeliveryPr ); } - public static function failedToFindOrderCustomer(): CouldNotProvideCarrierDeliveryPrice + public static function failedToFindOrderCustomer(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( 'Failed to find order customer', @@ -28,15 +28,15 @@ public static function failedToFindOrderCustomer(): CouldNotProvideCarrierDelive ); } - public static function failedToApplySelectedCarrierForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice + public static function failedToApplySelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( - 'Failed to apply selected carrier for subscription order', - ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER + 'Failed to apply selected carrier', + ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER ); } - public static function failedToFindOrderDeliveryAddress(): CouldNotProvideCarrierDeliveryPrice + public static function failedToFindOrderDeliveryAddress(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( 'Failed to find order delivery address', @@ -44,7 +44,7 @@ public static function failedToFindOrderDeliveryAddress(): CouldNotProvideCarrie ); } - public static function failedToFindOrderDeliveryCountry(): CouldNotProvideCarrierDeliveryPrice + public static function failedToFindOrderDeliveryCountry(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( 'Failed to find order delivery country', @@ -52,11 +52,11 @@ public static function failedToFindOrderDeliveryCountry(): CouldNotProvideCarrie ); } - public static function failedToGetSelectedCarrierPriceForSubscriptionOrder(): CouldNotProvideCarrierDeliveryPrice + public static function failedToGetSelectedCarrierPrice(): CouldNotProvideSubscriptionCarrierDeliveryPrice { return new self( - 'Failed to get selected carrier price for subscription order', - ExceptionCode::ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE_FOR_SUBSCRIPTION_ORDER + 'Failed to get selected carrier price', + ExceptionCode::ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE ); } } diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index f20f4d025..f2a485c64 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -6,15 +6,13 @@ class ExceptionCode { //Order error codes starts from 1000 - public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001; - public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002; - public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER = 1003; - public const ORDER_FAILED_TO_FIND_ORDER_CART = 1004; - public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1005; - public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER = 1006; - public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1007; - public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1008; - public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE_FOR_SUBSCRIPTION_ORDER = 1009; + public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1001; + public const ORDER_FAILED_TO_FIND_ORDER_CART = 1002; + public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1003; + public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1004; + public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1005; + public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1006; + public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1007; //Cart error codes starts from 2000 diff --git a/subscription/Factory/CreateSubscriptionDataFactory.php b/subscription/Factory/CreateSubscriptionDataFactory.php index aa58a68f3..fbf63adb7 100644 --- a/subscription/Factory/CreateSubscriptionDataFactory.php +++ b/subscription/Factory/CreateSubscriptionDataFactory.php @@ -10,9 +10,9 @@ use Mollie\Repository\PaymentMethodRepositoryInterface; use Mollie\Subscription\DTO\CreateSubscriptionData as SubscriptionDataDTO; use Mollie\Subscription\DTO\Object\Amount; -use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice; +use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice; use Mollie\Subscription\Exception\SubscriptionIntervalException; -use Mollie\Subscription\Provider\CarrierDeliveryPriceProvider; +use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider; use Mollie\Subscription\Provider\SubscriptionDescriptionProvider; use Mollie\Subscription\Provider\SubscriptionIntervalProvider; use Mollie\Subscription\Repository\CombinationRepository; @@ -43,8 +43,8 @@ class CreateSubscriptionDataFactory private $module; /** @var Context */ private $context; - /** @var CarrierDeliveryPriceProvider */ - private $carrierDeliveryPriceProvider; + /** @var SubscriptionCarrierDeliveryPriceProvider */ + private $subscriptionCarrierDeliveryPriceProvider; public function __construct( MolCustomerRepository $customerRepository, @@ -55,7 +55,7 @@ public function __construct( PaymentMethodRepositoryInterface $methodRepository, Mollie $module, Context $context, - CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider + SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider ) { $this->customerRepository = $customerRepository; $this->subscriptionInterval = $subscriptionInterval; @@ -65,12 +65,12 @@ public function __construct( $this->methodRepository = $methodRepository; $this->module = $module; $this->context = $context; - $this->carrierDeliveryPriceProvider = $carrierDeliveryPriceProvider; + $this->subscriptionCarrierDeliveryPriceProvider = $subscriptionCarrierDeliveryPriceProvider; } /** * @throws \PrestaShopException - * @throws CouldNotProvideCarrierDeliveryPrice + * @throws CouldNotProvideSubscriptionCarrierDeliveryPrice * @throws SubscriptionIntervalException */ public function build(Order $order, array $subscriptionProduct): SubscriptionDataDTO @@ -87,13 +87,13 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat $description = $this->subscriptionDescription->getSubscriptionDescription($order); try { - $deliveryPrice = $this->carrierDeliveryPriceProvider->getPrice( + $deliveryPrice = $this->subscriptionCarrierDeliveryPriceProvider->getPrice( (int) $order->id_address_delivery, (int) $order->id_cart, (int) $order->id_customer, $subscriptionProduct ); - } catch (CouldNotProvideCarrierDeliveryPrice $exception) { + } catch (CouldNotProvideSubscriptionCarrierDeliveryPrice $exception) { // TODO throw generic error when new logger will be implemented throw $exception; } diff --git a/subscription/Provider/CarrierDeliveryPriceProvider.php b/subscription/Provider/SubscriptionCarrierDeliveryPriceProvider.php similarity index 78% rename from subscription/Provider/CarrierDeliveryPriceProvider.php rename to subscription/Provider/SubscriptionCarrierDeliveryPriceProvider.php index f22f794e8..39dd93fbf 100644 --- a/subscription/Provider/CarrierDeliveryPriceProvider.php +++ b/subscription/Provider/SubscriptionCarrierDeliveryPriceProvider.php @@ -9,9 +9,9 @@ use Mollie\Repository\CartRepositoryInterface; use Mollie\Repository\CountryRepositoryInterface; use Mollie\Repository\CustomerRepositoryInterface; -use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice; +use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice; -class CarrierDeliveryPriceProvider +class SubscriptionCarrierDeliveryPriceProvider { /** @var ConfigurationAdapter */ private $configuration; @@ -43,7 +43,7 @@ public function __construct( } /** - * @throws CouldNotProvideCarrierDeliveryPrice + * @throws CouldNotProvideSubscriptionCarrierDeliveryPrice */ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, array $subscriptionProduct): float { @@ -52,10 +52,12 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a /** @var \Carrier|null $carrier */ $carrier = $this->carrierRepository->findOneBy([ 'id_carrier' => $subscriptionCarrierId, + 'active' => 1, + 'deleted' => 0, ]); if (!$carrier) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindSelectedCarrierForSubscriptionOrder(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindSelectedCarrier(); } /** @var \Cart|null $cart */ @@ -64,7 +66,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ]); if (!$cart) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCart(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderCart(); } /** @var \Customer|null $customer */ @@ -73,7 +75,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ]); if (!$customer) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderCustomer(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderCustomer(); } $getAvailableOrderCarriers = $this->carrierRepository->getCarriersForOrder( @@ -83,7 +85,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ); if (!in_array($subscriptionCarrierId, array_column($getAvailableOrderCarriers, 'id_carrier'), false)) { - throw CouldNotProvideCarrierDeliveryPrice::failedToApplySelectedCarrierForSubscriptionOrder(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToApplySelectedCarrier(); } /** @var \Address|bool $address */ @@ -92,7 +94,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ]); if (!$address) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryAddress(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderDeliveryAddress(); } /** @var \Country|bool $country */ @@ -101,7 +103,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ]); if (!$country) { - throw CouldNotProvideCarrierDeliveryPrice::failedToFindOrderDeliveryCountry(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToFindOrderDeliveryCountry(); } /** @var float|bool $deliveryPrice */ @@ -114,7 +116,7 @@ public function getPrice(int $addressDeliveryId, int $cartId, int $customerId, a ); if (is_bool($deliveryPrice) && !$deliveryPrice) { - throw CouldNotProvideCarrierDeliveryPrice::failedToGetSelectedCarrierPriceForSubscriptionOrder(); + throw CouldNotProvideSubscriptionCarrierDeliveryPrice::failedToGetSelectedCarrierPrice(); } return (float) $deliveryPrice; diff --git a/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php b/tests/Integration/Subscription/Provider/SubscriptionCarrierDeliveryPriceProviderTest.php similarity index 81% rename from tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php rename to tests/Integration/Subscription/Provider/SubscriptionCarrierDeliveryPriceProviderTest.php index bf8e5416c..1a5a890dc 100644 --- a/tests/Integration/Subscription/Provider/CarrierDeliveryPriceProviderTest.php +++ b/tests/Integration/Subscription/Provider/SubscriptionCarrierDeliveryPriceProviderTest.php @@ -4,16 +4,16 @@ use Mollie\Adapter\ConfigurationAdapter; use Mollie\Config\Config; -use Mollie\Subscription\Exception\CouldNotProvideCarrierDeliveryPrice; +use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice; use Mollie\Subscription\Exception\ExceptionCode; -use Mollie\Subscription\Provider\CarrierDeliveryPriceProvider; +use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider; use Mollie\Tests\Integration\BaseTestCase; use Mollie\Tests\Integration\Factory\AddressFactory; use Mollie\Tests\Integration\Factory\CarrierFactory; use Mollie\Tests\Integration\Factory\CartFactory; use Mollie\Tests\Integration\Factory\ProductFactory; -class CarrierDeliveryPriceProviderTest extends BaseTestCase +class SubscriptionCarrierDeliveryPriceProviderTest extends BaseTestCase { public function setUp(): void { @@ -67,10 +67,10 @@ public function testItSuccessfullyProvidesCarrierDeliveryPrice(): void $cart->updateQty(2, $product1->id); $cart->updateQty(3, $product2->id); - /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ - $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); + /** @var SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider */ + $subscriptionCarrierDeliveryPriceProvider = $this->getService(SubscriptionCarrierDeliveryPriceProvider::class); - $result = $carrierDeliveryPriceProvider->getPrice( + $result = $subscriptionCarrierDeliveryPriceProvider->getPrice( $address->id, $cart->id, $cart->id_customer, @@ -124,13 +124,13 @@ public function testItUnsuccessfullyProvidesCarrierDeliveryPriceCarrierIsOutOfZo $cart->updateQty(2, $product1->id); $cart->updateQty(3, $product2->id); - $this->expectException(CouldNotProvideCarrierDeliveryPrice::class); - $this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER_FOR_SUBSCRIPTION_ORDER); + $this->expectException(CouldNotProvideSubscriptionCarrierDeliveryPrice::class); + $this->expectExceptionCode(ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER); - /** @var CarrierDeliveryPriceProvider $carrierDeliveryPriceProvider */ - $carrierDeliveryPriceProvider = $this->getService(CarrierDeliveryPriceProvider::class); + /** @var SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider */ + $subscriptionCarrierDeliveryPriceProvider = $this->getService(SubscriptionCarrierDeliveryPriceProvider::class); - $carrierDeliveryPriceProvider->getPrice( + $subscriptionCarrierDeliveryPriceProvider->getPrice( $address->id, $cart->id, $cart->id_customer, From 2525a477888b745f2399d79d13b683871e08e610 Mon Sep 17 00:00:00 2001 From: mandan2 Date: Tue, 26 Sep 2023 14:03:52 +0300 Subject: [PATCH 4/4] addedd back to exception codes --- subscription/Exception/ExceptionCode.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/subscription/Exception/ExceptionCode.php b/subscription/Exception/ExceptionCode.php index f2a485c64..32dd51f28 100644 --- a/subscription/Exception/ExceptionCode.php +++ b/subscription/Exception/ExceptionCode.php @@ -6,13 +6,15 @@ class ExceptionCode { //Order error codes starts from 1000 - public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1001; - public const ORDER_FAILED_TO_FIND_ORDER_CART = 1002; - public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1003; - public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1004; - public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1005; - public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1006; - public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1007; + public const ORDER_FAILED_TO_CREATE_ORDER_PAYMENT_FEE = 1001; + public const ORDER_FAILED_TO_UPDATE_ORDER_TOTAL_WITH_PAYMENT_FEE = 1002; + public const ORDER_FAILED_TO_FIND_SELECTED_CARRIER = 1003; + public const ORDER_FAILED_TO_FIND_ORDER_CART = 1004; + public const ORDER_FAILED_TO_FIND_ORDER_CUSTOMER = 1005; + public const ORDER_FAILED_TO_APPLY_SELECTED_CARRIER = 1006; + public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS = 1007; + public const ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY = 1008; + public const ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE = 1009; //Cart error codes starts from 2000