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

[PIWOO-363] Feature flag for Merchant capture module #853

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions inc/settings/mollie_advanced_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -199,38 +199,6 @@ class="mollie-settings-advanced-payment-desc-label button button-secondary butto
__('Clear now', 'mollie-payments-for-woocommerce')
) . '</a>)',
],
[
'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);
145 changes: 85 additions & 60 deletions src/MerchantCapture/MerchantCaptureModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
50 changes: 50 additions & 0 deletions src/MerchantCapture/MollieCaptureSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Mollie\WooCommerce\MerchantCapture;

class MollieCaptureSettings
{
public function settings(array $advancedSettings, string $pluginName): array
{
$mollieCaptureSettings = [
[
'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 array_merge($advancedSettings, $mollieCaptureSettings);
}
}
Loading