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

Task/piwoo 473 refactor payments api #969

Open
wants to merge 57 commits into
base: develop
Choose a base branch
from

Conversation

mmaymo
Copy link
Collaborator

@mmaymo mmaymo commented Dec 17, 2024

Handles PIWOO-473
It also adds the payment-gateway library and refactors part of the code base into new classes and folders

@mmaymo mmaymo requested a review from Biont December 17, 2024 07:54
Copy link
Collaborator

@Biont Biont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual payment gateway library is not present in this branch, right?

@@ -47,7 +50,7 @@ public function maybeDisableMealVoucherGateway(?array $gateways): array
}
$mealVoucherGatewayIndex = false;
foreach ($gateways as $key => $gateway) {
if (!($gateway instanceof MolliePaymentGateway)) {
if (!($gateway instanceof PaymentGateway)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it would be wiser to match against a list of $gateway->id here.
The instanceof check might cause trouble if mollie is used among another gateway that uses our library.

@@ -25,7 +25,7 @@
use WC_Payment_Gateway;
use WP_Error;

class MolliePaymentGateway extends WC_Payment_Gateway implements MolliePaymentGatewayI
class MolliePaymentGatewayHandler extends WC_Payment_Gateway implements MolliePaymentGatewayI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I gather, you no longer need to extend WC_Payment_Gateway here, right? This class is being reduced to "a bucket of utility functions" that are in the process of being refactored to a more fitting place...?

In other words, this is no longer acting as a payment gateway by itself..?
Just for my understanding :)

$amount,
$reason
);
return $this->refundProcessor->process_refund($order_id, $amount, $reason);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->refundProcessor is never set and RefundProcessorInterface does not have a process_refund method, but a refundOrderPayment.

It also seems to contradict my earlier question about whether this is now a utility class (if it actively carries out refunds instead of aiding with it I mean)

Is this dead code?

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface as Logger;

class OldGatewayBuilder
Copy link
Collaborator

@Biont Biont Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how this interacts with the payment gateway library?
It is used by the gateway.instances service (as opposed to the payment_gateways service).
If I am still correct in my assumption that MolliePaymentGatewayI is mostly defining a "common toolbox until everything is refactored", then it might make sense to avoid confusion in the service/classnames.

Perhaps a __deprecated.gateway_helpers => fn() => new DeprecatedGatewayHelper() would work wonders giving clarity about the nature of things and the intent behind them.

In two years, it might be hard to dissect that this is a middle step during refactoring.

@@ -26,7 +27,7 @@
use Mollie\WooCommerce\PaymentMethods\Constants;
use WC_Order;

class MollieSubscriptionGateway extends MolliePaymentGateway
class MollieSubscriptionGatewayHandler extends MolliePaymentGatewayHandler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a little dangerous to me

This class implements WC_Payment_Gateway and sets up required hooks for $this->scheduled_subscription_payment() and others.

Some of these contain calls to the parent:: method, for example process_subscription_payment and process_payment

As a consequence, these methods do not go through the service locator and the API of the payment gateway library, but rather through the (assumed deprecated) old implementation. Am I seeing this correctly?

Maybe this is all dead code (the WC_Payment_Gateway methods I mean) that you haven't removed yet, though. Removing this in the next step would help me see clearer.

Comment on lines 83 to 86
public function setGateway($gateway)
{
$this->gateway = $gateway;
$this->deprecatedGatewayHelper = $gateway;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be removed. It is only called by MollieSubscriptionGatewayHandler, but I could not find any usages of $this->paymentProcessor nor $this->paymentService() in that class - nor in the MolliePaymentGatewayHandler base class.

public function handleSubscriptions(\Inpsyde\PaymentGateway\PaymentGateway $paymentGateway, ?int $orderId): void
{
if ($paymentGateway->supports('subscriptions')) {
$this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think such filters should be registered globally during bootstrap, not during an individual payment

$this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment();
$isSubscription = $this->dataHelper->isSubscription($orderId);
if ($isSubscription) {
$this->deprecatedGatewayHelper->isSubscriptionPayment = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not find usaged of $isSubscriptionPayment. Is this even needed any longer?

Comment on lines 12 to 15
public const MAXIMAL_LENGHT_ADDRESS = 100;
public const MAXIMAL_LENGHT_POSTALCODE = 20;
public const MAXIMAL_LENGHT_CITY = 200;
public const MAXIMAL_LENGHT_REGION = 200;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling ("LENGTH")

{
private $dataHelper;
private $settingsHelper;
private array $decorators;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper type hints so the usage of RequestDecoratorInterface is found by the IDE


$context = 'payment';
foreach ($this->decorators as $decorator) {
$requestData = $decorator->decorate($requestData, $order, $context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the term "decorator" is not the best fit for what you are doing here.
A decorator is applied to objects in order to transparently (->invisible from the outside) modify their behaviour without changing any code.

Here you are building/modifying scalar data (an array) with a structured approach. Maybe it is the middleware pattern you are looking for? - it would also give you two entrypoints for modification (before & after) so that one middleware can make changes to another middleware's output.


class PaymentFieldsService
class PaymentFieldsManager
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I do not think there should be a public function setStrategy, allowing modifying the internal state from the outside. That sounds like a ticking timebomb, because there is no guarantee that executeStrategy yields the expected outcome when it can be changed from the outside at any moment.

  • It also seems that there is a 1:1 relation between payment method and payment fields, so the option to (re)set a specific strategy does not seem needed at all, this can be configured statically.

  • The dynamic instantiation based on $paymentMethod->id is pretty limiting - there is no way to pass constructor arguments and/or modify the returned instance in any way. Would it not be much more flexible if we could just delegate to the DI container here?

  • If there is a 1:1 mapping from payment method to fields strategy, why are we not simply defining many PaymentFieldsRenderer objects to begin with? Instead, we have a one-size-fits-all renderer that pipes all handling through this strategy manager. I think it would be better to simply define multiple 'payment_gateway.<METHOD_ID>.payment_fields_renderer' services. Is there anything that blocked you from going with this approach? Let me know if there's anything missing in the library.

@mmaymo mmaymo marked this pull request as ready for review January 7, 2025 14:59
@mmaymo mmaymo requested review from danielhuesken and Biont January 7, 2025 14:59
Copy link
Collaborator

@Biont Biont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see some room for improving a few details, but I don't think it's worth blocking a merge for them. See comments

Comment on lines +130 to +136
$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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find piece with this dynamic classname generation. It is not very flexible because it limits our naming options and forces all possible classes to come from a single namespace - and as a result, fom one single folder in our plugin.

If we would maintain a classmap in an external service like this:

$fieldStrategy = [
  'billie' => BillieFieldsStrategy::class,
  'riverty' => RivertyFieldsStrategy::class,
  // more...
];

then we could just check if an entry exists in the array and we would no longer be tied to a single PSR-4 location: Strategies could be supplied by other modules. At the same time, we'd no longer be forced to follow a single naming convention.

Comment on lines +436 to +438
$className = 'Mollie\\WooCommerce\\PaymentMethods\\PaymentFieldsStrategies\\' . ucfirst($paymentMethod->getProperty('id')) . 'FieldsStrategy';
return class_exists($className) ? new $className($deprecatedGatewayHelper, $gatewayDescription, $dataHelper) : new DefaultFieldsStrategy($deprecatedGatewayHelper, $gatewayDescription, $dataHelper);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we're even duplicating code here. A factory working off the aforementioned classmap/dictionary could be reused here.

Comment on lines +51 to +53
$middlewareClass = 'Mollie\\WooCommerce\\Payment\\Request\\Middleware' . $field;
if (class_exists($middlewareClass)) {
$middleware = $this->container->get($middlewareClass);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this doing anything? Looks it can only match MiddlewareHandler, but that does not feel intentional. Maybe I just don't understand what this is doing and how it works

Comment on lines +55 to +65
return array_reduce(
array_reverse($middlewares),
static function ($next, $middleware) {
return static function ($requestData, $order, $context) use ($middleware, $next) {
return $middleware($requestData, $order, $context, $next);
};
},
static function ($requestData) {
return $requestData;
}
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does psalm accept this? I feel this could really use some more typehints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants