From c9aa5a9014a16e234508ac12201bfd283f631acb Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 2 Nov 2023 11:04:35 +0100 Subject: [PATCH 1/2] Feature flag for MerchantCaptureModule. --- inc/settings/mollie_advanced_settings.php | 41 +---- src/MerchantCapture/MerchantCaptureModule.php | 145 ++++++++++-------- src/MerchantCapture/MollieCaptureSettings.php | 50 ++++++ 3 files changed, 140 insertions(+), 96 deletions(-) create mode 100644 src/MerchantCapture/MollieCaptureSettings.php diff --git a/inc/settings/mollie_advanced_settings.php b/inc/settings/mollie_advanced_settings.php index e4026f49..712914c1 100644 --- a/inc/settings/mollie_advanced_settings.php +++ b/inc/settings/mollie_advanced_settings.php @@ -17,7 +17,7 @@ '{customer.company}' => _x('Customer\'s company name', 'Label {customer.company} description for payment description options', 'mollie-payments-for-woocommerce'), ]; -return [ +$mollieAdvancedSettings = [ [ 'id' => $pluginName . '_title', 'title' => __('Mollie advanced settings', 'mollie-payments-for-woocommerce'), @@ -198,39 +198,8 @@ class="mollie-settings-advanced-payment-desc-label button button-secondary butto 'desc' => __("Remove options and scheduled actions from database when uninstalling the plugin.", "mollie-payments-for-woocommerce") . ' (' . strtolower( __('Clear now', 'mollie-payments-for-woocommerce') ) . ')', - ], - [ - 'id' => $pluginName . '_place_payment_onhold', - 'title' => __('Placing payments on Hold', 'mollie-payments-for-woocommerce'), - 'type' => 'select', - 'desc_tip' => true, - 'options' => [ - 'immediate_capture' => __('Capture payments immediately', 'mollie-payments-for-woocommerce'), - 'later_capture' => __('Authorize payments for a later capture', 'mollie-payments-for-woocommerce'), - ], - 'default' => 'immediate_capture', - 'desc' => sprintf( - __( - 'Authorized payment can be captured or voided by changing the order status instead of doing it manually.', - 'mollie-payments-for-woocommerce' - ) - ), - ], - [ - 'id' => $pluginName . '_capture_or_void', - 'title' => __( - 'Capture or void on status change', - 'mollie-payments-for-woocommerce' - ), - 'type' => 'checkbox', - 'default' => 'no', - 'desc' => __( - 'Capture authorized payments automatically when setting the order status to Processing or Completed. Void the payment by setting the order status Canceled.', - 'mollie-payments-for-woocommerce' - ), - ], - [ - 'id' => $pluginName . '_sectionend', - 'type' => 'sectionend', - ], + ] ]; + + +return apply_filters('inpsyde.mollie-advanced-settings', $mollieAdvancedSettings, $pluginName); diff --git a/src/MerchantCapture/MerchantCaptureModule.php b/src/MerchantCapture/MerchantCaptureModule.php index 4d028857..9232061c 100644 --- a/src/MerchantCapture/MerchantCaptureModule.php +++ b/src/MerchantCapture/MerchantCaptureModule.php @@ -125,70 +125,95 @@ public function services(): array public function run(ContainerInterface $container): bool { - $pluginId = $container->get('shared.plugin_id'); - add_action($pluginId . '_after_webhook_action', static function (Payment $payment, WC_Order $order) use ($container) { - if ($payment->isAuthorized()) { - if (!$payment->getAmountCaptured() == 0.0) { - return; - } - $order->set_status(SharedDataDictionary::STATUS_ON_HOLD); - $order->update_meta_data(self::ORDER_PAYMENT_STATUS_META_KEY, ManualCaptureStatus::STATUS_AUTHORIZED); - $order->save(); - } elseif ($payment->isPaid() && ($container->get('merchant.manual_capture.is_waiting'))($order)) { - $order->update_meta_data(self::ORDER_PAYMENT_STATUS_META_KEY, ManualCaptureStatus::STATUS_CAPTURED); - $order->save(); - } - }, 10, 2); + add_action('init', static function () use ($container) { + $pluginId = $container->get('shared.plugin_id'); - add_action('woocommerce_order_refunded', static function (int $orderId) use ($container) { - $order = wc_get_order($orderId); - if (!is_a($order, WC_Order::class)) { + if (!apply_filters('mollie_wc_gateway_enable_merchant_capture_module', false)) { return; } - $merchantCanCapture = ($container->get('merchant.manual_capture.is_authorized'))($order); - if ($merchantCanCapture) { - ($container->get(VoidPayment::class))($order->get_id()); - } - }); - add_action('woocommerce_order_actions_start', static function (int $orderId) use ($container) { - $order = wc_get_order($orderId); - if (!is_a($order, WC_Order::class)) { - return; - } - $paymentStatus = $order->get_meta(MerchantCaptureModule::ORDER_PAYMENT_STATUS_META_KEY, true); - $actionBlockParagraphs = []; - - ob_start(); - (new StatusRenderer())($paymentStatus); - - $actionBlockParagraphs[] = ob_get_clean(); - if (($container->get('merchant.manual_capture.can_capture_the_order'))($order)) { - $actionBlockParagraphs[] = __( - 'To capture the authorized payment, select capture action from the list below.', - 'mollie-payments-for-woocommerce' - ); - } elseif (($container->get('merchant.manual_capture.is_authorized'))($order)) { - $actionBlockParagraphs[] = __( - 'Before capturing the authorized payment, ensure to set the order status to On Hold.', - 'mollie-payments-for-woocommerce' - ); - } - (new OrderActionBlock())($actionBlockParagraphs); - }); - add_filter( - 'mollie_wc_gateway_disable_ship_and_capture', - static function ($disableShipAndCapture, WC_Order $order) use ($container) { - if ($disableShipAndCapture) { - return true; + + add_action( + $pluginId . '_after_webhook_action', + static function (Payment $payment, WC_Order $order) use ($container) { + if ($payment->isAuthorized()) { + if (!$payment->getAmountCaptured() == 0.0) { + return; + } + $order->set_status(SharedDataDictionary::STATUS_ON_HOLD); + $order->update_meta_data( + self::ORDER_PAYMENT_STATUS_META_KEY, + ManualCaptureStatus::STATUS_AUTHORIZED + ); + $order->save(); + } elseif ($payment->isPaid() && ($container->get('merchant.manual_capture.is_waiting'))($order)) { + $order->update_meta_data( + self::ORDER_PAYMENT_STATUS_META_KEY, + ManualCaptureStatus::STATUS_CAPTURED + ); + $order->save(); + } + }, + 10, + 2 + ); + + add_action('woocommerce_order_refunded', static function (int $orderId) use ($container) { + $order = wc_get_order($orderId); + if (!is_a($order, WC_Order::class)) { + return; + } + $merchantCanCapture = ($container->get('merchant.manual_capture.is_authorized'))($order); + if ($merchantCanCapture) { + ($container->get(VoidPayment::class))($order->get_id()); } - return $container->get('merchant.manual_capture.is_waiting')($order); - }, - 10, - 2 - ); - new OrderListPaymentColumn(); - new ManualCapture($container); - new StateChangeCapture($container); + }); + add_action('woocommerce_order_actions_start', static function (int $orderId) use ($container) { + $order = wc_get_order($orderId); + if (!is_a($order, WC_Order::class)) { + return; + } + $paymentStatus = $order->get_meta(MerchantCaptureModule::ORDER_PAYMENT_STATUS_META_KEY, true); + $actionBlockParagraphs = []; + + ob_start(); + (new StatusRenderer())($paymentStatus); + + $actionBlockParagraphs[] = ob_get_clean(); + if (($container->get('merchant.manual_capture.can_capture_the_order'))($order)) { + $actionBlockParagraphs[] = __( + 'To capture the authorized payment, select capture action from the list below.', + 'mollie-payments-for-woocommerce' + ); + } elseif (($container->get('merchant.manual_capture.is_authorized'))($order)) { + $actionBlockParagraphs[] = __( + 'Before capturing the authorized payment, ensure to set the order status to On Hold.', + 'mollie-payments-for-woocommerce' + ); + } + (new OrderActionBlock())($actionBlockParagraphs); + }); + add_filter( + 'mollie_wc_gateway_disable_ship_and_capture', + static function ($disableShipAndCapture, WC_Order $order) use ($container) { + if ($disableShipAndCapture) { + return true; + } + return $container->get('merchant.manual_capture.is_waiting')($order); + }, + 10, + 2 + ); + add_filter( + 'inpsyde.mollie-advanced-settings', + ['Mollie\WooCommerce\MerchantCapture\MollieCaptureSettings', 'settings'], + 10, + 2 + ); + new OrderListPaymentColumn(); + new ManualCapture($container); + new StateChangeCapture($container); + }); + return true; } } diff --git a/src/MerchantCapture/MollieCaptureSettings.php b/src/MerchantCapture/MollieCaptureSettings.php new file mode 100644 index 00000000..8a9a2091 --- /dev/null +++ b/src/MerchantCapture/MollieCaptureSettings.php @@ -0,0 +1,50 @@ + $pluginName . '_place_payment_onhold', + 'title' => __('Placing payments on Hold', 'mollie-payments-for-woocommerce'), + 'type' => 'select', + 'desc_tip' => true, + 'options' => [ + 'immediate_capture' => __('Capture payments immediately', 'mollie-payments-for-woocommerce'), + 'later_capture' => __('Authorize payments for a later capture', 'mollie-payments-for-woocommerce'), + ], + 'default' => 'immediate_capture', + 'desc' => sprintf( + __( + 'Authorized payment can be captured or voided by changing the order status instead of doing it manually.', + 'mollie-payments-for-woocommerce' + ) + ), + ], + [ + 'id' => $pluginName . '_capture_or_void', + 'title' => __( + 'Capture or void on status change', + 'mollie-payments-for-woocommerce' + ), + 'type' => 'checkbox', + 'default' => 'no', + 'desc' => __( + 'Capture authorized payments automatically when setting the order status to Processing or Completed. Void the payment by setting the order status Canceled.', + 'mollie-payments-for-woocommerce' + ), + ], + [ + 'id' => $pluginName . '_sectionend', + 'type' => 'sectionend', + ], + ]; + + return array_merge($advancedSettings, $mollieCaptureSettings); + } +} From a364a7e38123e35e33f5c81f6dad3bf52627ae24 Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 2 Nov 2023 11:08:01 +0100 Subject: [PATCH 2/2] Code style --- inc/settings/mollie_advanced_settings.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inc/settings/mollie_advanced_settings.php b/inc/settings/mollie_advanced_settings.php index 712914c1..abfd156d 100644 --- a/inc/settings/mollie_advanced_settings.php +++ b/inc/settings/mollie_advanced_settings.php @@ -198,8 +198,7 @@ class="mollie-settings-advanced-payment-desc-label button button-secondary butto 'desc' => __("Remove options and scheduled actions from database when uninstalling the plugin.", "mollie-payments-for-woocommerce") . ' (' . strtolower( __('Clear now', 'mollie-payments-for-woocommerce') ) . ')', - ] + ], ]; - return apply_filters('inpsyde.mollie-advanced-settings', $mollieAdvancedSettings, $pluginName);