woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php

129 lines
3.1 KiB
PHP
Raw Normal View History

2020-08-28 08:13:45 +03:00
<?php
/**
* Determines whether specific gateways need to be disabled.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\WcGateway\Checkout
2020-08-28 08:13:45 +03:00
*/
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\WcGateway\Checkout;
2020-08-28 08:13:45 +03:00
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Session\SessionHandler;
2022-07-19 09:20:26 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
2020-08-28 08:13:45 +03:00
/**
* Class DisableGateways
*/
class DisableGateways {
/**
* The Session Handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* The Settings.
*
* @var ContainerInterface
*/
private $settings;
/**
* DisableGateways constructor.
*
* @param SessionHandler $session_handler The Session Handler.
* @param ContainerInterface $settings The Settings.
*/
public function __construct(
SessionHandler $session_handler,
ContainerInterface $settings
2020-08-28 08:13:45 +03:00
) {
2022-01-03 15:15:33 +01:00
$this->session_handler = $session_handler;
$this->settings = $settings;
2020-08-28 08:13:45 +03:00
}
/**
* Controls the logic for enabling/disabling gateways.
*
* @param array $methods The Gateways.
*
* @return array
*/
public function handler( array $methods ): array {
if ( ! isset( $methods[ PayPalGateway::ID ] ) && ! isset( $methods[ CreditCardGateway::ID ] ) ) {
return $methods;
}
2022-07-19 09:20:26 +03:00
if ( $this->disable_all_gateways() ) {
2020-08-28 08:13:45 +03:00
unset( $methods[ PayPalGateway::ID ] );
unset( $methods[ CreditCardGateway::ID ] );
2022-07-19 09:20:26 +03:00
unset( $methods[ CardButtonGateway::ID ] );
2020-08-28 08:13:45 +03:00
return $methods;
}
if ( ! $this->settings->has( 'client_id' ) || empty( $this->settings->get( 'client_id' ) ) ) {
unset( $methods[ CreditCardGateway::ID ] );
}
if ( $this->settings->has( 'button_enabled' ) && ! $this->settings->get( 'button_enabled' ) && ! $this->session_handler->order() && is_checkout() ) {
2020-09-11 14:24:08 +03:00
unset( $methods[ PayPalGateway::ID ] );
}
2020-08-28 08:13:45 +03:00
if ( ! $this->needs_to_disable_gateways() ) {
return $methods;
}
return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] );
}
/**
2022-07-19 09:20:26 +03:00
* Whether all gateways should be disabled or not.
*
* @return bool
*/
2022-07-19 09:20:26 +03:00
private function disable_all_gateways() : bool {
foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
if ( PayPalGateway::ID === $gateway->id && $gateway->enabled !== 'yes' ) {
return true;
}
}
if ( ! $this->settings->has( 'merchant_email' ) || ! is_email( $this->settings->get( 'merchant_email' ) ) ) {
return true;
}
return false;
}
2020-08-28 08:13:45 +03:00
/**
* Whether the Gateways need to be disabled. When we come to the checkout with a running PayPal
* session, we need to disable the other Gateways, so the customer can smoothly sail through the
* process.
*
* @return bool
*/
private function needs_to_disable_gateways(): bool {
$order = $this->session_handler->order();
if ( ! $order ) {
return false;
}
2022-07-19 09:29:12 +03:00
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // DCC.
2020-08-28 08:13:45 +03:00
}
2022-07-19 09:29:12 +03:00
if ( 'card' === $this->session_handler->funding_source() ) {
return false; // Card buttons.
}
2020-08-28 08:13:45 +03:00
return true;
}
}