announce subscription support in gateway and force checkout deactivation if no support

This commit is contained in:
David Remer 2020-07-23 16:01:02 +03:00
parent 40b3e12057
commit 68fe89af4b
6 changed files with 44 additions and 13 deletions

View file

@ -16,4 +16,11 @@ class DisabledSmartButton implements SmartButtonInterface
{ {
return true; return true;
} }
public function canSaveVaultToken(): bool {
return false;
}
public function hasSubscription(): bool {
return false;
}
} }

View file

@ -60,7 +60,7 @@ class SmartButton implements SmartButtonInterface
public function renderWrapper(): bool public function renderWrapper(): bool
{ {
if (! $this->saveVaultToken() && $this->hasSubscription()) { if (! $this->canSaveVaultToken() && $this->hasSubscription()) {
return false; return false;
} }
$buttonRenderer = static function () { $buttonRenderer = static function () {
@ -116,10 +116,11 @@ class SmartButton implements SmartButtonInterface
31 31
); );
} }
$dccNotEnabledOnProductPage = $this->settings->has('dcc_single_product_enabled') &&
!$this->settings->get('dcc_single_product_enabled');
if ( if (
(is_product() || wc_post_content_has_shortcode('product_page')) (is_product() || wc_post_content_has_shortcode('product_page'))
&& $this->settings->has('dcc_single_product_enabled') && ! $dccNotEnabledOnProductPage
&& $this->settings->get('dcc_single_product_enabled')
) { ) {
add_action( add_action(
'woocommerce_single_product_summary', 'woocommerce_single_product_summary',
@ -181,7 +182,7 @@ class SmartButton implements SmartButtonInterface
public function enqueue(): bool public function enqueue(): bool
{ {
if (! $this->saveVaultToken() && $this->hasSubscription()) { if (! $this->canSaveVaultToken() && $this->hasSubscription()) {
return false; return false;
} }
wp_enqueue_style( wp_enqueue_style(
@ -228,7 +229,7 @@ class SmartButton implements SmartButtonInterface
) { ) {
return; return;
} }
$saveCard = $this->saveVaultToken() ? sprintf( $saveCard = $this->canSaveVaultToken() ? sprintf(
'<div> '<div>
<label for="ppcp-vault-%1$s">%2$s</label> <label for="ppcp-vault-%1$s">%2$s</label>
@ -271,7 +272,7 @@ class SmartButton implements SmartButtonInterface
} }
// phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong // phpcs:enable Inpsyde.CodeQuality.FunctionLength.TooLong
private function saveVaultToken(): bool public function canSaveVaultToken(): bool
{ {
if (! $this->settings->has('client_id') || ! $this->settings->get('client_id')) { if (! $this->settings->has('client_id') || ! $this->settings->get('client_id')) {
@ -283,7 +284,7 @@ class SmartButton implements SmartButtonInterface
return is_user_logged_in(); return is_user_logged_in();
} }
private function hasSubscription(): bool public function hasSubscription(): bool
{ {
if (is_product()) { if (is_product()) {
@ -388,7 +389,7 @@ class SmartButton implements SmartButtonInterface
//ToDo: Update date on releases. //ToDo: Update date on releases.
'integration-date' => date('Y-m-d'), 'integration-date' => date('Y-m-d'),
'components' => implode(',', $this->components()), 'components' => implode(',', $this->components()),
'vault' => $this->dccIsEnabled() || $this->saveVaultToken() ? 'true' : 'false', 'vault' => $this->dccIsEnabled() || $this->canSaveVaultToken() ? 'true' : 'false',
'commit' => is_checkout() ? 'true' : 'false', 'commit' => is_checkout() ? 'true' : 'false',
'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture', 'intent' => ($this->settings->has('intent')) ? $this->settings->get('intent') : 'capture',
]; ];
@ -416,7 +417,7 @@ class SmartButton implements SmartButtonInterface
if (!is_user_logged_in()) { if (!is_user_logged_in()) {
return $attributes; return $attributes;
} }
if (! $this->dccIsEnabled() && ! $this->saveVaultToken()) { if (! $this->dccIsEnabled() && ! $this->canSaveVaultToken()) {
return $attributes; return $attributes;
} }
$clientToken = $this->identityToken->generateForCustomer((int) get_current_user_id()); $clientToken = $this->identityToken->generateForCustomer((int) get_current_user_id());

View file

@ -9,4 +9,8 @@ interface SmartButtonInterface
public function renderWrapper(): bool; public function renderWrapper(): bool;
public function enqueue(): bool; public function enqueue(): bool;
public function canSaveVaultToken(): bool;
public function hasSubscription(): bool;
} }

View file

@ -28,18 +28,23 @@ return [
$notice = $container->get('wcgateway.notice.authorize-order-action'); $notice = $container->get('wcgateway.notice.authorize-order-action');
$settings = $container->get('wcgateway.settings'); $settings = $container->get('wcgateway.settings');
$smartButton = $container->get('button.smart-button');
$supportsSubscription = $smartButton->canSaveVaultToken();
return new WcGateway( return new WcGateway(
$settingsRenderer, $settingsRenderer,
$orderProcessor, $orderProcessor,
$authorizedPayments, $authorizedPayments,
$notice, $notice,
$settings $settings,
$supportsSubscription
); );
}, },
'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways { 'wcgateway.disabler' => static function (ContainerInterface $container): DisableGateways {
$sessionHandler = $container->get('session.handler'); $sessionHandler = $container->get('session.handler');
$settings = $container->get('wcgateway.settings'); $settings = $container->get('wcgateway.settings');
return new DisableGateways($sessionHandler, $settings); $smartButton = $container->get('button.smart-button');
$subscriptionDisable = ! $smartButton->canSaveVaultToken() && $smartButton->hasSubscription();
return new DisableGateways($sessionHandler, $settings, $subscriptionDisable);
}, },
'wcgateway.settings' => static function (ContainerInterface $container): Settings { 'wcgateway.settings' => static function (ContainerInterface $container): Settings {
return new Settings(); return new Settings();

View file

@ -13,13 +13,16 @@ class DisableGateways
private $sessionHandler; private $sessionHandler;
private $settings; private $settings;
private $subscriptionDisable;
public function __construct( public function __construct(
SessionHandler $sessionHandler, SessionHandler $sessionHandler,
ContainerInterface $settings ContainerInterface $settings,
bool $subscriptionDisable
) { ) {
$this->sessionHandler = $sessionHandler; $this->sessionHandler = $sessionHandler;
$this->settings = $settings; $this->settings = $settings;
$this->subscriptionDisable = $subscriptionDisable;
} }
public function handler(array $methods): array public function handler(array $methods): array
@ -34,6 +37,12 @@ class DisableGateways
unset($methods[WcGateway::ID]); unset($methods[WcGateway::ID]);
return $methods; return $methods;
} }
if ($this->subscriptionDisable) {
unset($methods[WcGateway::ID]);
return $methods;
}
if (! $this->needsToDisableGateways()) { if (! $this->needsToDisableGateways()) {
return $methods; return $methods;
} }

View file

@ -10,6 +10,7 @@ use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\OrderFactory;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository; use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Button\Assets\SmartButton;
use Inpsyde\PayPalCommerce\Onboarding\Render\OnboardingRenderer; use Inpsyde\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice;
@ -40,7 +41,8 @@ class WcGateway extends \WC_Payment_Gateway
OrderProcessor $orderProcessor, OrderProcessor $orderProcessor,
AuthorizedPaymentsProcessor $authorizedPayments, AuthorizedPaymentsProcessor $authorizedPayments,
AuthorizeOrderActionNotice $notice, AuthorizeOrderActionNotice $notice,
ContainerInterface $config ContainerInterface $config,
bool $supportsSubscription
) { ) {
$this->id = self::ID; $this->id = self::ID;
@ -49,6 +51,9 @@ class WcGateway extends \WC_Payment_Gateway
$this->notice = $notice; $this->notice = $notice;
$this->settingsRenderer = $settingsRenderer; $this->settingsRenderer = $settingsRenderer;
$this->config = $config; $this->config = $config;
if ($supportsSubscription) {
$this->supports = array('subscriptions', 'products');
}
$this->method_title = __('PayPal Payments', 'woocommerce-paypal-commerce-gateway'); $this->method_title = __('PayPal Payments', 'woocommerce-paypal-commerce-gateway');
$this->method_description = __( $this->method_description = __(