Skip to content

Commit

Permalink
PIPRES-261: Create fresh specific price
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Oct 2, 2023
1 parent 26e134a commit 2990e9e
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 38 deletions.
3 changes: 3 additions & 0 deletions src/ServiceProvider/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
use Mollie\Subscription\Repository\RecurringOrdersProductRepository;
use Mollie\Subscription\Repository\RecurringOrdersProductRepositoryInterface;
use Mollie\Subscription\Repository\SpecificPriceRepository;
use Mollie\Subscription\Repository\SpecificPriceRepositoryInterface;
use Mollie\Subscription\Utility\Clock;
use Mollie\Subscription\Utility\ClockInterface;
use Mollie\Utility\Decoder\DecoderInterface;
Expand Down Expand Up @@ -139,6 +141,7 @@ public function register(Container $container)

$this->addService($container, RetryHandlerInterface::class, $container->get(RetryHandler::class));

$this->addService($container, SpecificPriceRepositoryInterface::class, $container->get(SpecificPriceRepository::class));
$this->addService($container, ProductRepositoryInterface::class, $container->get(ProductRepository::class));
$this->addService($container, OrderDetailRepositoryInterface::class, $container->get(OrderDetailRepository::class));
$this->addService($container, CountryRepositoryInterface::class, $container->get(CountryRepository::class));
Expand Down
67 changes: 67 additions & 0 deletions subscription/Action/CreateSpecificPriceAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Mollie\Subscription\Action;

use Mollie\Subscription\DTO\CreateSpecificPriceData;
use Mollie\Subscription\Repository\SpecificPriceRepositoryInterface;

class CreateSpecificPriceAction
{
/** @var SpecificPriceRepositoryInterface */
private $specificPriceRepository;

public function __construct(
SpecificPriceRepositoryInterface $specificPriceRepository
) {
$this->specificPriceRepository = $specificPriceRepository;
}

/**
* @throws \Throwable
*/
public function run(CreateSpecificPriceData $data): \SpecificPrice
{
/** @var \SpecificPrice[] $specificPrices */
$specificPrices = $this->specificPriceRepository->findAllBy([
'id_product' => $data->getProductId(),
'id_product_attribute' => $data->getProductAttributeId(),
'price' => $data->getPrice(),
'id_customer' => $data->getCustomerId(),
'id_shop' => $data->getShopId(),
'id_currency' => $data->getCurrencyId(),
'id_shop_group' => $data->getShopGroupId(),
'id_country' => 0,
'id_group' => 0,
'from_quantity' => 0,
'reduction' => 0,
'reduction_type' => 'amount',
'from' => '0000-00-00 00:00:00',
'to' => '0000-00-00 00:00:00',
]);

foreach ($specificPrices as $specificPrice) {
$specificPrice->delete();
}

$specificPrice = new \SpecificPrice();

$specificPrice->id_product = $data->getProductId();
$specificPrice->id_product_attribute = $data->getProductAttributeId();
$specificPrice->price = $data->getPrice();
$specificPrice->id_customer = $data->getCustomerId();
$specificPrice->id_shop = $data->getShopId();
$specificPrice->id_currency = $data->getCurrencyId();
$specificPrice->id_shop_group = $data->getShopGroupId();
$specificPrice->id_country = 0;
$specificPrice->id_group = 0;
$specificPrice->from_quantity = 0;
$specificPrice->reduction = 0;
$specificPrice->reduction_type = 'amount';
$specificPrice->from = '0000-00-00 00:00:00';
$specificPrice->to = '0000-00-00 00:00:00';

$specificPrice->add();

return $specificPrice;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public function indexAction()
'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 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),
'cartRule1' => $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),
Expand Down
115 changes: 115 additions & 0 deletions subscription/DTO/CreateSpecificPriceData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Mollie\Subscription\DTO;

class CreateSpecificPriceData
{
/** @var int */
private $productId;
/** @var int */
private $productAttributeId;
/** @var float */
private $price;
/** @var int */
private $customerId;
/** @var int */
private $shopId;
/** @var int */
private $shopGroupId;
/** @var int */
private $currencyId;

public function __construct(
int $productId,
int $productAttributeId,
float $price,
int $customerId,
int $shopId,
int $shopGroupId,
int $currencyId
) {
$this->productId = $productId;
$this->productAttributeId = $productAttributeId;
$this->price = $price;
$this->customerId = $customerId;
$this->shopId = $shopId;
$this->shopGroupId = $shopGroupId;
$this->currencyId = $currencyId;
}

/**
* @return int
*/
public function getProductId(): int
{
return $this->productId;
}

/**
* @return int
*/
public function getProductAttributeId(): int
{
return $this->productAttributeId;
}

/**
* @return float
*/
public function getPrice(): float
{
return $this->price;
}

/**
* @return int
*/
public function getCustomerId(): int
{
return $this->customerId;
}

/**
* @return int
*/
public function getShopId(): int
{
return $this->shopId;
}

/**
* @return int
*/
public function getShopGroupId(): int
{
return $this->shopGroupId;
}

/**
* @return int
*/
public function getCurrencyId(): int
{
return $this->currencyId;
}

public static function create(
int $productId,
int $productAttributeId,
float $price,
int $customerId,
int $shopId,
int $shopGroupId,
int $currencyId
): self {
return new self(
$productId,
$productAttributeId,
$price,
$customerId,
$shopId,
$shopGroupId,
$currencyId
);
}
}
72 changes: 38 additions & 34 deletions subscription/Handler/RecurringOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Cart;
use Mollie;
use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Adapter\Shop;
use Mollie\Api\Resources\Payment;
use Mollie\Api\Resources\Subscription as MollieSubscription;
use Mollie\Api\Types\PaymentStatus;
Expand All @@ -17,12 +16,15 @@
use Mollie\Exception\TransactionException;
use Mollie\Logger\PrestaLoggerInterface;
use Mollie\Repository\CarrierRepositoryInterface;
use Mollie\Repository\OrderRepositoryInterface;
use Mollie\Repository\PaymentMethodRepositoryInterface;
use Mollie\Service\MailService;
use Mollie\Service\MollieOrderCreationService;
use Mollie\Service\OrderStatusService;
use Mollie\Service\PaymentMethodService;
use Mollie\Subscription\Action\CreateSpecificPriceAction;
use Mollie\Subscription\Api\SubscriptionApi;
use Mollie\Subscription\DTO\CreateSpecificPriceData;
use Mollie\Subscription\Exception\CouldNotHandleRecurringOrder;
use Mollie\Subscription\Factory\GetSubscriptionDataFactory;
use Mollie\Subscription\Repository\RecurringOrderRepositoryInterface;
Expand All @@ -32,7 +34,6 @@
use MolRecurringOrder;
use MolRecurringOrdersProduct;
use Order;
use SpecificPrice;

class RecurringOrderHandler
{
Expand All @@ -54,8 +55,6 @@ class RecurringOrderHandler
private $paymentMethodService;
/** @var ClockInterface */
private $clock;
/** @var Shop */
private $shop;
/** @var MailService */
private $mailService;
/** @var ConfigurationAdapter */
Expand All @@ -66,6 +65,10 @@ class RecurringOrderHandler
private $carrierRepository;
/** @var PrestaLoggerInterface */
private $logger;
/** @var CreateSpecificPriceAction */
private $createSpecificPriceAction;
/** @var Mollie\Repository\OrderRepositoryInterface */
private $orderRepository;

public function __construct(
SubscriptionApi $subscriptionApi,
Expand All @@ -77,12 +80,13 @@ public function __construct(
OrderStatusService $orderStatusService,
PaymentMethodService $paymentMethodService,
ClockInterface $clock,
Shop $shop,
MailService $mailService,
ConfigurationAdapter $configuration,
RecurringOrdersProductRepositoryInterface $recurringOrdersProductRepository,
CarrierRepositoryInterface $carrierRepository,
PrestaLoggerInterface $logger
PrestaLoggerInterface $logger,
CreateSpecificPriceAction $createSpecificPriceAction,
OrderRepositoryInterface $orderRepository
) {
$this->subscriptionApi = $subscriptionApi;
$this->subscriptionDataFactory = $subscriptionDataFactory;
Expand All @@ -93,12 +97,13 @@ public function __construct(
$this->orderStatusService = $orderStatusService;
$this->paymentMethodService = $paymentMethodService;
$this->clock = $clock;
$this->shop = $shop;
$this->mailService = $mailService;
$this->configuration = $configuration;
$this->recurringOrdersProductRepository = $recurringOrdersProductRepository;
$this->carrierRepository = $carrierRepository;
$this->logger = $logger;
$this->createSpecificPriceAction = $createSpecificPriceAction;
$this->orderRepository = $orderRepository;
}

public function handle(string $transactionId): string
Expand Down Expand Up @@ -152,9 +157,23 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec
return;
}

/** @var \Order|null $originalOrder */
$originalOrder = $this->orderRepository->findOneBy([
'id_order' => $recurringOrder->id_order,
]);

if (!$originalOrder) {
return;
}

/** @var Cart $newCart */
$newCart = $newCart['cart'];

$newCart->id_shop = $originalOrder->id_shop;
$newCart->id_shop_group = $originalOrder->id_shop_group;

$newCart->update();

/** @var MolRecurringOrdersProduct $subscriptionProduct */
$subscriptionProduct = $this->recurringOrdersProductRepository->findOneBy([
'id_mol_recurring_orders_product' => $recurringOrder->id_mol_recurring_orders_product,
Expand Down Expand Up @@ -209,7 +228,18 @@ private function createSubscription(Payment $transaction, MolRecurringOrder $rec
throw CouldNotHandleRecurringOrder::failedToApplySelectedCarrier();
}

$specificPrice = $this->createSpecificPrice($recurringOrderProduct, $recurringOrder);
/**
* Creating temporary specific price for recurring order that will be deleted after order is created
*/
$specificPrice = $this->createSpecificPriceAction->run(CreateSpecificPriceData::create(
(int) $recurringOrderProduct->id_product,
(int) $recurringOrderProduct->id_product_attribute,
(float) $recurringOrderProduct->unit_price,
(int) $recurringOrder->id_customer,
(int) $newCart->id_shop,
(int) $newCart->id_shop_group,
(int) $recurringOrder->id_currency
));

$paymentMethod = $this->paymentMethodService->getPaymentMethod($transaction);

Expand Down Expand Up @@ -284,32 +314,6 @@ private function cancelSubscription(int $recurringOrderId): void
$this->mailService->sendSubscriptionCancelWarningEmail($recurringOrderId);
}

/**
* creating temporary specific price for recurring order that will be deleted after order is created
*/
private function createSpecificPrice(MolRecurringOrdersProduct $molRecurringOrdersProduct, MolRecurringOrder $recurringOrder): SpecificPrice
{
$specificPrice = new SpecificPrice();
$specificPrice->id_product = $molRecurringOrdersProduct->id_product;
$specificPrice->id_product_attribute = $molRecurringOrdersProduct->id_product_attribute;
$specificPrice->price = $molRecurringOrdersProduct->unit_price;
$specificPrice->id_customer = $recurringOrder->id_customer;
$specificPrice->id_shop = $this->shop->getShop()->id;
$specificPrice->id_currency = $recurringOrder->id_currency;
$specificPrice->id_country = 0;
$specificPrice->id_shop_group = 0;
$specificPrice->id_group = 0;
$specificPrice->from_quantity = 0;
$specificPrice->reduction = 0;
$specificPrice->reduction_type = 'amount';
$specificPrice->from = '0000-00-00 00:00:00';
$specificPrice->to = '0000-00-00 00:00:00';

$specificPrice->add();

return $specificPrice;
}

private function updateSubscriptionOrderAddress(Cart $cart, int $addressInvoiceId, int $addressDeliveryId): Cart
{
$cart->id_address_invoice = $addressInvoiceId;
Expand Down
11 changes: 11 additions & 0 deletions subscription/Repository/SpecificPriceRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Mollie\Subscription\Repository;

class SpecificPriceRepository extends AbstractRepository implements SpecificPriceRepositoryInterface
{
public function __construct()
{
parent::__construct(\SpecificPrice::class);
}
}
9 changes: 9 additions & 0 deletions subscription/Repository/SpecificPriceRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Mollie\Subscription\Repository;

use Mollie\Repository\ReadOnlyRepositoryInterface;

interface SpecificPriceRepositoryInterface extends ReadOnlyRepositoryInterface
{
}
Loading

0 comments on commit 2990e9e

Please sign in to comment.