Skip to content

Commit

Permalink
Extract hooks to service
Browse files Browse the repository at this point in the history
Thankyou and instructions hooks out of deprecated helper
  • Loading branch information
mmaymo committed Jan 1, 2025
1 parent d374e5f commit 8a89466
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 122 deletions.
2 changes: 1 addition & 1 deletion mollie-payments-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Mollie Payments for WooCommerce
* Plugin URI: https://www.mollie.com
* Description: Accept payments in WooCommerce with the official Mollie plugin
* Version: 7.9.0-test
* Version: 7.9.0
* Author: Mollie
* Author URI: https://www.mollie.com
* Requires at least: 5.0
Expand Down
17 changes: 15 additions & 2 deletions src/Gateway/GatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function run(ContainerInterface $container): bool
$gateways = $maybeEnablegatewayHelper->maybeDisableBankTransferGateway($gateways);
return $maybeEnablegatewayHelper->maybeDisableMealVoucherGateway($gateways);
});
// Add subscription filters after payment gateways are loaded

add_filter(
'woocommerce_payment_gateways',
static function ($gateways) use ($container) {
Expand All @@ -80,16 +80,28 @@ static function ($gateways) use ($container) {
if (!$isMolliegateway) {
continue;
}

// Add subscription filters after payment gateways are loaded
$isSubscriptiongateway = $gateway->supports('subscriptions');
if ($isSubscriptiongateway) {
$deprecatedGatewayHelpers[$gateway->id]->addSubscriptionFilters($gateway);
}

// Add payment instructions
$displayInstructionsService = $container->get('gateway.hooks.displayInstructions');
$displayInstructionsService($gateway);
// Add thankyou page actions for gateway
$thankyouPageService = $container->get('gateway.hooks.thankyouPage');
if (!has_action('woocommerce_thankyou_' . $gateway->id)) {
$thankyouPageService($gateway);
}

}
return $gateways;
},
30
);


// Disable SEPA as payment option in WooCommerce checkout
add_filter(
'woocommerce_available_payment_gateways',
Expand Down Expand Up @@ -184,6 +196,7 @@ static function ($paymentContext) {
return $fields;
}, 10, 3);


return true;
}

Expand Down
116 changes: 1 addition & 115 deletions src/Gateway/MolliePaymentGatewayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MolliePaymentGatewayHandler
/**
* @var PaymentMethodI
*/
protected $paymentMethod;
public $paymentMethod;
/**
* @var string
*/
Expand Down Expand Up @@ -117,24 +117,6 @@ public function __construct(
// Use gateway class name as gateway id
$this->gatewayId();

if (!has_action('woocommerce_thankyou_' . $this->id)) {
add_action(
'woocommerce_thankyou_' . $this->id,
[$this, 'thankyou_page']
);
}
add_action(
'woocommerce_email_after_order_table',
[$this, 'displayInstructions'],
10,
3
);
add_action(
'woocommerce_email_order_meta',
[$this, 'displayInstructions'],
10,
3
);
$this->mollieOrderService->setGateway($this);

add_action(
Expand Down Expand Up @@ -426,102 +408,6 @@ public function activePaymentObject($orderId, $useCache): Payment
return $activePaymentObject;
}

/**
* Output for the order received page.
*/
public function thankyou_page($order_id)
{
$order = wc_get_order($order_id);

// Order not found
if (!$order) {
return;
}

// Empty cart
if (WC()->cart) {
WC()->cart->empty_cart();
}

// Same as email instructions, just run that
$this->displayInstructions(
$order,
$admin_instructions = false,
$plain_text = false
);
}

/**
* Add content to the WC emails.
*
* @param WC_Order $order
* @param bool $admin_instructions (default: false)
* @param bool $plain_text (default: false)
*
* @return void
*/
public function displayInstructions(
WC_Order $order,
$admin_instructions = false,
$plain_text = false
) {

if (
($admin_instructions && !$this::$alreadyDisplayedAdminInstructions)
|| (!$admin_instructions && !$this::$alreadyDisplayedCustomerInstructions)
) {
$order_payment_method = $order->get_payment_method();

// Invalid gateway
if ($this->id !== $order_payment_method) {
return;
}

$payment = $this->paymentObject()->getActiveMolliePayment(
$order->get_id()
);

// Mollie payment not found or invalid gateway
if (
!$payment
|| $payment->method !== $this->paymentMethod->getProperty('id')
) {
return;
}
$this->orderInstructionsManager->setStrategy($this);
$instructions = $this->orderInstructionsManager->executeStrategy(
$this,
$payment,
$order,
$admin_instructions
);

if (!empty($instructions)) {
$instructions = wptexturize($instructions);
//save instructions in order meta
$order->update_meta_data(
'_mollie_payment_instructions',
$instructions
);
$order->save();

if ($plain_text) {
echo esc_html($instructions) . PHP_EOL;
} else {
echo '<section class="woocommerce-order-details woocommerce-info mollie-instructions" >';
echo wp_kses(wpautop($instructions), ['p' => [], 'strong' => [], 'br' => []]) . PHP_EOL;
echo '</section>';
}
}
}
if ($admin_instructions && !$this::$alreadyDisplayedAdminInstructions) {
$this::$alreadyDisplayedAdminInstructions = true;
}
if (!$admin_instructions && !$this::$alreadyDisplayedCustomerInstructions) {
$this::$alreadyDisplayedCustomerInstructions = true;
}
}

/**
* @param $title
* @param null $id
Expand Down
75 changes: 73 additions & 2 deletions src/Gateway/inc/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Dhii\Services\Factory;
use Inpsyde\PaymentGateway\PaymentGateway;
use Inpsyde\PaymentGateway\PaymentRequestValidatorInterface;
use Inpsyde\PaymentGateway\RefundProcessorInterface;
use Mollie\WooCommerce\Buttons\ApplePayButton\AppleAjaxRequests;
Expand Down Expand Up @@ -267,7 +268,7 @@
return $paymentMethods[$methodId];
};
},
'gateway.subscriptionHooks' => static function (): array {
'gateway.subscriptionsSupports' => static function (): array {
return [
'subscriptions',
'subscription_cancellation',
Expand All @@ -281,6 +282,76 @@
'subscription_payment_method_change_customer',
];
},
'gateway.hooks.thankyouPage' => static function (ContainerInterface $container) {
return static function (PaymentGateway $paymentGateway) use ($container) {
$instructionsManager = $container->get(OrderInstructionsManager::class);
$oldGatewayInstances = $container->get('__deprecated.gateway_helpers');
$gatewayId = $paymentGateway->id;
$deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId];
add_action(
'woocommerce_thankyou_' . $paymentGateway->id,
function ($order_id) use ($instructionsManager, $paymentGateway, $deprecatedGatewayHelper) {
$order = wc_get_order($order_id);

// Order not found
if (!$order) {
return;
}

// Empty cart
if (WC()->cart) {
WC()->cart->empty_cart();
}

// Same as email instructions, just run that
$instructionsManager->displayInstructions(
$paymentGateway,
$deprecatedGatewayHelper,
$order,
false,
false
);
}
);
};
},
'gateway.hooks.displayInstructions' => static function (ContainerInterface $container) {
return static function (PaymentGateway $paymentGateway) use ($container) {
$instructionsManager = $container->get(OrderInstructionsManager::class);
$oldGatewayInstances = $container->get('__deprecated.gateway_helpers');
$gatewayId = $paymentGateway->id;
$deprecatedGatewayHelper = $oldGatewayInstances[$gatewayId];
add_action(
'woocommerce_email_after_order_table',
function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $paymentGateway, $deprecatedGatewayHelper) {
$instructionsManager->displayInstructions(
$paymentGateway,
$deprecatedGatewayHelper,
$order,
$sent_to_admin,
$plain_text
);
},
10,
3
);

add_action(
'woocommerce_email_order_meta',
function ($order, $sent_to_admin, $plain_text) use ($instructionsManager, $paymentGateway, $deprecatedGatewayHelper) {
$instructionsManager->displayInstructions(
$paymentGateway,
$deprecatedGatewayHelper,
$order,
$sent_to_admin,
$plain_text
);
},
10,
3
);
};
},

];
$paymentMethods = SharedDataDictionary::GATEWAY_CLASSNAMES;
Expand Down Expand Up @@ -392,7 +463,7 @@ static function (array $gatewayInstances) use ($gatewayId): callable {
$supports = $paymentMethod->getProperty('supports');
$isSepa = $paymentMethod->getProperty('SEPA') === true;
$isSubscription = $paymentMethod->getProperty('Subscription') === true;
$subscriptionHooks = $container->get('gateway.subscriptionHooks');
$subscriptionHooks = $container->get('gateway.subscriptionsSupports');
if ($isSepa || $isSubscription) {
$supports = array_merge($supports, $subscriptionHooks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

namespace Mollie\WooCommerce\PaymentMethods\InstructionStrategies;

use WC_Order;

class OrderInstructionsManager
{
protected $strategy;
protected static $alreadyDisplayedAdminInstructions = false;
protected static $alreadyDisplayedCustomerInstructions = false;

public function setStrategy($deprecatedGatewayHelper)
{
if (!$deprecatedGatewayHelper->paymentMethod()->getProperty('instructions')) {
Expand All @@ -18,12 +23,84 @@ public function setStrategy($deprecatedGatewayHelper)
}

public function executeStrategy(
$deprecatedGatewayHelper,
$paymentGateway,
$payment,
$order = null,
$admin_instructions = false
) {

return $this->strategy->execute($deprecatedGatewayHelper, $payment, $order, $admin_instructions);
return $this->strategy->execute($paymentGateway, $payment, $order, $admin_instructions);
}

/**
* Add content to the WC emails.
*
* @param WC_Order $order
* @param bool $admin_instructions (default: false)
* @param bool $plain_text (default: false)
*
* @return void
*/
public function displayInstructions(
$paymentGateway,
$deprecatedGatewayHelper,
WC_Order $order,
$admin_instructions = false,
$plain_text = false
) {
if (
($admin_instructions && !self::$alreadyDisplayedAdminInstructions)
|| (!$admin_instructions && !self::$alreadyDisplayedCustomerInstructions)
) {
$order_payment_method = $order->get_payment_method();

// Invalid gateway
if ($deprecatedGatewayHelper->id !== $order_payment_method) {
return;
}

$payment = $deprecatedGatewayHelper->paymentObject()->getActiveMolliePayment(
$order->get_id()
);

// Mollie payment not found or invalid gateway
if (
!$payment
|| $payment->method !== $deprecatedGatewayHelper->paymentMethod->getProperty('id')
) {
return;
}
$this->setStrategy($deprecatedGatewayHelper);
$instructions = $this->executeStrategy(
$paymentGateway,
$payment,
$order,
$admin_instructions
);

if (!empty($instructions)) {
$instructions = wptexturize($instructions);
//save instructions in order meta
$order->update_meta_data(
'_mollie_payment_instructions',
$instructions
);
$order->save();

if ($plain_text) {
echo esc_html($instructions) . PHP_EOL;
} else {
echo '<section class="woocommerce-order-details woocommerce-info mollie-instructions" >';
echo wp_kses(wpautop($instructions), ['p' => [], 'strong' => [], 'br' => []]) . PHP_EOL;
echo '</section>';
}
}
}
if ($admin_instructions && !self::$alreadyDisplayedAdminInstructions) {
self::$alreadyDisplayedAdminInstructions = true;
}
if (!$admin_instructions && !self::$alreadyDisplayedCustomerInstructions) {
self::$alreadyDisplayedCustomerInstructions = true;
}
}
}

0 comments on commit 8a89466

Please sign in to comment.