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

DEVICE_DATA_NOT_AVAILABLE error message when FraudNet is enabled (1429) #1177

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 1 addition & 34 deletions modules/ppcp-wc-gateway/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,39 +1333,6 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice {

return $enabled_ppcp_gateways;
},
'wcgateway.is-paypal-continuation' => static function ( ContainerInterface $container ): bool {
$session_handler = $container->get( 'session.handler' );
assert( $session_handler instanceof SessionHandler );

$order = $session_handler->order();
if ( ! $order ) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.
}
if ( 'card' === $session_handler->funding_source() ) {
return false; // Ignore for card buttons.
}
return true;
},
'wcgateway.current-context' => static function ( ContainerInterface $container ): string {
$context = 'mini-cart';
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
$context = 'product';
}
if ( is_cart() ) {
$context = 'cart';
}
if ( is_checkout() && ! $container->get( 'wcgateway.is-paypal-continuation' ) ) {
$context = 'checkout';
}
if ( is_checkout_pay_page() ) {
$context = 'pay-now';
}
return $context;
},
'wcgateway.is-fraudnet-enabled' => static function ( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
Expand All @@ -1380,7 +1347,7 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice {
$container->get( 'onboarding.environment' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.enabled-ppcp-gateways' ),
$container->get( 'wcgateway.current-context' ),
$container->get( 'session.handler' ),
$container->get( 'wcgateway.is-fraudnet-enabled' )
);
},
Expand Down
91 changes: 69 additions & 22 deletions modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;

use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
Expand Down Expand Up @@ -63,11 +65,11 @@ class FraudNetAssets {
protected $enabled_ppcp_gateways;

/**
* The current context.
* The session handler
*
* @var string
* @var SessionHandler
*/
protected $context;
protected $session_handler;

/**
* True if FraudNet support is enabled in settings, otherwise false.
Expand All @@ -79,14 +81,14 @@ class FraudNetAssets {
/**
* Assets constructor.
*
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param FraudNet $fraud_net The FraudNet entity.
* @param Environment $environment The environment.
* @param Settings $settings The Settings.
* @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways.
* @param string $context The current context.
* @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false.
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param FraudNet $fraud_net The FraudNet entity.
* @param Environment $environment The environment.
* @param Settings $settings The Settings.
* @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways.
* @param SessionHandler $session_handler The session handler.
* @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false.
*/
public function __construct(
string $module_url,
Expand All @@ -95,7 +97,7 @@ public function __construct(
Environment $environment,
Settings $settings,
array $enabled_ppcp_gateways,
string $context,
SessionHandler $session_handler,
bool $is_fraudnet_enabled
) {
$this->module_url = $module_url;
Expand All @@ -104,7 +106,7 @@ public function __construct(
$this->environment = $environment;
$this->settings = $settings;
$this->enabled_ppcp_gateways = $enabled_ppcp_gateways;
$this->context = $context;
$this->session_handler = $session_handler;
$this->is_fraudnet_enabled = $is_fraudnet_enabled;
}

Expand Down Expand Up @@ -151,7 +153,7 @@ protected function should_load_fraudnet_script(): bool {
$is_pui_gateway_enabled = in_array( PayUponInvoiceGateway::ID, $this->enabled_ppcp_gateways, true );
$is_only_standard_gateway_enabled = $this->enabled_ppcp_gateways === array( PayPalGateway::ID );

if ( $this->context !== 'checkout' || $is_only_standard_gateway_enabled ) {
if ( $this->context() !== 'checkout' || $is_only_standard_gateway_enabled ) {
return $this->is_fraudnet_enabled && $this->are_buttons_enabled_for_context();
}

Expand All @@ -168,19 +170,64 @@ protected function are_buttons_enabled_for_context() : bool {
if ( ! in_array( PayPalGateway::ID, $this->enabled_ppcp_gateways, true ) ) {
return false;
}
try {
$button_locations = $this->settings->get( 'smart_button_locations' );
} catch ( NotFoundException $exception ) {
return false;
}

$location_prefix = $this->context === 'checkout' ? '' : "{$this->context}_";
$setting_name = "button_{$location_prefix}enabled";
$buttons_enabled_for_context = $this->settings->has( $setting_name ) && $this->settings->get( $setting_name );
if ( $this->context() === 'pay-now' ) {
return true;
}

if ( $this->context === 'product' ) {
return $buttons_enabled_for_context || $this->settings->has( 'mini-cart' ) && $this->settings->get( 'mini-cart' );
if ( $this->context() === 'product' ) {
return in_array( 'product', $button_locations, true ) || in_array( 'mini-cart', $button_locations, true );
}

if ( $this->context === 'pay-now' ) {
return true;
return in_array( $this->context(), $button_locations, true );
}

/**
* Get context.
*
* @return string
*/
protected function context(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to extract this somewhere to avoid duplication (currently here and in SmartButton). Maybe into a trait.

$context = 'mini-cart';
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
$context = 'product';
}
if ( is_cart() ) {
$context = 'cart';
}
if ( is_checkout() && ! $this->is_paypal_continuation() ) {
$context = 'checkout';
}
if ( is_checkout_pay_page() ) {
$context = 'pay-now';
}

return $context;
}

/**
* Whether is PayPal continuation or not.
*
* @return bool
*/
protected function is_paypal_continuation(): bool {
$order = $this->session_handler->order();
if ( ! $order ) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.
}
if ( 'card' === $this->session_handler->funding_source() ) {
return false; // Ignore for card buttons.
}

return $buttons_enabled_for_context;
return true;
}
}