Skip to content

Commit

Permalink
PIPRES-373: start date added into subscription request
Browse files Browse the repository at this point in the history
  • Loading branch information
GytisZum committed May 22, 2024
1 parent be0eda2 commit 72fafc3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions subscription/DTO/CreateSubscriptionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public function setMetaData(array $metaData): void
$this->metaData = $metaData;
}

public function setStartDate(string $startDate): void
{
$this->startDate = $startDate;
}

public function jsonSerialize(): array
{
$json = [];
Expand Down
9 changes: 8 additions & 1 deletion subscription/Factory/CreateSubscriptionDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Mollie\Subscription\Provider\SubscriptionCarrierDeliveryPriceProvider;
use Mollie\Subscription\Provider\SubscriptionDescriptionProvider;
use Mollie\Subscription\Provider\SubscriptionIntervalProvider;
use Mollie\Subscription\Provider\SubscriptionStartDateProvider;
use Mollie\Subscription\Repository\CombinationRepository;
use Mollie\Subscription\Repository\CurrencyRepository as CurrencyAdapter;
use Mollie\Utility\SecureKeyUtility;
Expand Down Expand Up @@ -59,6 +60,8 @@ class CreateSubscriptionDataFactory
private $context;
/** @var SubscriptionCarrierDeliveryPriceProvider */
private $subscriptionCarrierDeliveryPriceProvider;
/** @var SubscriptionStartDateProvider */
private $subscriptionStartDateProvider;

public function __construct(
MolCustomerRepository $customerRepository,
Expand All @@ -69,7 +72,8 @@ public function __construct(
PaymentMethodRepositoryInterface $methodRepository,
Mollie $module,
Context $context,
SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider
SubscriptionCarrierDeliveryPriceProvider $subscriptionCarrierDeliveryPriceProvider,
SubscriptionStartDateProvider $subscriptionStartDateProvider
) {
$this->customerRepository = $customerRepository;
$this->subscriptionInterval = $subscriptionInterval;
Expand All @@ -80,6 +84,7 @@ public function __construct(
$this->module = $module;
$this->context = $context;
$this->subscriptionCarrierDeliveryPriceProvider = $subscriptionCarrierDeliveryPriceProvider;
$this->subscriptionStartDateProvider = $subscriptionStartDateProvider;
}

/**
Expand Down Expand Up @@ -139,6 +144,8 @@ public function build(Order $order, array $subscriptionProduct): SubscriptionDat
]
);

$subscriptionData->setStartDate($this->subscriptionStartDateProvider->getSubscriptionStartDate($combination));

// todo: check for solution what to do when mandate is missing
$payment = $this->methodRepository->getPaymentBy('cart_id', $order->id_cart);
$subscriptionData->setMandateId($payment['mandate_id']);
Expand Down
76 changes: 76 additions & 0 deletions subscription/Provider/SubscriptionStartDateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Mollie https://www.mollie.nl
*
* @author Mollie B.V. <[email protected]>
* @copyright Mollie B.V.
* @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md
*
* @see https://github.com/mollie/PrestaShop
* @codingStandardsIgnoreStart
*/

declare(strict_types=1);

namespace Mollie\Subscription\Provider;

use Combination;
use DateInterval;
use DateTime;
use DateTimeZone;
use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Subscription\Config\Config;
use Mollie\Subscription\DTO\Object\Interval;
use Mollie\Subscription\Exception\SubscriptionIntervalException;
use Mollie\Subscription\Utility\Clock;

if (!defined('_PS_VERSION_')) {
exit;
}

class SubscriptionStartDateProvider
{
/** @var ConfigurationAdapter */
private $configuration;

public function __construct(ConfigurationAdapter $configuration, Clock $clock)
{
$this->configuration = $configuration;
$this->clock = $clock;
}

/**
* Returns subscription date time
*
* @throws SubscriptionIntervalException
*/
public function getSubscriptionStartDate(Combination $combination): string
{
$currentTime = new DateTime('now', new DateTimeZone('UTC'));

foreach ($combination->getWsProductOptionValues() as $attribute) {
switch ($attribute['id']) {
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_DAILY):
$interval = new DateInterval('P1D');
break;
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_WEEKLY):
$interval = new DateInterval('P7D');
break;
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_MONTHLY):
$interval = new DateInterval('P1M');
break;
case $this->configuration->get(Config::SUBSCRIPTION_ATTRIBUTE_YEARLY):
$interval = new DateInterval('P1Y');
break;
default:
throw new SubscriptionIntervalException(sprintf('No interval exists for this %s attribute', $combination->id));
}

// Add the interval to the current time
$currentTime->add($interval);
return $currentTime->format('Y-m-d');
}

throw new SubscriptionIntervalException(sprintf('No interval exists for this %s attribute', $combination->id));
}
}

0 comments on commit 72fafc3

Please sign in to comment.