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: Separate carrier for recurring orders improvements #821

Merged
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
21 changes: 17 additions & 4 deletions controllers/front/recurringOrderDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

use Mollie\Controller\AbstractMollieController;
use Mollie\Logger\PrestaLoggerInterface;
use Mollie\Subscription\Handler\FreeOrderCreationHandler;
use Mollie\Subscription\Handler\SubscriptionCancellationHandler;
use Mollie\Subscription\Presenter\RecurringOrderPresenter;
Expand Down Expand Up @@ -74,13 +75,25 @@ public function initContent()
Tools::redirect(Context::getContext()->link->getModuleLink($this->module->name, 'subscriptions', [], true));
}

/** @var PrestaLoggerInterface $logger */
$logger = $this->module->getService(PrestaLoggerInterface::class);

/** @var RecurringOrderPresenter $recurringOrderPresenter */
$recurringOrderPresenter = $this->module->getService(RecurringOrderPresenter::class);

$this->context->smarty->assign([
'recurringOrderData' => $recurringOrderPresenter->present($recurringOrderId),
'token' => Tools::getToken(),
]);
try {
$this->context->smarty->assign([
'recurringOrderData' => $recurringOrderPresenter->present($recurringOrderId),
'token' => Tools::getToken(),
]);
} catch (Throwable $exception) {
$logger->error('Failed to present subscription order', [
'Exception message' => $exception->getMessage(),
'Exception code' => $exception->getCode(),
]);

Tools::redirect(Context::getContext()->link->getModuleLink($this->module->name, 'subscriptions', [], true));
}

parent::initContent();
$this->context->controller->addCSS($this->module->getPathUri() . 'views/css/front/subscription/customer_order_detail.css');
Expand Down
9 changes: 9 additions & 0 deletions controllers/front/subscriptionWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Mollie\Controller\AbstractMollieController;
use Mollie\Errors\Http\HttpStatusCode;
use Mollie\Handler\ErrorHandler\ErrorHandler;
use Mollie\Logger\PrestaLoggerInterface;
use Mollie\Subscription\Handler\RecurringOrderHandler;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -62,9 +63,17 @@ protected function executeWebhook()
/** @var ErrorHandler $errorHandler */
$errorHandler = $this->module->getService(ErrorHandler::class);

/** @var PrestaLoggerInterface $logger */
$logger = $this->module->getService(PrestaLoggerInterface::class);

try {
$recurringOrderHandler->handle($transactionId);
} catch (\Throwable $exception) {
$logger->error('Failed to handle recurring order', [
'Exception message' => $exception->getMessage(),
'Exception code' => $exception->getCode(),
]);

$errorHandler->handle($exception, null, false);

$this->respond('failed', HttpStatusCode::HTTP_BAD_REQUEST);
Expand Down
15 changes: 15 additions & 0 deletions src/Adapter/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,19 @@ public function getShopGroupId(): int
{
return (int) PrestashopContext::getContext()->shop->id_shop_group;
}

public function formatPrice(float $price, string $isoCode): string
{
$locale = PrestashopContext::getContext()->getCurrentLocale();

/* @phpstan-ignore-next-line */
if (!$locale) {
return (string) $price;
}

return $locale->formatPrice(
$price,
$isoCode
);
}
}
4 changes: 2 additions & 2 deletions src/Adapter/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function getShop(): \Shop
return \Context::getContext()->shop;
}

public function getContext()
public function getContext(): int
{
return $this->getShop()->getContext();
return (int) $this->getShop()->getContext();
}
}
57 changes: 56 additions & 1 deletion src/Builder/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Mollie\Api\Types\RefundStatus;
use Mollie\Config\Config;
use Mollie\Provider\CustomLogoProviderInterface;
use Mollie\Repository\CarrierRepositoryInterface;
use Mollie\Repository\TaxRulesGroupRepositoryInterface;
use Mollie\Service\ApiService;
use Mollie\Service\ConfigFieldService;
Expand Down Expand Up @@ -90,6 +91,8 @@ class FormBuilder

/** @var Context */
private $context;
/** @var CarrierRepositoryInterface */
private $carrierRepository;

public function __construct(
Mollie $module,
Expand All @@ -103,7 +106,8 @@ public function __construct(
CustomLogoProviderInterface $creditCardLogoProvider,
ConfigurationAdapter $configuration,
TaxRulesGroupRepositoryInterface $taxRulesGroupRepository,
Context $context
Context $context,
CarrierRepositoryInterface $carrierRepository
) {
$this->module = $module;
$this->apiService = $apiService;
Expand All @@ -117,6 +121,7 @@ public function __construct(
$this->configuration = $configuration;
$this->taxRulesGroupRepository = $taxRulesGroupRepository;
$this->context = $context;
$this->carrierRepository = $carrierRepository;
}

public function buildSettingsForm()
Expand Down Expand Up @@ -527,6 +532,8 @@ protected function getAdvancedSettingsSection()
],
];

$input = array_merge($input, $this->getShippingOptions($advancedSettings));

$messageStatus = $this->module->l('Status for %s payments', self::FILE_NAME);
$descriptionStatus = $this->module->l('`%s` payments get `%s` status', self::FILE_NAME);
$messageMail = $this->module->l('Send email when %s', self::FILE_NAME);
Expand Down Expand Up @@ -821,4 +828,52 @@ private function getSettingTabs($isApiKeyProvided)

return $tabs;
}

private function getShippingOptions(string $tab): array
{
/** @var \Carrier[] $carriers */
$carriers = $this->carrierRepository->findAllBy([
'active' => 1,
'deleted' => 0,
]);

$mappedCarriers = [];

$mappedCarriers[] = [
'id' => 0,
'name' => $this->module->l('Not selected', self::FILE_NAME),
];

foreach ($carriers as $carrier) {
$mappedCarriers[] = [
'id' => $carrier->id,
'name' => $carrier->name,
];
}

$header = [
'type' => 'mollie-h2',
'name' => '',
'tab' => $tab,
'title' => $this->module->l('Subscriptions', self::FILE_NAME),
];

$options = [
'type' => 'select',
'label' => $this->module->l('Select shipping option to use in subscription orders', self::FILE_NAME),
'desc' => $this->module->l('WARNING: do not change selection after getting first subscription order.', self::FILE_NAME),
'tab' => $tab,
'name' => Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID,
'options' => [
'query' => $mappedCarriers,
'id' => 'id',
'name' => 'name',
],
];

return [
$header,
$options,
];
}
}
2 changes: 2 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class Config
const MOLLIE_CARRIER_URL_SOURCE = 'MOLLIE_CARRIER_URL_SOURCE_';
const MOLLIE_CARRIER_CUSTOM_URL = 'MOLLIE_CARRIER_CUSTOM_URL_';

const MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID = 'MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID';

const MOLLIE_METHOD_ENABLED = 'MOLLIE_METHOD_ENABLED_';
const MOLLIE_METHOD_TITLE = 'MOLLIE_METHOD_TITLE_';
const MOLLIE_METHOD_API = 'MOLLIE_METHOD_API_';
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CouldNotCreateOrderPaymentFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CouldNotCreateOrderPaymentFee extends MollieException
{
public static function failedToInsertOrderPaymentFee(Exception $exception): CouldNotCreateOrderPaymentFee
public static function failedToInsertOrderPaymentFee(Exception $exception): self
{
return new self(
'Failed to insert order payment fee.',
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CouldNotInstallModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class CouldNotInstallModule extends MollieException
{
public static function failedToInstallOrderState(string $orderStateName, \Exception $exception): CouldNotInstallModule
public static function failedToInstallOrderState(string $orderStateName, \Exception $exception): self
{
return new self(
sprintf('Failed to install order state (%s).', $orderStateName),
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CouldNotUpdateOrderTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CouldNotUpdateOrderTotals extends MollieException
{
public static function failedToUpdateOrderTotals(Exception $exception): CouldNotUpdateOrderTotals
public static function failedToUpdateOrderTotals(Exception $exception): self
{
return new self(
'Failed to update order totals.',
Expand Down
8 changes: 4 additions & 4 deletions src/Handler/Exception/CouldNotHandleOrderPaymentFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CouldNotHandleOrderPaymentFee extends MollieException
{
public static function failedToRetrievePaymentMethod(Throwable $exception): CouldNotHandleOrderPaymentFee
public static function failedToRetrievePaymentMethod(Throwable $exception): self
{
return new self(
'Failed to retrieve payment method',
Expand All @@ -17,7 +17,7 @@ public static function failedToRetrievePaymentMethod(Throwable $exception): Coul
);
}

public static function failedToRetrievePaymentFee(Throwable $exception): CouldNotHandleOrderPaymentFee
public static function failedToRetrievePaymentFee(Throwable $exception): self
{
return new self(
'Failed to retrieve payment fee',
Expand All @@ -26,7 +26,7 @@ public static function failedToRetrievePaymentFee(Throwable $exception): CouldNo
);
}

public static function failedToCreateOrderPaymentFee(Throwable $exception): CouldNotHandleOrderPaymentFee
public static function failedToCreateOrderPaymentFee(Throwable $exception): self
{
return new self(
'Failed to create order payment fee',
Expand All @@ -35,7 +35,7 @@ public static function failedToCreateOrderPaymentFee(Throwable $exception): Coul
);
}

public static function failedToUpdateOrderTotalWithPaymentFee(Throwable $exception): CouldNotHandleOrderPaymentFee
public static function failedToUpdateOrderTotalWithPaymentFee(Throwable $exception): self
{
return new self(
'Failed to update order total with payment fee.',
Expand Down
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(),
]
);
}
}
}
2 changes: 2 additions & 0 deletions src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ protected function initConfig()
$this->configurationAdapter->updateValue(Config::MOLLIE_API, Config::MOLLIE_ORDERS_API);
$this->configurationAdapter->updateValue(Config::MOLLIE_APPLE_PAY_DIRECT_STYLE, 0);
$this->configurationAdapter->updateValue(Config::MOLLIE_BANCONTACT_QR_CODE_ENABLED, 0);

$this->configurationAdapter->updateValue(Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID, 0);
}

public function setDefaultCarrierStatuses()
Expand Down
1 change: 1 addition & 0 deletions src/Install/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private function deleteConfig()
Config::METHODS_CONFIG,
Config::MOLLIE_MAIL_WHEN_COMPLETED,
Config::MOLLIE_API_KEY_TEST,
Config::MOLLIE_SUBSCRIPTION_ORDER_CARRIER_ID,
];

$this->deleteConfigurations($configurations);
Expand Down
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;
}
16 changes: 16 additions & 0 deletions src/Repository/CarrierRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mollie\Repository;

class CarrierRepository extends AbstractRepository implements CarrierRepositoryInterface
{
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);
}
}
8 changes: 8 additions & 0 deletions src/Repository/CarrierRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Mollie\Repository;

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
Loading
Loading