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;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
2020-08-28 08:13:45 +03:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2021-12-22 12:46:48 +01:00
|
|
|
ContainerInterface $settings
|
2020-08-28 08:13:45 +03:00
|
|
|
) {
|
|
|
|
|
2021-10-25 16:27:05 +02: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;
|
|
|
|
}
|
2020-09-29 10:17:59 +03:00
|
|
|
if ( $this->disable_both_gateways() ) {
|
2020-08-28 08:13:45 +03:00
|
|
|
unset( $methods[ PayPalGateway::ID ] );
|
|
|
|
unset( $methods[ CreditCardGateway::ID ] );
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $this->settings->has( 'client_id' ) || empty( $this->settings->get( 'client_id' ) ) ) {
|
|
|
|
unset( $methods[ CreditCardGateway::ID ] );
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:24:08 +03:00
|
|
|
if ( $this->settings->has( 'button_enabled' ) && ! $this->settings->get( 'button_enabled' ) && ! $this->session_handler->order() ) {
|
|
|
|
unset( $methods[ PayPalGateway::ID ] );
|
2020-09-03 11:23:11 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
if ( ! $this->needs_to_disable_gateways() ) {
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->is_credit_card() ) {
|
2021-02-16 11:52:39 +01:00
|
|
|
return array(
|
|
|
|
CreditCardGateway::ID => $methods[ CreditCardGateway::ID ],
|
|
|
|
PayPalGateway::ID => $methods[ PayPalGateway::ID ],
|
|
|
|
);
|
2020-08-28 08:13:45 +03:00
|
|
|
}
|
|
|
|
return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] );
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:17:59 +03:00
|
|
|
/**
|
|
|
|
* Whether both gateways should be disabled or not.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function disable_both_gateways() : bool {
|
|
|
|
if ( ! $this->settings->has( 'enabled' ) || ! $this->settings->get( 'enabled' ) ) {
|
|
|
|
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 {
|
|
|
|
return $this->session_handler->order() !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the current PayPal session is done via DCC payment.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function is_credit_card(): bool {
|
|
|
|
$order = $this->session_handler->order();
|
|
|
|
if ( ! $order ) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-01 09:00:45 +03:00
|
|
|
if ( ! $order->payment_source() || ! $order->payment_source()->card() ) {
|
2020-08-28 08:13:45 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|