diff --git a/subscription/Action/CreateRecurringOrderAction.php b/subscription/Action/CreateRecurringOrderAction.php new file mode 100644 index 000000000..09aa62f18 --- /dev/null +++ b/subscription/Action/CreateRecurringOrderAction.php @@ -0,0 +1,78 @@ + + * @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; + } +} diff --git a/subscription/DTO/CreateRecurringOrderData.php b/subscription/DTO/CreateRecurringOrderData.php new file mode 100644 index 000000000..8e7d01de1 --- /dev/null +++ b/subscription/DTO/CreateRecurringOrderData.php @@ -0,0 +1,255 @@ + + * @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 + ); + } +} diff --git a/subscription/Exception/CouldNotCreateRecurringOrder.php b/subscription/Exception/CouldNotCreateRecurringOrder.php new file mode 100644 index 000000000..27c111d2d --- /dev/null +++ b/subscription/Exception/CouldNotCreateRecurringOrder.php @@ -0,0 +1,21 @@ + + * @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 +{ +} diff --git a/subscription/Handler/SubscriptionCreationHandler.php b/subscription/Handler/SubscriptionCreationHandler.php index ab48bedc1..03c086968 100644 --- a/subscription/Handler/SubscriptionCreationHandler.php +++ b/subscription/Handler/SubscriptionCreationHandler.php @@ -14,15 +14,13 @@ namespace Mollie\Subscription\Handler; -use Mollie\Api\Resources\Subscription; +use Mollie\Subscription\Action\CreateRecurringOrderAction; use Mollie\Subscription\Action\CreateRecurringOrdersProductAction; use Mollie\Subscription\Api\SubscriptionApi; +use Mollie\Subscription\DTO\CreateRecurringOrderData; use Mollie\Subscription\DTO\CreateRecurringOrdersProductData; use Mollie\Subscription\Factory\CreateSubscriptionDataFactory; -use Mollie\Subscription\Utility\ClockInterface; use Mollie\Subscription\Validator\SubscriptionProductValidator; -use MolRecurringOrder; -use MolRecurringOrdersProduct; use Order; if (!defined('_PS_VERSION_')) { @@ -31,31 +29,29 @@ class SubscriptionCreationHandler { - /** @var ClockInterface */ - private $clock; - /** @var SubscriptionApi */ private $subscriptionApi; - /** @var CreateSubscriptionDataFactory */ private $createSubscriptionDataFactory; /** @var SubscriptionProductValidator */ private $subscriptionProductValidator; /** @var CreateRecurringOrdersProductAction */ private $createRecurringOrdersProductAction; + /** @var CreateRecurringOrderAction */ + private $createRecurringOrderAction; public function __construct( - ClockInterface $clock, SubscriptionApi $subscriptionApi, CreateSubscriptionDataFactory $subscriptionDataFactory, SubscriptionProductValidator $subscriptionProductValidator, - CreateRecurringOrdersProductAction $createRecurringOrdersProductAction + CreateRecurringOrdersProductAction $createRecurringOrdersProductAction, + CreateRecurringOrderAction $createRecurringOrderAction ) { - $this->clock = $clock; $this->subscriptionApi = $subscriptionApi; $this->createSubscriptionDataFactory = $subscriptionDataFactory; $this->subscriptionProductValidator = $subscriptionProductValidator; $this->createRecurringOrdersProductAction = $createRecurringOrdersProductAction; + $this->createRecurringOrderAction = $createRecurringOrderAction; } /** @@ -94,30 +90,29 @@ public function handle(Order $order, string $method): void throw $exception; } - $this->createRecurringOrder($recurringOrdersProduct, $order, $subscription, $method); - } + try { + $this->createRecurringOrderAction->run(CreateRecurringOrderData::create( + (int) $recurringOrdersProduct->id, + (int) $order->id, + (int) $order->id_cart, + (int) $order->id_currency, + (int) $order->id_customer, + (int) $order->id_address_delivery, + (int) $order->id_address_invoice, + (string) $subscription->description, + (string) $subscription->status, + (float) $subscription->amount->value, + $method, + (string) $subscription->nextPaymentDate, + (string) $subscription->nextPaymentDate, // TODO: add logic to get reminder date when reminder is done + (string) $subscription->canceledAt, + (string) $subscription->id, + (string) $subscription->customerId + )); + } catch (\Throwable $exception) { + // TODO throw different exception - private function createRecurringOrder(MolRecurringOrdersProduct $recurringOrdersProduct, Order $order, Subscription $subscription, string $method): void - { - $recurringOrder = new MolRecurringOrder(); - $recurringOrder->id_mol_recurring_orders_product = $recurringOrdersProduct->id; - $recurringOrder->id_order = $order->id; - $recurringOrder->id_cart = $order->id_cart; - $recurringOrder->id_currency = $order->id_currency; - $recurringOrder->id_customer = $order->id_customer; - $recurringOrder->id_address_delivery = $order->id_address_delivery; - $recurringOrder->id_address_invoice = $order->id_address_invoice; - $recurringOrder->description = $subscription->description; - $recurringOrder->status = $subscription->status; - $recurringOrder->total_tax_incl = (float) $subscription->amount->value; - $recurringOrder->payment_method = $method; - $recurringOrder->next_payment = $subscription->nextPaymentDate; - $recurringOrder->reminder_at = $subscription->nextPaymentDate; //todo: add logic to get reminder date when reminder is done - $recurringOrder->cancelled_at = $subscription->canceledAt; - $recurringOrder->mollie_subscription_id = $subscription->id; - $recurringOrder->mollie_customer_id = $subscription->customerId; - $recurringOrder->date_add = $this->clock->getDateFromTimeStamp(strtotime($subscription->createdAt)); - $recurringOrder->date_update = $this->clock->getCurrentDate(); - $recurringOrder->add(); + throw $exception; + } } } diff --git a/tests/Integration/Subscription/Action/CreateRecurringOrderActionTest.php b/tests/Integration/Subscription/Action/CreateRecurringOrderActionTest.php new file mode 100644 index 000000000..f50498ce9 --- /dev/null +++ b/tests/Integration/Subscription/Action/CreateRecurringOrderActionTest.php @@ -0,0 +1,85 @@ + + * @copyright Mollie B.V. + * @license https://github.com/mollie/PrestaShop/blob/master/LICENSE.md + * + * @see https://github.com/mollie/PrestaShop + * @codingStandardsIgnoreStart + */ + +namespace Mollie\Tests\Integration\Subscription\Action; + +use Mollie\Subscription\Action\CreateRecurringOrderAction; +use Mollie\Subscription\DTO\CreateRecurringOrderData; +use Mollie\Tests\Integration\BaseTestCase; + +class CreateRecurringOrderActionTest extends BaseTestCase +{ + public function testItSuccessfullyCreateDatabaseEntry(): void + { + $this->assertDatabaseHasNot(\MolRecurringOrder::class, [ + 'id_mol_recurring_orders_product' => 1, + 'id_order' => 1, + 'id_cart' => 1, + 'id_currency' => 1, + 'id_customer' => 1, + 'id_address_delivery' => 1, + 'id_address_invoice' => 1, + 'mollie_subscription_id' => 'test-mollie-subscription-id', + 'mollie_customer_id' => 'test-mollie-customer-id', + 'description' => 'test-description', + 'status' => 'test-status', + 'total_tax_incl' => 19.99, + 'payment_method' => 'test-payment-method', + 'next_payment' => '2023-09-09 12:00:00', + 'reminder_at' => '2023-09-10 12:00:00', + 'cancelled_at' => '2023-09-11 12:00:00', + ]); + + /** @var CreateRecurringOrderAction $createRecurringOrderAction */ + $createRecurringOrderAction = $this->getService(CreateRecurringOrderAction::class); + + $result = $createRecurringOrderAction->run(CreateRecurringOrderData::create( + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 'test-description', + 'test-status', + 19.99, + 'test-payment-method', + '2023-09-09 12:00:00', + '2023-09-10 12:00:00', + '2023-09-11 12:00:00', + 'test-mollie-subscription-id', + 'test-mollie-customer-id' + )); + + $this->assertDatabaseHas(\MolRecurringOrder::class, [ + 'id_mol_recurring_orders_product' => 1, + 'id_order' => 1, + 'id_cart' => 1, + 'id_currency' => 1, + 'id_customer' => 1, + 'id_address_delivery' => 1, + 'id_address_invoice' => 1, + 'mollie_subscription_id' => 'test-mollie-subscription-id', + 'mollie_customer_id' => 'test-mollie-customer-id', + 'description' => 'test-description', + 'status' => 'test-status', + 'total_tax_incl' => 19.99, + 'payment_method' => 'test-payment-method', + 'next_payment' => '2023-09-09 12:00:00', + 'reminder_at' => '2023-09-10 12:00:00', + 'cancelled_at' => '2023-09-11 12:00:00', + ]); + + $result->delete(); + } +}