-
Notifications
You must be signed in to change notification settings - Fork 55
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
base: develop
Are you sure you want to change the base?
Conversation
The SubscriptionModule was missing
There was a problem hiding this 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)) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
src/Gateway/OldGatewayBuilder.php
Outdated
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface as Logger; | ||
|
||
class OldGatewayBuilder |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
now uses callbacks to container objects namespaces were fixed dependencies fixed
Thankyou and instructions hooks out of deprecated helper
Now we send the description of the new gateway to the default strategy
src/Payment/PaymentProcessor.php
Outdated
public function setGateway($gateway) | ||
{ | ||
$this->gateway = $gateway; | ||
$this->deprecatedGatewayHelper = $gateway; | ||
} |
There was a problem hiding this comment.
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.
src/Payment/PaymentProcessor.php
Outdated
public function handleSubscriptions(\Inpsyde\PaymentGateway\PaymentGateway $paymentGateway, ?int $orderId): void | ||
{ | ||
if ($paymentGateway->supports('subscriptions')) { | ||
$this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment(); |
There was a problem hiding this comment.
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
src/Payment/PaymentProcessor.php
Outdated
$this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment(); | ||
$isSubscription = $this->dataHelper->isSubscription($orderId); | ||
if ($isSubscription) { | ||
$this->deprecatedGatewayHelper->isSubscriptionPayment = true; |
There was a problem hiding this comment.
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?
public const MAXIMAL_LENGHT_ADDRESS = 100; | ||
public const MAXIMAL_LENGHT_POSTALCODE = 20; | ||
public const MAXIMAL_LENGHT_CITY = 200; | ||
public const MAXIMAL_LENGHT_REGION = 200; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 thatexecuteStrategy
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.
# Conflicts: # src/Payment/MollieObject.php # tests/php/Functional/Payment/OrderRequestStrategyTest.php
Fix file names as per PSR-4
…-api' into task/PIWOO-473-refactor-payments-api
this is a middle step before removing the class
There was a problem hiding this 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
$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); |
There was a problem hiding this comment.
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.
$className = 'Mollie\\WooCommerce\\PaymentMethods\\PaymentFieldsStrategies\\' . ucfirst($paymentMethod->getProperty('id')) . 'FieldsStrategy'; | ||
return class_exists($className) ? new $className($deprecatedGatewayHelper, $gatewayDescription, $dataHelper) : new DefaultFieldsStrategy($deprecatedGatewayHelper, $gatewayDescription, $dataHelper); | ||
} |
There was a problem hiding this comment.
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.
$middlewareClass = 'Mollie\\WooCommerce\\Payment\\Request\\Middleware' . $field; | ||
if (class_exists($middlewareClass)) { | ||
$middleware = $this->container->get($middlewareClass); |
There was a problem hiding this comment.
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
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; | ||
} | ||
); |
There was a problem hiding this comment.
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
Handles PIWOO-473
It also adds the payment-gateway library and refactors part of the code base into new classes and folders