Skip to content

Commit

Permalink
Remove fields manager use services
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jan 3, 2025
1 parent 51a6ffa commit 0fd207a
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 172 deletions.
11 changes: 8 additions & 3 deletions src/Assets/MollieCheckoutBlocksSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayI;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\DefaultFieldsStrategy;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;
use Mollie\WooCommerce\Shared\Data;

Expand Down Expand Up @@ -126,9 +127,13 @@ public static function gatewayDataForWCBlocks(Data $dataService, array $deprecat
$content = $method->getProcessedDescriptionForBlock();
$issuers = false;
if ($method->getProperty('paymentFields') === true) {
$paymentFieldsService = $method->paymentFieldsService();
$paymentFieldsService->setStrategy($method);
$issuers = $method->paymentFieldsService()->getStrategyMarkup($deprecatedGateway);
$className = 'Mollie\\WooCommerce\\PaymentMethods\\PaymentFieldsStrategies\\' . ucfirst($method->getProperty('id')) . 'FieldsStrategy';
$paymentFieldsStrategy = class_exists($className) ? new $className(
$deprecatedGateway,
$gateway->get_description(),
$dataService
) : new DefaultFieldsStrategy($deprecatedGateway, $gateway->get_description(), $dataService);
$issuers = $paymentFieldsStrategy->getFieldMarkup($deprecatedGateway, $dataService);
}
if ($gatewayId === 'creditcard') {
$content .= $issuers;
Expand Down
17 changes: 0 additions & 17 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,16 @@
use Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
use Inpsyde\Modularity\Module\ServiceModule;
use Mollie\WooCommerce\BlockService\CheckoutBlockService;
use Mollie\WooCommerce\Buttons\ApplePayButton\AppleAjaxRequests;
use Mollie\WooCommerce\Buttons\ApplePayButton\ApplePayDirectHandler;
use Mollie\WooCommerce\Buttons\ApplePayButton\ResponsesToApple;
use Mollie\WooCommerce\Buttons\PayPalButton\DataToPayPal;
use Mollie\WooCommerce\Buttons\PayPalButton\PayPalAjaxRequests;
use Mollie\WooCommerce\Buttons\PayPalButton\PayPalButtonHandler;
use Mollie\WooCommerce\Gateway\Voucher\MaybeDisableGateway;
use Mollie\WooCommerce\Notice\AdminNotice;
use Mollie\WooCommerce\Notice\NoticeInterface;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\PaymentFieldsManager;
use Mollie\WooCommerce\PaymentMethods\IconFactory;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\Settings\Settings;
use Mollie\WooCommerce\Shared\Data;
use Mollie\WooCommerce\Shared\GatewaySurchargeHandler;
use Mollie\WooCommerce\Subscription\MollieSubscriptionGatewayHandler;
use Mollie\WooCommerce\PaymentMethods\Constants;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface as Logger;

class GatewayModule implements ServiceModule, ExecutableModule, ExtendingModule
{
Expand Down Expand Up @@ -357,15 +347,12 @@ protected function instantiatePaymentMethods($container): array
assert($settingsHelper instanceof Settings);
$surchargeService = $container->get(Surcharge::class);
assert($surchargeService instanceof Surcharge);
$paymentFieldsService = $container->get(PaymentFieldsManager::class);
assert($paymentFieldsService instanceof PaymentFieldsManager);
foreach ($listAllAvailablePaymentMethods as $paymentMethodAvailable) {
$paymentMethodId = $paymentMethodAvailable['id'];
$paymentMethods[$paymentMethodId] = $this->buildPaymentMethod(
$paymentMethodId,
$iconFactory,
$settingsHelper,
$paymentFieldsService,
$surchargeService,
$paymentMethodAvailable
);
Expand All @@ -378,7 +365,6 @@ protected function instantiatePaymentMethods($container): array
$paymentMethodId,
$iconFactory,
$settingsHelper,
$paymentFieldsService,
$surchargeService,
[]
);
Expand All @@ -389,7 +375,6 @@ protected function instantiatePaymentMethods($container): array
* @param string $id
* @param IconFactory $iconFactory
* @param Settings $settingsHelper
* @param PaymentFieldsManager $paymentFieldsService
* @param Surcharge $surchargeService
* @param array $paymentMethods
* @return PaymentMethodI | array
Expand All @@ -398,7 +383,6 @@ public function buildPaymentMethod(
string $id,
IconFactory $iconFactory,
Settings $settingsHelper,
PaymentFieldsManager $paymentFieldsService,
Surcharge $surchargeService,
array $apiMethod
) {
Expand All @@ -408,7 +392,6 @@ public function buildPaymentMethod(
$paymentMethod = new $paymentMethodClassName(
$iconFactory,
$settingsHelper,
$paymentFieldsService,
$surchargeService,
$apiMethod
);
Expand Down
19 changes: 9 additions & 10 deletions src/Gateway/inc/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
use Mollie\WooCommerce\PaymentMethods\InstructionStrategies\OrderInstructionsManager;
use Mollie\WooCommerce\Payment\PaymentCheckoutRedirectService;
use Mollie\WooCommerce\Payment\PaymentFactory;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\PaymentFieldsManager;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\DefaultFieldsStrategy;
use Mollie\WooCommerce\Payment\PaymentProcessor;
use Mollie\WooCommerce\PaymentMethods\Constants;
use Mollie\WooCommerce\PaymentMethods\Icon\GatewayIconsRenderer;
use Mollie\WooCommerce\PaymentMethods\IconFactory;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\NoopPaymentFieldsRenderer;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\PaymentFieldsRenderer;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;
use Mollie\WooCommerce\SDK\Api;
use Mollie\WooCommerce\SDK\HttpResponse;
Expand Down Expand Up @@ -155,11 +154,6 @@
OrderInstructionsManager::class => static function (): OrderInstructionsManager {
return new OrderInstructionsManager();
},
PaymentFieldsManager::class => static function (ContainerInterface $container): PaymentFieldsManager {
$data = $container->get('settings.data_helper');
assert($data instanceof Data);
return new PaymentFieldsManager($data);
},
PaymentCheckoutRedirectService::class => static function (
ContainerInterface $container
): PaymentCheckoutRedirectService {
Expand Down Expand Up @@ -428,15 +422,20 @@ static function ($filteredOption) {
$paymentMethods = $container->get('gateway.paymentMethods');
$methodId = substr($gatewayId, strrpos($gatewayId, '_') + 1);
$paymentMethod = $paymentMethods[$methodId];

$oldGatewayInstances = $container->get('__deprecated.gateway_helpers');
//not all payment methods have a gateway
if (!isset($oldGatewayInstances[$gatewayId])) {
return new NoopPaymentFieldsRenderer();
}
$deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId];
$gatewayDescription = $container->get('payment_gateway.' . $gatewayId . '.description');
return new PaymentFieldsRenderer($paymentMethod, $deprecatedGatewayHelper, $gatewayDescription);
$dataHelper = $container->get('settings.data_helper');
$deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId];
if (!$paymentMethod->getProperty('paymentFields')) {
return new DefaultFieldsStrategy($deprecatedGatewayHelper, $gatewayDescription, $dataHelper);
} else {
$className = 'Mollie\\WooCommerce\\PaymentMethods\\PaymentFieldsStrategies\\' . ucfirst($paymentMethod->getProperty('id')) . 'FieldsStrategy';
return class_exists($className) ? new $className($deprecatedGatewayHelper, $gatewayDescription, $dataHelper) : new DefaultFieldsStrategy($deprecatedGatewayHelper, $gatewayDescription, $dataHelper);
}
};

$dynamicServices["payment_gateway.$gatewayId.title"] = static function (ContainerInterface $container) use ($gatewayId) {
Expand Down
27 changes: 1 addition & 26 deletions src/PaymentMethods/AbstractPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Mollie\WooCommerce\PaymentMethods;

use Mollie\WooCommerce\Gateway\Surcharge;
use Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies\PaymentFieldsManager;
use Mollie\WooCommerce\Settings\Settings;
use Mollie\WooCommerce\Shared\SharedDataDictionary;

Expand All @@ -31,10 +30,7 @@ abstract class AbstractPaymentMethod implements PaymentMethodI
* @var Settings
*/
protected $settingsHelper;
/**
* @var PaymentFieldsManager
*/
protected $paymentFieldsService;

/**
* @var Surcharge
*/
Expand All @@ -47,15 +43,13 @@ abstract class AbstractPaymentMethod implements PaymentMethodI
public function __construct(
IconFactory $iconFactory,
Settings $settingsHelper,
PaymentFieldsManager $paymentFieldsService,
Surcharge $surcharge,
array $apiPaymentMethod
) {

$this->id = $this->getIdFromConfig();
$this->iconFactory = $iconFactory;
$this->settingsHelper = $settingsHelper;
$this->paymentFieldsService = $paymentFieldsService;
$this->surcharge = $surcharge;
$this->config = $this->getConfig();
$this->settings = $this->getSettings();
Expand Down Expand Up @@ -176,25 +170,6 @@ public function getAllFormFields()
return $this->getFormFields($this->getSharedFormFields());
}

/**
* Sets the gateway's payment fields strategy based on payment method
* @param $deprecatedHelperGateway
* @return string
*/
public function paymentFieldsStrategy($deprecatedHelperGateway, $gatewayDescription)
{
$this->paymentFieldsService->setStrategy($this);
return $this->paymentFieldsService->executeStrategy($deprecatedHelperGateway, $gatewayDescription);
}

/**
* @return PaymentFieldsManager
*/
public function paymentFieldsService(): PaymentFieldsManager
{
return $this->paymentFieldsService;
}

/**
* Access the payment method processed description, surcharge included
* @return mixed|string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;
use Inpsyde\PaymentGateway\PaymentGateway;
use Mollie\WooCommerce\Gateway\MolliePaymentGatewayHandler;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;
use Mollie\WooCommerce\Shared\Data;

class AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
protected MolliePaymentGatewayHandler $deprecatedHelperGateway;

protected string $gatewayDescription;
/**
* @var mixed
*/
protected Data $dataHelper;

public function __construct($deprecatedHelperGateway, $gateway, $dataHelper)
{

$this->deprecatedHelperGateway = $deprecatedHelperGateway;
$this->gatewayDescription = $gateway;
$this->dataHelper = $dataHelper;
}

/**
* @inheritDoc
*/
public function renderFields(): string
{
return '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class BancomatpayFieldsStrategy implements PaymentFieldsStrategyI
use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;

class BancomatpayFieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
const FIELD_PHONE = "billing_phone_bancomatpay";

public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper)
public function renderFields(): string
{
$showPhoneField = false;
$isPhoneRequired = get_option('mollie_wc_is_phone_required_flag');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class BillieFieldsStrategy implements PaymentFieldsStrategyI
use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;

class BillieFieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
const FIELD_COMPANY = "billing_company";

public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper): string
{
public function renderFields(): string {
$showCompanyField = false;

if (is_checkout_pay_page()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;
use Mollie\WooCommerce\PaymentMethods\PaymentMethodI;

class CreditcardFieldsStrategy implements PaymentFieldsStrategyI
class CreditcardFieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper): string
public function renderFields(): string
{
if (!$this->isMollieComponentsEnabled($deprecatedHelperGateway->paymentMethod())) {
if (!$this->isMollieComponentsEnabled($this->deprecatedHelperGateway->paymentMethod())) {
return '';
}
$deprecatedHelperGateway->has_fields = true;
$this->deprecatedHelperGateway->has_fields = true;
$allowedHtml = $this->svgAllowedHtml();

$output = '<div class="mollie-components"></div>';
Expand All @@ -23,8 +24,8 @@ public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelp
'%1$s Secure payments provided by %2$s',
'mollie-payments-for-woocommerce'
),
wp_kses($this->lockIcon($dataHelper), $allowedHtml),
wp_kses($this->mollieLogo($dataHelper), $allowedHtml)
wp_kses($this->lockIcon($this->dataHelper), $allowedHtml),
wp_kses($this->mollieLogo($this->dataHelper), $allowedHtml)
);
$output .= '</p>';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class DefaultFieldsStrategy implements PaymentFieldsStrategyI
use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;

class DefaultFieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper): string
public function renderFields(): string
{
return $gatewayDescription;
return $this->gatewayDescription;
}

public function getFieldMarkup($gateway, $dataHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class GiftcardFieldsStrategy implements PaymentFieldsStrategyI
use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;

class GiftcardFieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
use IssuersDropdownBehavior;

public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper): string
public function renderFields(): string
{
if (!$this->dropDownEnabled($deprecatedHelperGateway)) {
return $gatewayDescription;
if (!$this->dropDownEnabled($this->deprecatedHelperGateway)) {
return $this->gatewayDescription;
}

$issuers = $this->getIssuers($deprecatedHelperGateway, $dataHelper);
$issuers = $this->getIssuers($this->deprecatedHelperGateway, $this->dataHelper);
if (empty($issuers)) {
return $gatewayDescription;
return $this->gatewayDescription;
}
$selectedIssuer = $deprecatedHelperGateway->paymentMethod()->getSelectedIssuer();
$selectedIssuer = $this->deprecatedHelperGateway->paymentMethod()->getSelectedIssuer();

$html = '';

Expand All @@ -35,7 +37,7 @@ public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelp
return wpautop(wptexturize($html));
}

return $this->renderIssuers($deprecatedHelperGateway, $issuers, $selectedIssuer);
return $this->renderIssuers($this->deprecatedHelperGateway, $issuers, $selectedIssuer);
}

public function getFieldMarkup($gateway, $dataHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace Mollie\WooCommerce\PaymentMethods\PaymentFieldsStrategies;

class In3FieldsStrategy implements PaymentFieldsStrategyI
use Inpsyde\PaymentGateway\PaymentFieldsRendererInterface;

class In3FieldsStrategy extends AbstractPaymentFieldsRenderer implements PaymentFieldsRendererInterface
{
use PaymentFieldsStrategiesTrait;

const FIELD_BIRTHDATE = "billing_birthdate_in3";
const FIELD_PHONE = "billing_phone_in3";

public function execute($deprecatedHelperGateway, $gatewayDescription, $dataHelper): string
public function renderFields(): string
{
$showBirthdateField = false;
$showPhoneField = false;
Expand Down
Loading

0 comments on commit 0fd207a

Please sign in to comment.