diff --git a/commerce_paypal.module b/commerce_paypal.module index 359090f..bcdecbc 100644 --- a/commerce_paypal.module +++ b/commerce_paypal.module @@ -17,6 +17,86 @@ use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +/** + * Loads all enabled commerce payment gateway entities. + * + * @return \Drupal\commerce_payment\Entity\PaymentGatewayInterface[] + * An array of enabled payment gateway entities. + */ +function load_enabled_commerce_payment_gateways() { + $payment_gateways = \Drupal::entityTypeManager() + ->getStorage('commerce_payment_gateway') + ->loadMultiple(); + + $enabled_gateways = []; + foreach ($payment_gateways as $gateway) { + if ($gateway->status()) { + $enabled_gateways[$gateway->id()] = $gateway; + } + } + + return $enabled_gateways; +} + +/** + * Checks if there is only one enabled payment gateway of specified PayPal types. + * + * @return bool + * True if there is only one enabled PayPal gateway, false otherwise. + */ +function check_single_paypal_gateway() { + // Define the PayPal plugin types you want to check. + $paypal_plugin_types = [ + 'paypal_express_checkout', + 'paypal_payflow', + 'paypal_payflow_link', + 'paypal_checkout', + ]; + + // Get all enabled gateways. + $enabled_gateways = load_enabled_commerce_payment_gateways(); + + // Filter gateways to only include the specified PayPal types. + $paypal_gateways = array_filter($enabled_gateways, function ($gateway) use ($paypal_plugin_types) { + return in_array($gateway->getPluginId(), $paypal_plugin_types); + }); + + // Check if there's exactly one PayPal gateway enabled. + return count($paypal_gateways) === 1; +} + +/** + * Retrieves the single enabled PayPal gateway if it's the only one enabled. + * + * @return \Drupal\commerce_payment\Entity\PaymentGatewayInterface|null + * The single PayPal gateway entity or null if conditions are not met. + */ +function get_single_paypal_gateway() { + // Define the PayPal plugin types you want to check. + $paypal_plugin_types = [ + 'paypal_express_checkout', + 'paypal_payflow', + 'paypal_payflow_link', + 'paypal_checkout', + ]; + + // Get all enabled gateways. + $enabled_gateways = load_enabled_commerce_payment_gateways(); + + // Filter gateways to only include the specified PayPal types. + $paypal_gateways = array_filter($enabled_gateways, function ($gateway) use ($paypal_plugin_types) { + return in_array($gateway->getPluginId(), $paypal_plugin_types); + }); + + // Return the gateway entity if there's exactly one PayPal gateway enabled. + if (count($paypal_gateways) === 1) { + return reset($paypal_gateways); // Returns the first and only element in the array. + } + + return null; // Return null if conditions are not met. +} + + /** * Implements hook_theme(). */ @@ -101,7 +181,6 @@ function commerce_paypal_form_commerce_checkout_flow_alter(&$form, FormStateInte $plugin = $payment_gateway->getPlugin(); if ($plugin instanceof CheckoutInterface && $plugin->getPaymentSolution() === 'smart_payment_buttons') { $paypal_checkout_options_count++; - // This will ensure we only keep the first paypal checkout option found. if ($paypal_checkout_options_count > 1 && isset($form['payment_information']['payment_method']['#options'][$key])) { unset($form['payment_information']['payment_method']['#options'][$key]); @@ -109,16 +188,30 @@ function commerce_paypal_form_commerce_checkout_flow_alter(&$form, FormStateInte } } } + if (!in_array($form['#step_id'], ['review', 'complete'])) { return; } - if ($order->get('payment_gateway')->isEmpty() || - !$order->get('payment_gateway')->entity || - $order->get('checkout_flow')->target_id === 'paypal_checkout') { - return; + + // Example usage: + if (check_single_paypal_gateway()) { + // Do some stuff here for the single PayPal gateway case. + if ($order->get('checkout_flow')->target_id === 'paypal_checkout') { + return; + } + /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */ + $payment_gateway = get_single_paypal_gateway(); + } + else { + if ($order->get('payment_gateway')->isEmpty() || + !$order->get('payment_gateway')->entity || + $order->get('checkout_flow')->target_id === 'paypal_checkout') { + return; // Is returning here. + } + /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */ + $payment_gateway = $order->get('payment_gateway')->entity; } - /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */ - $payment_gateway = $order->get('payment_gateway')->entity; + $payment_gateway_plugin = $payment_gateway->getPlugin(); // Add fixes for Payflow Link iframe.