Skip to content

Commit

Permalink
Unlink payment processor from deprecated helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Jan 2, 2025
1 parent bb97562 commit 51a6ffa
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 97 deletions.
5 changes: 0 additions & 5 deletions src/Gateway/DeprecatedGatewayBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container):
assert($logger instanceof Logger);
$notice = $container->get(FrontendNotice::class);
assert($notice instanceof FrontendNotice);
$paymentProcessor = $container->get(PaymentProcessor::class);
assert($paymentProcessor instanceof PaymentProcessor);
$mollieOrderService = $container->get(MollieOrderService::class);
assert($mollieOrderService instanceof MollieOrderService);
$HttpResponseService = $container->get('SDK.HttpResponse');
Expand Down Expand Up @@ -62,7 +60,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container):
$gateways[$key] = new MollieSepaRecurringGatewayHandler(
$directDebit,
$paymentMethod,
$paymentProcessor,
$orderInstructionsManager,
$mollieOrderService,
$data,
Expand All @@ -78,7 +75,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container):
} elseif ($paymentMethod->getProperty('Subscription')) {
$gateways[$key] = new MollieSubscriptionGatewayHandler(
$paymentMethod,
$paymentProcessor,
$orderInstructionsManager,
$mollieOrderService,
$data,
Expand All @@ -94,7 +90,6 @@ public function instantiatePaymentMethodGateways(ContainerInterface $container):
} else {
$gateways[$key] = new MolliePaymentGatewayHandler(
$paymentMethod,
$paymentProcessor,
$orderInstructionsManager,
$mollieOrderService,
$data,
Expand Down
4 changes: 3 additions & 1 deletion src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ static function ($gateways) use ($container) {
if (!has_action('woocommerce_thankyou_' . $gateway->id)) {
$thankyouPageService($gateway);
}

// Add subscription payment hooks
$isSubscriptionPaymentService = $container->get('gateway.hooks.isSubscriptionPayment');
$isSubscriptionPaymentService($gateway);
}
return $gateways;
},
Expand Down
8 changes: 0 additions & 8 deletions src/Gateway/MolliePaymentGatewayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class MolliePaymentGatewayHandler
*/
public function __construct(
PaymentMethodI $paymentMethod,
PaymentProcessor $paymentProcessor,
OrderInstructionsManager $orderInstructionsProcessor,
MollieOrderService $mollieOrderService,
Data $dataService,
Expand All @@ -105,7 +104,6 @@ public function __construct(
$this->paymentMethod = $paymentMethod;
$this->logger = $logger;
$this->notice = $notice;
$this->paymentProcessor = $paymentProcessor;
$this->orderInstructionsManager = $orderInstructionsProcessor;
$this->mollieOrderService = $mollieOrderService;
$this->httpResponse = $httpResponse;
Expand Down Expand Up @@ -141,19 +139,13 @@ public function __construct(
if ($this->enabled === 'yes' && $this->paymentMethod->getProperty('filtersOnBuild')) {
$this->paymentMethod->filtersOnBuild();
}
//$this->refundProcessor = new RefundProcessor($this);
}

public function paymentMethod(): PaymentMethodI
{
return $this->paymentMethod;
}

public function paymentService()
{
return $this->paymentProcessor;
}

public function dataService()
{
return $this->dataService;
Expand Down
41 changes: 35 additions & 6 deletions src/Gateway/inc/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
$paymentCheckoutRedirectService = $container->get(PaymentCheckoutRedirectService::class);
assert($paymentCheckoutRedirectService instanceof PaymentCheckoutRedirectService);
$voucherDefaultCategory = $container->get('voucher.defaultCategory');
return new PaymentProcessor($notice, $logger, $paymentFactory, $data, $api, $settings, $pluginId, $paymentCheckoutRedirectService, $voucherDefaultCategory);
$deprecatedGatewayInstances = $container->get('__deprecated.gateway_helpers');
return new PaymentProcessor($notice, $logger, $paymentFactory, $data, $api, $settings, $pluginId, $paymentCheckoutRedirectService, $voucherDefaultCategory, $deprecatedGatewayInstances);
},
OrderInstructionsManager::class => static function (): OrderInstructionsManager {
return new OrderInstructionsManager();
Expand Down Expand Up @@ -352,6 +353,38 @@ function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $payme
);
};
},
'gateway.hooks.isSubscriptionPayment' => static function (ContainerInterface $container) {
return static function (PaymentGateway $paymentGateway) use ($container) {
$pluginId = $container->get('shared.plugin_id');
$dataHelper = $container->get('settings.data_helper');
if ($paymentGateway->supports('subscriptions')) {
add_filter(
$pluginId . '_is_subscription_payment',
function ($isSubscription, $orderId) use ($pluginId, $dataHelper) {
if ($dataHelper->isWcSubscription($orderId)) {
add_filter(
$pluginId . '_is_automatic_payment_disabled',
static function ($filteredOption) {
if (
'yes' == get_option(
\WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments'
)
) {
return true;
}
return $filteredOption;
}
);
return true;
}
return $isSubscription;
},
10,
2
);
}
};
},

];
$paymentMethods = SharedDataDictionary::GATEWAY_CLASSNAMES;
Expand All @@ -364,11 +397,7 @@ function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $payme
return $container->get('payment_gateways.noop_payment_request_validator');
};
$dynamicServices["payment_gateway.$gatewayId.payment_processor"] = static function (ContainerInterface $container) use ($gatewayId): PaymentProcessor {
$oldGatewayInstances = $container->get('__deprecated.gateway_helpers');
$deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId];
$paymentProcessor = $container->get(PaymentProcessor::class);
$paymentProcessor->setGateway($deprecatedGatewayHelper);
return $paymentProcessor;
return $container->get(PaymentProcessor::class);
};
$dynamicServices["payment_gateway.$gatewayId.refund_processor"] = static function (ContainerInterface $container) use ($gatewayId): RefundProcessorInterface {
$getProperty = $container->get('gateway.getMethodPropertyByGatewayId');
Expand Down
58 changes: 25 additions & 33 deletions src/Payment/PaymentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mollie\WooCommerce\Payment;

use Inpsyde\PaymentGateway\PaymentGateway;
use Inpsyde\PaymentGateway\PaymentProcessorInterface;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Resources\Payment;
Expand Down Expand Up @@ -53,6 +54,8 @@ class PaymentProcessor implements PaymentProcessorInterface
* @var string
*/
protected $voucherDefaultCategory;
private PaymentGateway $gateway;
private array $deprecatedGatewayInstances;

/**
* PaymentProcessor constructor.
Expand All @@ -66,7 +69,8 @@ public function __construct(
Settings $settingsHelper,
string $pluginId,
PaymentCheckoutRedirectService $paymentCheckoutRedirectService,
string $voucherDefaultCategory
string $voucherDefaultCategory,
array $deprecatedGatewayInstances
) {

$this->notice = $notice;
Expand All @@ -78,17 +82,23 @@ public function __construct(
$this->pluginId = $pluginId;
$this->paymentCheckoutRedirectService = $paymentCheckoutRedirectService;
$this->voucherDefaultCategory = $voucherDefaultCategory;
$this->deprecatedGatewayInstances = $deprecatedGatewayInstances;
}

public function setGateway($gateway)
public function setGatewayHelper(string $paymentGatewayId)
{
$this->deprecatedGatewayHelper = $gateway;
$this->deprecatedGatewayHelper = $this->deprecatedGatewayInstances[$paymentGatewayId];
}
public function setGateway(PaymentGateway $gateway)
{
$this->gateway = $gateway;
}

public function processPayment($order, $paymentGateway): array
{
$orderId = $order->get_id();
$this->handleSubscriptions($paymentGateway, $orderId);
$this->setGateway($paymentGateway);
$this->setGatewayHelper($paymentGateway->id);

$redirectUrl = $paymentGateway->get_return_url($order);
$paymentMethod = $this->deprecatedGatewayHelper->paymentMethod();
Expand Down Expand Up @@ -339,7 +349,7 @@ protected function processAsMollieOrder(
$data = array_filter($paymentRequestData);

$data = apply_filters(
'woocommerce_' . $this->deprecatedGatewayHelper->id . '_args',
'woocommerce_' . $this->gateway->id . '_args',
$data,
$order
);
Expand Down Expand Up @@ -465,12 +475,12 @@ protected function processAsMolliePayment(
$data = array_filter($paymentRequestData);

$data = apply_filters(
'woocommerce_' . $this->deprecatedGatewayHelper->id . '_args',
'woocommerce_' . $this->gateway->id . '_args',
$data,
$order
);
$data = apply_filters(
'woocommerce_' . $this->deprecatedGatewayHelper->id . 'payment_args',
'woocommerce_' . $this->gateway->id . 'payment_args',
$data,
$order
);
Expand Down Expand Up @@ -527,18 +537,16 @@ protected function processPaymentForMollie(
$customer_id,
$apiKey
) {

$paymentMethod = $this->deprecatedGatewayHelper->paymentMethod();
//
// PROCESS REGULAR PAYMENT AS MOLLIE ORDER
//
if ($molliePaymentType === self::PAYMENT_METHOD_TYPE_ORDER) {
// if the capture is set to manual, and this is a credit card payment, we need to create a payment instead of an order
$captureType = get_option('mollie-payments-for-woocommerce_place_payment_onhold');

if ($captureType === 'later_capture' && $this->deprecatedGatewayHelper->id === 'mollie_wc_gateway_creditcard') {
if ($captureType === 'later_capture' && $this->gateway->id === 'mollie_wc_gateway_creditcard') {
$this->logger->debug(
"{$this->deprecatedGatewayHelper->id}: Create payment for order {$orderId} capture set to manual",
"{$this->gateway->id}: Create payment for order {$orderId} capture set to manual",
[true]
);

Expand All @@ -550,7 +558,7 @@ protected function processPaymentForMollie(
return $paymentObject;
}
$this->logger->debug(
"{$this->deprecatedGatewayHelper->id}: Create Mollie payment object for order {$orderId}",
"{$this->gateway->id}: Create Mollie payment object for order {$orderId}",
[true]
);

Expand Down Expand Up @@ -642,7 +650,7 @@ public function updateOrderStatus(\WC_Order $order, $new_status, $note = '', $re
protected function noValidMandateForSubsSwitchFailure($orderId): void
{
$this->logger->debug(
$this->deprecatedGatewayHelper->id . ': Subscription switch failed, no valid mandate for order #' . $orderId
$this->gateway->id . ': Subscription switch failed, no valid mandate for order #' . $orderId
);
$this->notice->addNotice(
'error',
Expand All @@ -669,11 +677,11 @@ protected function subsSwitchCompleted($order): array
)
);

$this->logger->debug($this->deprecatedGatewayHelper->id . ': Subscription switch completed, valid mandate for order #' . $order->get_id());
$this->logger->debug($this->gateway->id . ': Subscription switch completed, valid mandate for order #' . $order->get_id());

return [
'result' => 'success',
'redirect' => $this->deprecatedGatewayHelper->get_return_url($order),
'redirect' => $this->gateway->get_return_url($order),
];
}

Expand All @@ -691,7 +699,7 @@ protected function processValidMandate($order, ?string $customerId, $apiKey): bo
);
$paymentRequestData = $paymentObject->getPaymentRequestData($order, $customerId);
$data = array_filter($paymentRequestData);
$data = apply_filters('woocommerce_' . $this->deprecatedGatewayHelper->id . '_args', $data, $order);
$data = apply_filters('woocommerce_' . $this->gateway->id . '_args', $data, $order);

$mandates = $this->apiHelper->getApiClient($apiKey)->customers->get($customerId)->mandates();
$validMandate = false;
Expand All @@ -711,7 +719,7 @@ protected function processSubscriptionSwitch(WC_Order $order, int $orderId, ?str
// PROCESS SUBSCRIPTION SWITCH - If this is a subscription switch and customer has a valid mandate, process the order internally
//
try {
$this->logger->debug($this->deprecatedGatewayHelper->id . ': Subscription switch started, fetching mandate(s) for order #' . $orderId);
$this->logger->debug($this->gateway->id . ': Subscription switch started, fetching mandate(s) for order #' . $orderId);
$validMandate = $this->processValidMandate($order, $customerId, $apiKey);
if ($validMandate) {
return $this->subsSwitchCompleted($order);
Expand Down Expand Up @@ -925,20 +933,4 @@ public function handleMollieFraudRejection(ApiException $e): void
);
}
}

/**
* @param \Inpsyde\PaymentGateway\PaymentGateway $paymentGateway
* @param int|null $orderId
* @return void
*/
public function handleSubscriptions(\Inpsyde\PaymentGateway\PaymentGateway $paymentGateway, ?int $orderId): void
{
if ($paymentGateway->supports('subscriptions')) {
$this->deprecatedGatewayHelper->addWcSubscriptionsFiltersForPayment();
$isSubscription = $this->dataHelper->isSubscription($orderId);
if ($isSubscription) {
$this->deprecatedGatewayHelper->isSubscriptionPayment = true;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Payment/Request/Decorators/StoreCustomerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function decorate(array $requestData, WC_Order $order, $context = null):
$storeCustomer = $this->settingsHelper->shouldStoreCustomer();
if (!$storeCustomer) {
if ($context === 'order') {
unset($requestData['payment']['customerId']);
unset($requestData['payment']['customerId']);
} elseif ($context === 'payment') {
unset($requestData['customerId']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Payment/Request/Strategies/OrderRequestStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function createRequest(WC_Order $order, $customerId): array
'orderNumber' => $order->get_order_number(),
'payment' => [
'customerId' => $customerId,
]
],
];

$context = 'order';
Expand Down
2 changes: 0 additions & 2 deletions src/Subscription/MollieSepaRecurringGatewayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class MollieSepaRecurringGatewayHandler extends MollieSubscriptionGatewayHandler
public function __construct(
PaymentMethodI $directDebitPaymentMethod,
PaymentMethodI $paymentMethod,
PaymentProcessor $paymentProcessor,
OrderInstructionsManager $orderInstructionsService,
MollieOrderService $mollieOrderService,
Data $dataService,
Expand All @@ -52,7 +51,6 @@ public function __construct(

parent::__construct(
$paymentMethod,
$paymentProcessor,
$orderInstructionsService,
$mollieOrderService,
$dataService,
Expand Down
Loading

0 comments on commit 51a6ffa

Please sign in to comment.