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 all commits
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
45 changes: 2 additions & 43 deletions modules/ppcp-button/src/Assets/SmartButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\StartPayPalVaultingEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
Expand All @@ -41,7 +42,7 @@
*/
class SmartButton implements SmartButtonInterface {

use FreeTrialHandlerTrait;
use FreeTrialHandlerTrait, ContextTrait;

/**
* The Settings status helper.
Expand Down Expand Up @@ -1067,48 +1068,6 @@ private function load_button_component() : bool {
}
}

/**
* The current context.
*
* @return string
*/
private function context(): string {
$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;
}

/**
* Checks if PayPal payment was already initiated (on the product or cart pages).
*
* @return bool
*/
private 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 true;
}

/**
* Whether DCC is enabled or not.
*
Expand Down
62 changes: 62 additions & 0 deletions modules/ppcp-button/src/Helper/ContextTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Helper trait for context.
*
* @package WooCommerce\PayPalCommerce\Button\Helper
*/

declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Button\Helper;

trait ContextTrait {

/**
* The current context.
*
* @return string
*/
protected function context(): string {
$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;
}

/**
* Checks if PayPal payment was already initiated (on the product or cart pages).
*
* @return bool
*/
private 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 true;
}
}
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
53 changes: 29 additions & 24 deletions modules/ppcp-wc-gateway/src/Assets/FraudNetAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace WooCommerce\PayPalCommerce\WcGateway\Assets;

use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
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 All @@ -20,6 +23,8 @@
*/
class FraudNetAssets {

use ContextTrait;

/**
* The URL of this module.
*
Expand Down Expand Up @@ -63,11 +68,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 +84,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 +100,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 +109,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,12 +156,11 @@ 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();
}

return $is_pui_gateway_enabled ? true : $this->is_fraudnet_enabled;

}

/**
Expand All @@ -168,19 +172,20 @@ protected function are_buttons_enabled_for_context() : bool {
if ( ! in_array( PayPalGateway::ID, $this->enabled_ppcp_gateways, true ) ) {
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 === 'product' ) {
return $buttons_enabled_for_context || $this->settings->has( 'mini-cart' ) && $this->settings->get( 'mini-cart' );
try {
$button_locations = $this->settings->get( 'smart_button_locations' );
} catch ( NotFoundException $exception ) {
return false;
}

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

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

return in_array( $this->context(), $button_locations, true );
}
}