Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIPRES-261: Get carrier price to create subscription #817

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Handler/Order/OrderCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +86,8 @@ class OrderCreationHandler
private $subscriptionOrder;
/** @var PaymentFeeProviderInterface */
private $paymentFeeProvider;
/** @var PrestaLoggerInterface */
private $logger;

public function __construct(
Mollie $module,
Expand All @@ -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;
Expand All @@ -104,6 +108,7 @@ public function __construct(
$this->recurringOrderCreation = $recurringOrderCreation;
$this->subscriptionOrder = $subscriptionOrder;
$this->paymentFeeProvider = $paymentFeeProvider;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -286,6 +291,16 @@ 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(),
'Exception code' => $exception->getCode(),
]
);
}
}
}
9 changes: 6 additions & 3 deletions src/Repository/AddressRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Repository/AddressRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

interface AddressRepositoryInterface extends ReadOnlyRepositoryInterface
{
public function getZoneById(int $id_address_delivery): int;
}
5 changes: 5 additions & 0 deletions src/Repository/CarrierRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions src/Repository/CarrierRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

interface CarrierRepositoryInterface extends ReadOnlyRepositoryInterface
{
public function getCarriersForOrder(int $id_zone, array $groups = null, \Cart $cart = null, &$error = []): array;
}
2 changes: 1 addition & 1 deletion src/Repository/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Country;
use Db;

final class CountryRepository extends AbstractRepository
final class CountryRepository extends AbstractRepository implements CountryRepositoryInterface
{
public function __construct()
{
Expand Down
7 changes: 7 additions & 0 deletions src/Repository/CountryRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Mollie\Repository;

interface CountryRepositoryInterface extends ReadOnlyRepositoryInterface
{
}
3 changes: 3 additions & 0 deletions src/ServiceProvider/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
use Mollie\Repository\CartRepositoryInterface;
use Mollie\Repository\CartRuleRepository;
use Mollie\Repository\CartRuleRepositoryInterface;
use Mollie\Repository\CountryRepository;
use Mollie\Repository\CountryRepositoryInterface;
use Mollie\Repository\CurrencyRepository;
use Mollie\Repository\CurrencyRepositoryInterface;
use Mollie\Repository\CustomerRepository;
Expand Down Expand Up @@ -133,6 +135,7 @@ public function register(Container $container)

$this->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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +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),
'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),
'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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Mollie\Subscription\Exception;

class CouldNotProvideSubscriptionCarrierDeliveryPrice extends MollieSubscriptionException
{
public static function failedToFindSelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self instead of full name

{
return new self(
'Failed to find selected carrier',
ExceptionCode::ORDER_FAILED_TO_FIND_SELECTED_CARRIER
);
}

public static function failedToFindOrderCart(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order cart',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_CART
);
}

public static function failedToFindOrderCustomer(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order customer',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_CUSTOMER
);
}

public static function failedToApplySelectedCarrier(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to apply selected carrier',
ExceptionCode::ORDER_FAILED_TO_APPLY_SELECTED_CARRIER
);
}

public static function failedToFindOrderDeliveryAddress(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order delivery address',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_DELIVERY_ADDRESS
);
}

public static function failedToFindOrderDeliveryCountry(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to find order delivery country',
ExceptionCode::ORDER_FAILED_TO_FIND_ORDER_DELIVERY_COUNTRY
);
}

public static function failedToGetSelectedCarrierPrice(): CouldNotProvideSubscriptionCarrierDeliveryPrice
{
return new self(
'Failed to get selected carrier price',
ExceptionCode::ORDER_FAILED_TO_GET_SELECTED_CARRIER_PRICE
);
}
}
7 changes: 7 additions & 0 deletions subscription/Exception/ExceptionCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class ExceptionCode

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

Expand Down
45 changes: 32 additions & 13 deletions subscription/Factory/CreateSubscriptionDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Mollie\Subscription\Factory;

use Mollie;
use Mollie\Adapter\Link;
use Mollie\Adapter\Context;
use Mollie\Repository\MolCustomerRepository;
use Mollie\Repository\PaymentMethodRepositoryInterface;
use Mollie\Subscription\DTO\CreateSubscriptionData as SubscriptionDataDTO;
use Mollie\Subscription\DTO\Object\Amount;
use Mollie\Subscription\Exception\CouldNotProvideSubscriptionCarrierDeliveryPrice;
use Mollie\Subscription\Exception\SubscriptionIntervalException;
use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider;
use Mollie\Subscription\Provider\SubscriptionDescriptionProvider;
use Mollie\Subscription\Provider\SubscriptionIntervalProvider;
use Mollie\Subscription\Repository\CombinationRepository;
Expand All @@ -36,10 +39,12 @@ class CreateSubscriptionDataFactory

/** @var PaymentMethodRepositoryInterface */
private $methodRepository;
/** @var Link */
private $link;
/** @var Mollie */
private $module;
/** @var Context */
private $context;
/** @var SubscriptionCarrierDeliveryPriceProvider */
private $subscriptionCarrierDeliveryPriceProvider;

public function __construct(
MolCustomerRepository $customerRepository,
Expand All @@ -48,19 +53,26 @@ public function __construct(
CurrencyAdapter $currencyAdapter,
CombinationRepository $combination,
PaymentMethodRepositoryInterface $methodRepository,
Link $link,
Mollie $module
Mollie $module,
Context $context,
SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider
) {
$this->customerRepository = $customerRepository;
$this->subscriptionInterval = $subscriptionInterval;
$this->subscriptionDescription = $subscriptionDescription;
$this->currencyAdapter = $currencyAdapter;
$this->combination = $combination;
$this->methodRepository = $methodRepository;
$this->link = $link;
$this->module = $module;
$this->context = $context;
$this->subscriptionCarrierDeliveryPriceProvider = $subscriptionCarrierDeliveryPriceProvider;
}

/**
* @throws \PrestaShopException
* @throws CouldNotProvideSubscriptionCarrierDeliveryPrice
* @throws SubscriptionIntervalException
*/
public function build(Order $order, array $subscriptionProduct): SubscriptionDataDTO
{
$customer = $order->getCustomer();
Expand All @@ -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->subscriptionCarrierDeliveryPriceProvider->getPrice(
(int) $order->id_address_delivery,
(int) $order->id_cart,
(int) $order->id_customer,
$subscriptionProduct
);
} catch (CouldNotProvideSubscriptionCarrierDeliveryPrice $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,
Expand All @@ -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'
));
Expand Down
5 changes: 4 additions & 1 deletion subscription/Handler/SubscriptionCreationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
Loading
Loading