Skip to content

Commit

Permalink
PIPRES-349: Abstract recurring order creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Nov 28, 2023
1 parent f885748 commit 1d1cdda
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 35 deletions.
78 changes: 78 additions & 0 deletions subscription/Action/CreateRecurringOrderAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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
*/

namespace Mollie\Subscription\Action;

use Mollie\Subscription\DTO\CreateRecurringOrderData;
use Mollie\Subscription\Exception\CouldNotCreateRecurringOrder;
use Mollie\Subscription\Exception\MollieSubscriptionException;
use Mollie\Subscription\Logger\LoggerInterface;
use Mollie\Subscription\Utility\ClockInterface;

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

class CreateRecurringOrderAction
{
/** @var LoggerInterface */
private $logger;
/** @var ClockInterface */
private $clock;

public function __construct(
LoggerInterface $logger,
ClockInterface $clock
) {
$this->logger = $logger;
$this->clock = $clock;
}

/**
* @throws MollieSubscriptionException
*/
public function run(CreateRecurringOrderData $data): \MolRecurringOrder
{
$this->logger->debug(sprintf('%s - Function called', __METHOD__));

try {
$recurringOrder = new \MolRecurringOrder();

$recurringOrder->id_mol_recurring_orders_product = $data->getRecurringOrdersProductId();
$recurringOrder->id_order = $data->getOrderId();
$recurringOrder->id_cart = $data->getCartId();
$recurringOrder->id_currency = $data->getCurrencyId();
$recurringOrder->id_customer = $data->getCustomerId();
$recurringOrder->id_address_delivery = $data->getDeliveryAddressId();
$recurringOrder->id_address_invoice = $data->getInvoiceAddressId();
$recurringOrder->description = $data->getDescription();
$recurringOrder->status = $data->getStatus();
$recurringOrder->total_tax_incl = $data->getSubscriptionTotalAmount();
$recurringOrder->payment_method = $data->getMethod();
$recurringOrder->next_payment = $data->getNextPayment();
$recurringOrder->reminder_at = $data->getReminderAt();
$recurringOrder->cancelled_at = $data->getCancelledAt();
$recurringOrder->mollie_subscription_id = $data->getMollieSubscriptionId();
$recurringOrder->mollie_customer_id = $data->getMollieCustomerId();
$recurringOrder->date_add = $this->clock->getCurrentDate();
$recurringOrder->date_update = $this->clock->getCurrentDate();

$recurringOrder->add();
} catch (\Throwable $exception) {
throw CouldNotCreateRecurringOrder::unknownError($exception);
}

$this->logger->debug(sprintf('%s - Function ended', __METHOD__));

return $recurringOrder;
}
}
255 changes: 255 additions & 0 deletions subscription/DTO/CreateRecurringOrderData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
<?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
*/

namespace Mollie\Subscription\DTO;

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

class CreateRecurringOrderData
{
/** @var int */
private $recurringOrdersProductId;
/** @var int */
private $orderId;
/** @var int */
private $cartId;
/** @var int */
private $currencyId;
/** @var int */
private $customerId;
/** @var int */
private $deliveryAddressId;
/** @var int */
private $invoiceAddressId;
/** @var string */
private $description;
/** @var string */
private $status;
/** @var float */
private $subscriptionTotalAmount;
/** @var string */
private $method;
/** @var string */
private $nextPayment;
/** @var string */
private $reminderAt;
/** @var string */
private $cancelledAt;
/** @var string */
private $mollieSubscriptionId;
/** @var string */
private $mollieCustomerId;

private function __construct(
int $recurringOrdersProductId,
int $orderId,
int $cartId,
int $currencyId,
int $customerId,
int $deliveryAddressId,
int $invoiceAddressId,
string $description,
string $status,
float $subscriptionTotalAmount,
string $method,
string $nextPayment,
string $reminderAt,
string $cancelledAt,
string $mollieSubscriptionId,
string $mollieCustomerId
) {
$this->recurringOrdersProductId = $recurringOrdersProductId;
$this->orderId = $orderId;
$this->cartId = $cartId;
$this->currencyId = $currencyId;
$this->customerId = $customerId;
$this->deliveryAddressId = $deliveryAddressId;
$this->invoiceAddressId = $invoiceAddressId;
$this->description = $description;
$this->status = $status;
$this->subscriptionTotalAmount = $subscriptionTotalAmount;
$this->method = $method;
$this->nextPayment = $nextPayment;
$this->reminderAt = $reminderAt;
$this->cancelledAt = $cancelledAt;
$this->mollieSubscriptionId = $mollieSubscriptionId;
$this->mollieCustomerId = $mollieCustomerId;
}

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

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

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

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

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

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

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

/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}

/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}

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

/**
* @return string
*/
public function getMethod(): string
{
return $this->method;
}

/**
* @return string
*/
public function getNextPayment(): string
{
return $this->nextPayment;
}

/**
* @return string
*/
public function getReminderAt(): string
{
return $this->reminderAt;
}

/**
* @return string
*/
public function getCancelledAt(): string
{
return $this->cancelledAt;
}

/**
* @return string
*/
public function getMollieSubscriptionId(): string
{
return $this->mollieSubscriptionId;
}

/**
* @return string
*/
public function getMollieCustomerId(): string
{
return $this->mollieCustomerId;
}

public static function create(
int $recurringOrdersProductId,
int $orderId,
int $cartId,
int $currencyId,
int $customerId,
int $deliveryAddressId,
int $invoiceAddressId,
string $description,
string $status,
float $subscriptionTotalAmount,
string $method,
string $nextPayment,
string $reminderAt,
string $cancelledAt,
string $mollieSubscriptionId,
string $mollieCustomerId
): self {
return new self(
$recurringOrdersProductId,
$orderId,
$cartId,
$currencyId,
$customerId,
$deliveryAddressId,
$invoiceAddressId,
$description,
$status,
$subscriptionTotalAmount,
$method,
$nextPayment,
$reminderAt,
$cancelledAt,
$mollieSubscriptionId,
$mollieCustomerId
);
}
}
21 changes: 21 additions & 0 deletions subscription/Exception/CouldNotCreateRecurringOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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
*/

namespace Mollie\Subscription\Exception;

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

class CouldNotCreateRecurringOrder extends MollieSubscriptionException
{
}
Loading

0 comments on commit 1d1cdda

Please sign in to comment.