Merge branch 'PCP-3398-critical-error-when-changing-subscription-payment-method-to-advanced-card-processing' into PCP-4110-incorrect-subscription-cancellation-handling-with-pay-pal-subscriptions

This commit is contained in:
Daniel Hüsken 2025-02-07 12:37:53 +01:00
commit 13f17410ea
No known key found for this signature in database
GPG key ID: 9F732DA37FA709E8
229 changed files with 6074 additions and 3425 deletions

View file

@ -18,7 +18,7 @@ use WooCommerce\PayPalCommerce\Googlepay\Endpoint\UpdatePaymentDataEndpoint;
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmApplies;
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus;
use WooCommerce\PayPalCommerce\Googlepay\Helper\AvailabilityNotice;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
@ -75,7 +75,7 @@ return array(
return new ApmProductStatus(
$container->get( 'wcgateway.settings' ),
$container->get( 'api.endpoint.partners' ),
$container->get( 'onboarding.state' ),
$container->get( 'settings.flag.is-connected' ),
$container->get( 'api.helper.failure-registry' )
);
}

View file

@ -16,7 +16,7 @@ use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Googlepay\Endpoint\UpdatePaymentDataEndpoint;
use WooCommerce\PayPalCommerce\Googlepay\GooglePayGateway;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;

View file

@ -233,7 +233,7 @@ class GooglepayModule implements ServiceModule, ExtendingModule, ExecutableModul
);
add_filter(
'woocommerce_paypal_payments_rest_common_merchant_data',
'woocommerce_paypal_payments_rest_common_merchant_features',
function ( array $features ) use ( $c ): array {
$product_status = $c->get( 'googlepay.helpers.apm-product-status' );
assert( $product_status instanceof ApmProductStatus );

View file

@ -9,130 +9,66 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusCapability;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\ApiClient\Helper\ProductStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
/**
* Class ApmProductStatus
*/
class ApmProductStatus {
const CAPABILITY_NAME = 'GOOGLE_PAY';
const SETTINGS_KEY = 'products_googlepay_enabled';
class ApmProductStatus extends ProductStatus {
public const CAPABILITY_NAME = 'GOOGLE_PAY';
public const SETTINGS_KEY = 'products_googlepay_enabled';
const SETTINGS_VALUE_ENABLED = 'yes';
const SETTINGS_VALUE_DISABLED = 'no';
const SETTINGS_VALUE_UNDEFINED = '';
/**
* The current status stored in memory.
*
* @var bool|null
*/
private $current_status = null;
/**
* If there was a request failure.
*
* @var bool
*/
private $has_request_failure = false;
public const SETTINGS_VALUE_ENABLED = 'yes';
public const SETTINGS_VALUE_DISABLED = 'no';
public const SETTINGS_VALUE_UNDEFINED = '';
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The partners endpoint.
*
* @var PartnersEndpoint
*/
private $partners_endpoint;
/**
* The onboarding status
*
* @var State
*/
private $onboarding_state;
/**
* The API failure registry
*
* @var FailureRegistry
*/
private $api_failure_registry;
private Settings $settings;
/**
* ApmProductStatus constructor.
*
* @param Settings $settings The Settings.
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param State $onboarding_state The onboarding state.
* @param Settings $settings The Settings.
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
* @param bool $is_connected The onboarding state.
* @param FailureRegistry $api_failure_registry The API failure registry.
*/
public function __construct(
Settings $settings,
PartnersEndpoint $partners_endpoint,
State $onboarding_state,
bool $is_connected,
FailureRegistry $api_failure_registry
) {
$this->settings = $settings;
$this->partners_endpoint = $partners_endpoint;
$this->onboarding_state = $onboarding_state;
$this->api_failure_registry = $api_failure_registry;
parent::__construct( $is_connected, $partners_endpoint, $api_failure_registry );
$this->settings = $settings;
}
/**
* Whether the active/subscribed products support Googlepay.
*
* @return bool
*/
public function is_active() : bool {
// If not onboarded then makes no sense to check status.
if ( ! $this->is_onboarded() ) {
return false;
}
/** {@inheritDoc} */
protected function check_local_state() : ?bool {
$status_override = apply_filters( 'woocommerce_paypal_payments_google_pay_product_status', null );
if ( null !== $status_override ) {
return $status_override;
}
// If status was already checked on this request return the same result.
if ( null !== $this->current_status ) {
return $this->current_status;
}
// Check if status was checked on previous requests.
if ( $this->settings->has( self::SETTINGS_KEY ) && ( $this->settings->get( self::SETTINGS_KEY ) ) ) {
$this->current_status = wc_string_to_bool( $this->settings->get( self::SETTINGS_KEY ) );
return $this->current_status;
return wc_string_to_bool( $this->settings->get( self::SETTINGS_KEY ) );
}
// Check API failure registry to prevent multiple failed API requests.
if ( $this->api_failure_registry->has_failure_in_timeframe( FailureRegistry::SELLER_STATUS_KEY, HOUR_IN_SECONDS ) ) {
$this->has_request_failure = true;
$this->current_status = false;
return $this->current_status;
}
// Request seller status via PayPal API.
try {
$seller_status = $this->partners_endpoint->seller_status();
} catch ( Throwable $error ) {
$this->has_request_failure = true;
$this->current_status = false;
return $this->current_status;
}
return null;
}
/** {@inheritDoc} */
protected function check_active_state( SellerStatus $seller_status ) : bool {
// Check the seller status for the intended capability.
$has_capability = false;
foreach ( $seller_status->products() as $product ) {
@ -145,65 +81,33 @@ class ApmProductStatus {
}
}
foreach ( $seller_status->capabilities() as $capability ) {
if ( $capability->name() === self::CAPABILITY_NAME && $capability->status() === SellerStatusCapability::STATUS_ACTIVE ) {
$has_capability = true;
if ( ! $has_capability ) {
foreach ( $seller_status->capabilities() as $capability ) {
if ( $capability->name() === self::CAPABILITY_NAME && $capability->status() === SellerStatusCapability::STATUS_ACTIVE ) {
$has_capability = true;
}
}
}
if ( $has_capability ) {
// Capability found, persist status and return true.
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
$this->settings->persist();
$this->current_status = true;
return $this->current_status;
} else {
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_DISABLED );
}
// Capability not found, persist status and return false.
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_DISABLED );
$this->settings->persist();
$this->current_status = false;
return $this->current_status;
return $has_capability;
}
/**
* Returns if the seller is onboarded.
*
* @return bool
*/
public function is_onboarded(): bool {
return $this->onboarding_state->current_state() >= State::STATE_ONBOARDED;
}
/**
* Returns if there was a request failure.
*
* @return bool
*/
public function has_request_failure(): bool {
return $this->has_request_failure;
}
/**
* Clears the persisted result to force a recheck.
*
* @param Settings|null $settings The settings object.
* We accept a Settings object to don't override other sequential settings that are being updated elsewhere.
* @return void
*/
public function clear( Settings $settings = null ): void {
/** {@inheritDoc} */
protected function clear_state( Settings $settings = null ) : void {
if ( null === $settings ) {
$settings = $this->settings;
}
$this->current_status = null;
if ( $settings->has( self::SETTINGS_KEY ) ) {
$settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_UNDEFINED );
$settings->persist();
}
}
}