mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Refactor ApplePay notices
Refactor AppleProductStatus
This commit is contained in:
parent
5d406ea612
commit
20f64d63ed
8 changed files with 438 additions and 155 deletions
|
@ -24,7 +24,7 @@ return array(
|
|||
return $fields;
|
||||
}
|
||||
|
||||
$is_available = $container->get( 'applepay.enabled' );
|
||||
$is_available = $container->get( 'applepay.available' );
|
||||
$is_referral = $container->get( 'applepay.is_referral' );
|
||||
|
||||
$insert_after = function ( array $array, string $key, array $new ): array {
|
||||
|
|
|
@ -16,11 +16,10 @@ use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
|
|||
use WooCommerce\PayPalCommerce\Applepay\Assets\DataToAppleButtonScripts;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Assets\BlocksPaymentMethod;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Helper\ApmApplies;
|
||||
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Helper\AvailabilityNotice;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
return array(
|
||||
'applepay.eligible' => static function ( ContainerInterface $container ): bool {
|
||||
|
@ -47,23 +46,37 @@ return array(
|
|||
|
||||
return ! $status->has_request_failure();
|
||||
},
|
||||
|
||||
'applepay.availability_notice' => static function ( ContainerInterface $container ): AvailabilityNotice {
|
||||
$settings = $container->get( 'wcgateway.settings' );
|
||||
|
||||
return new AvailabilityNotice(
|
||||
$container->get( 'applepay.apple-product-status' ),
|
||||
$container->get( 'wcgateway.is-wc-gateways-list-page' ),
|
||||
$container->get( 'wcgateway.is-ppcp-settings-page' ),
|
||||
$container->get( 'applepay.available' ) || ( ! $container->get( 'applepay.is_referral' ) ),
|
||||
$container->get( 'applepay.server_supported' ),
|
||||
$settings->has( 'applepay_validated' ) ? $settings->get( 'applepay_validated' ) === true : false,
|
||||
$container->get( 'applepay.button' )
|
||||
);
|
||||
},
|
||||
|
||||
'applepay.apple-product-status' => static function( ContainerInterface $container ): AppleProductStatus {
|
||||
return new AppleProductStatus(
|
||||
$container->get( 'wcgateway.settings' ),
|
||||
$container->get( 'api.endpoint.partners' ),
|
||||
$container->get( 'applepay.status-cache' ),
|
||||
$container->get( 'onboarding.state' ),
|
||||
$container->get( 'api.helper.failure-registry' )
|
||||
);
|
||||
},
|
||||
'applepay.enabled' => static function ( ContainerInterface $container ): bool {
|
||||
if ( apply_filters( 'woocommerce_paypal_payments_applepay_validate_product_status', false ) ) {
|
||||
'applepay.available' => static function ( ContainerInterface $container ): bool {
|
||||
if ( apply_filters( 'woocommerce_paypal_payments_applepay_validate_product_status', true ) ) {
|
||||
$status = $container->get( 'applepay.apple-product-status' );
|
||||
assert( $status instanceof AppleProductStatus );
|
||||
/**
|
||||
* If merchant isn't onboarded via /v1/customer/partner-referrals this returns false as the API call fails.
|
||||
*/
|
||||
return apply_filters( 'woocommerce_paypal_payments_applepay_product_status', $status->apple_is_active() );
|
||||
return apply_filters( 'woocommerce_paypal_payments_applepay_product_status', $status->is_active() );
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
@ -149,7 +162,7 @@ return array(
|
|||
$environment = $container->get( 'onboarding.environment' );
|
||||
assert( $environment instanceof Environment );
|
||||
|
||||
$enabled = $product_status->apple_is_active();
|
||||
$enabled = $product_status->is_active();
|
||||
|
||||
$enabled_status_text = esc_html__( 'Status: Available', 'woocommerce-paypal-payments' );
|
||||
$disabled_status_text = esc_html__( 'Status: Not yet enabled', 'woocommerce-paypal-payments' );
|
||||
|
|
|
@ -10,11 +10,11 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\Applepay;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
|
||||
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
|
||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Helper\AvailabilityNotice;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||
|
@ -40,59 +40,49 @@ class ApplepayModule implements ModuleInterface {
|
|||
* {@inheritDoc}
|
||||
*/
|
||||
public function run( ContainerInterface $c ): void {
|
||||
|
||||
// Clears product status when appropriate.
|
||||
add_action(
|
||||
'woocommerce_paypal_payments_clear_apm_product_status',
|
||||
function( Settings $settings = null ) use ( $c ): void {
|
||||
$apm_status = $c->get( 'applepay.apple-product-status' );
|
||||
assert( $apm_status instanceof AppleProductStatus );
|
||||
$apm_status->clear( $settings );
|
||||
}
|
||||
);
|
||||
|
||||
// Check if the module is applicable, correct country, currency, ... etc.
|
||||
if ( ! $c->get( 'applepay.eligible' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load the button handler.
|
||||
$apple_payment_method = $c->get( 'applepay.button' );
|
||||
// add onboarding and referrals hooks.
|
||||
$apple_payment_method->initialize();
|
||||
if ( ! $c->get( 'applepay.enabled' ) ) {
|
||||
return;
|
||||
}
|
||||
if ( ! $c->get( 'applepay.server_supported' ) ) {
|
||||
add_action(
|
||||
'admin_notices',
|
||||
static function () {
|
||||
?>
|
||||
<div class="notice notice-error is-dismissible">
|
||||
<p>
|
||||
<?php
|
||||
echo wp_kses_post(
|
||||
__( 'Apple Pay is not supported on this server. Please contact your hosting provider to enable it.', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
$merchant_validated = $settings->has( 'applepay_validated' ) ? $settings->get( 'applepay_validated' ) === true : false;
|
||||
if ( ! $merchant_validated ) {
|
||||
add_action(
|
||||
'admin_notices',
|
||||
static function () {
|
||||
?>
|
||||
<div class="notice notice-error is-dismissible">
|
||||
<p>
|
||||
<?php
|
||||
echo wp_kses_post(
|
||||
__( 'Apple Pay Validation Error. Please check the requirements.', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
);
|
||||
}
|
||||
$this->load_assets( $c );
|
||||
$this->handle_validation_file( $c );
|
||||
$this->render_buttons( $c );
|
||||
assert( $apple_payment_method instanceof ApplepayButton );
|
||||
$apple_payment_method->bootstrap_ajax_request();
|
||||
$apple_payment_method->initialize();
|
||||
|
||||
$this->remove_status_cache( $c );
|
||||
// Show notice if there are product availability issues.
|
||||
$availability_notice = $c->get( 'applepay.availability_notice' );
|
||||
assert( $availability_notice instanceof AvailabilityNotice );
|
||||
$availability_notice->execute();
|
||||
|
||||
// Return if server not supported.
|
||||
if ( ! $c->get( 'applepay.server_supported' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this merchant can activate / use the buttons.
|
||||
// We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
|
||||
if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->load_assets( $c, $apple_payment_method );
|
||||
$this->handle_validation_file( $c );
|
||||
$this->render_buttons( $c, $apple_payment_method );
|
||||
|
||||
$apple_payment_method->bootstrap_ajax_request();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,14 +116,17 @@ class ApplepayModule implements ModuleInterface {
|
|||
* Registers and enqueues the assets.
|
||||
*
|
||||
* @param ContainerInterface $c The container.
|
||||
* @param ApplePayButton $button The button.
|
||||
* @return void
|
||||
*/
|
||||
public function load_assets( ContainerInterface $c ): void {
|
||||
public function load_assets( ContainerInterface $c, ApplePayButton $button ): void {
|
||||
if ( ! $button->is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action(
|
||||
'wp_enqueue_scripts',
|
||||
function () use ( $c ) {
|
||||
$button = $c->get( 'applepay.button' );
|
||||
assert( $button instanceof ApplePayButton );
|
||||
function () use ( $c, $button ) {
|
||||
$smart_button = $c->get( 'button.smart-button' );
|
||||
assert( $smart_button instanceof SmartButtonInterface );
|
||||
$page_has_block = has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' );
|
||||
|
@ -154,9 +147,14 @@ class ApplepayModule implements ModuleInterface {
|
|||
* Renders the Apple Pay buttons in the enabled places.
|
||||
*
|
||||
* @param ContainerInterface $c The container.
|
||||
* @param ApplePayButton $button The button.
|
||||
* @return void
|
||||
*/
|
||||
public function render_buttons( ContainerInterface $c ): void {
|
||||
public function render_buttons( ContainerInterface $c, ApplePayButton $button ): void {
|
||||
if ( ! $button->is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
add_action(
|
||||
'wp',
|
||||
static function () use ( $c ) {
|
||||
|
@ -175,44 +173,6 @@ class ApplepayModule implements ModuleInterface {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the status cache.
|
||||
*
|
||||
* @param ContainerInterface $c The container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function remove_status_cache( ContainerInterface $c ): void {
|
||||
add_action(
|
||||
'woocommerce_paypal_payments_gateway_migrate_on_update',
|
||||
static function () use ( $c ) {
|
||||
$apple_status_cache = $c->get( 'applepay.status-cache' );
|
||||
assert( $apple_status_cache instanceof Cache );
|
||||
|
||||
$apple_status_cache->delete( AppleProductStatus::APPLE_STATUS_CACHE_KEY );
|
||||
|
||||
$settings = $c->get( 'wcgateway.settings' );
|
||||
$settings->set( 'products_apple_enabled', false );
|
||||
$settings->persist();
|
||||
|
||||
// Update caches.
|
||||
$apple_status = $c->get( 'applepay.apple-product-status' );
|
||||
assert( $apple_status instanceof AppleProductStatus );
|
||||
$apple_status->apple_is_active();
|
||||
}
|
||||
);
|
||||
|
||||
add_action(
|
||||
'woocommerce_paypal_payments_on_listening_request',
|
||||
static function () use ( $c ) {
|
||||
$apple_status = $c->get( 'applepay.status-cache' );
|
||||
if ( $apple_status->has( AppleProductStatus::APPLE_STATUS_CACHE_KEY ) ) {
|
||||
$apple_status->delete( AppleProductStatus::APPLE_STATUS_CACHE_KEY );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the validation file.
|
||||
*
|
||||
|
|
|
@ -422,7 +422,7 @@ class ApplePayButton implements ButtonInterface {
|
|||
}
|
||||
$applepay_request_data_object->order_data( $context );
|
||||
$this->update_posted_data( $applepay_request_data_object );
|
||||
if ( $context == 'product' ) {
|
||||
if ( $context === 'product' ) {
|
||||
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
|
||||
$cart = WC()->cart;
|
||||
$address = $applepay_request_data_object->shipping_address();
|
||||
|
@ -902,15 +902,19 @@ class ApplePayButton implements ButtonInterface {
|
|||
* Renders the Apple Pay button on the page
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @psalm-suppress RedundantCondition
|
||||
*/
|
||||
public function render(): bool {
|
||||
$is_applepay_button_enabled = $this->settings->has( 'applepay_button_enabled' ) ? $this->settings->get( 'applepay_button_enabled' ) : false;
|
||||
if ( ! $this->is_enabled() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$button_enabled_product = $is_applepay_button_enabled && $this->settings_status->is_smart_button_enabled_for_location( 'product' );
|
||||
$button_enabled_cart = $is_applepay_button_enabled && $this->settings_status->is_smart_button_enabled_for_location( 'cart' );
|
||||
$button_enabled_checkout = $is_applepay_button_enabled;
|
||||
$button_enabled_payorder = $is_applepay_button_enabled;
|
||||
$button_enabled_minicart = $is_applepay_button_enabled && $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' );
|
||||
$button_enabled_product = $this->settings_status->is_smart_button_enabled_for_location( 'product' );
|
||||
$button_enabled_cart = $this->settings_status->is_smart_button_enabled_for_location( 'cart' );
|
||||
$button_enabled_checkout = true;
|
||||
$button_enabled_payorder = true;
|
||||
$button_enabled_minicart = $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' );
|
||||
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_sdk_components_hook',
|
||||
|
@ -1054,7 +1058,6 @@ class ApplePayButton implements ButtonInterface {
|
|||
*/
|
||||
public function is_enabled(): bool {
|
||||
try {
|
||||
// todo add also onboarded apple and enabled buttons.
|
||||
return $this->settings->has( 'applepay_button_enabled' ) && $this->settings->get( 'applepay_button_enabled' );
|
||||
} catch ( Exception $e ) {
|
||||
return false;
|
||||
|
|
|
@ -1,41 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* Manage the Seller status.
|
||||
* Status of the ApplePay merchant connection.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\WcGateway\Helper
|
||||
* @package WooCommerce\PayPalCommerce\Applepay\Assets
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
|
||||
|
||||
use Throwable;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
|
||||
/**
|
||||
* Class PayUponInvoiceProductStatus
|
||||
* Class AppleProductStatus
|
||||
*/
|
||||
class AppleProductStatus {
|
||||
const CAPABILITY_NAME = 'APPLE_PAY';
|
||||
const SETTINGS_KEY = 'products_apple_enabled';
|
||||
|
||||
const APPLE_STATUS_CACHE_KEY = 'apple_status_cache';
|
||||
const SETTINGS_VALUE_ENABLED = 'yes';
|
||||
const SETTINGS_VALUE_DISABLED = 'no';
|
||||
const SETTINGS_VALUE_UNDEFINED = '';
|
||||
|
||||
/**
|
||||
* The Cache.
|
||||
*
|
||||
* @var Cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Caches the status for the current load.
|
||||
* The current status stored in memory.
|
||||
*
|
||||
* @var bool|null
|
||||
*/
|
||||
private $current_status_cache;
|
||||
private $current_status = null;
|
||||
|
||||
/**
|
||||
* If there was a request failure.
|
||||
|
@ -73,24 +69,21 @@ class AppleProductStatus {
|
|||
private $api_failure_registry;
|
||||
|
||||
/**
|
||||
* PayUponInvoiceProductStatus constructor.
|
||||
* AppleProductStatus constructor.
|
||||
*
|
||||
* @param Settings $settings The Settings.
|
||||
* @param PartnersEndpoint $partners_endpoint The Partner Endpoint.
|
||||
* @param Cache $cache The cache.
|
||||
* @param State $onboarding_state The onboarding state.
|
||||
* @param FailureRegistry $api_failure_registry The API failure registry.
|
||||
*/
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
PartnersEndpoint $partners_endpoint,
|
||||
Cache $cache,
|
||||
State $onboarding_state,
|
||||
FailureRegistry $api_failure_registry
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->partners_endpoint = $partners_endpoint;
|
||||
$this->cache = $cache;
|
||||
$this->onboarding_state = $onboarding_state;
|
||||
$this->api_failure_registry = $api_failure_registry;
|
||||
}
|
||||
|
@ -100,55 +93,71 @@ class AppleProductStatus {
|
|||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function apple_is_active() : bool {
|
||||
if ( $this->onboarding_state->current_state() < State::STATE_ONBOARDED ) {
|
||||
public function is_active() : bool {
|
||||
|
||||
// If not onboarded then makes no sense to check status.
|
||||
if ( ! $this->is_onboarded() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $this->cache->has( self::APPLE_STATUS_CACHE_KEY ) ) {
|
||||
return $this->cache->get( self::APPLE_STATUS_CACHE_KEY ) === 'true';
|
||||
// If status was already checked on this request return the same result.
|
||||
if ( null !== $this->current_status ) {
|
||||
return $this->current_status;
|
||||
}
|
||||
|
||||
if ( $this->current_status_cache === true ) {
|
||||
return $this->current_status_cache;
|
||||
}
|
||||
if ( $this->settings->has( 'products_apple_enabled' ) && $this->settings->get( 'products_apple_enabled' ) === true ) {
|
||||
$this->current_status_cache = true;
|
||||
return true;
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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_cache = false;
|
||||
return $this->current_status_cache;
|
||||
$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_cache = false;
|
||||
return false;
|
||||
$this->has_request_failure = true;
|
||||
$this->current_status = false;
|
||||
return $this->current_status;
|
||||
}
|
||||
|
||||
// Check the seller status for the intended capability.
|
||||
foreach ( $seller_status->products() as $product ) {
|
||||
if ( $product->name() !== 'PAYMENT_METHODS' ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( in_array( 'APPLE_PAY', $product->capabilities(), true ) ) {
|
||||
$this->settings->set( 'products_apple_enabled', true );
|
||||
if ( in_array( self::CAPABILITY_NAME, $product->capabilities(), true ) ) {
|
||||
// Capability found, persist status and return true.
|
||||
$this->settings->set( self::SETTINGS_KEY, self::SETTINGS_VALUE_ENABLED );
|
||||
$this->settings->persist();
|
||||
$this->current_status_cache = true;
|
||||
$this->cache->set( self::APPLE_STATUS_CACHE_KEY, 'true', 3 * MONTH_IN_SECONDS );
|
||||
return true;
|
||||
|
||||
$this->current_status = true;
|
||||
return $this->current_status;
|
||||
}
|
||||
}
|
||||
$this->cache->set( self::APPLE_STATUS_CACHE_KEY, 'false', 3 * MONTH_IN_SECONDS );
|
||||
|
||||
$this->current_status_cache = false;
|
||||
return false;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the seller is onboarded.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_onboarded(): bool {
|
||||
return $this->onboarding_state->current_state() >= State::STATE_ONBOARDED;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,4 +169,24 @@ class AppleProductStatus {
|
|||
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 {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
281
modules/ppcp-applepay/src/Helper/AvailabilityNotice.php
Normal file
281
modules/ppcp-applepay/src/Helper/AvailabilityNotice.php
Normal file
|
@ -0,0 +1,281 @@
|
|||
<?php
|
||||
/**
|
||||
* Adds availability notice if applicable.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Applepay\Helper
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Applepay\Helper;
|
||||
|
||||
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
|
||||
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
|
||||
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
|
||||
|
||||
/**
|
||||
* Class AvailabilityNotice
|
||||
*/
|
||||
class AvailabilityNotice {
|
||||
|
||||
/**
|
||||
* The product status handler.
|
||||
*
|
||||
* @var AppleProductStatus
|
||||
*/
|
||||
private $product_status;
|
||||
|
||||
/**
|
||||
* Indicates if we're on the WooCommerce gateways list page.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_wc_gateways_list_page;
|
||||
|
||||
/**
|
||||
* Indicates if we're on a PPCP Settings page.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_ppcp_settings_page;
|
||||
|
||||
/**
|
||||
* Indicates if ApplePay is available to be enabled.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_available;
|
||||
|
||||
/**
|
||||
* Indicates if this server is supported for ApplePay.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_server_supported;
|
||||
|
||||
/**
|
||||
* Indicates if the merchant is validated for ApplePay.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $is_merchant_validated;
|
||||
|
||||
/**
|
||||
* The button.
|
||||
*
|
||||
* @var ApplePayButton
|
||||
*/
|
||||
private $button;
|
||||
|
||||
/**
|
||||
* Class ApmProductStatus constructor.
|
||||
*
|
||||
* @param AppleProductStatus $product_status The product status handler.
|
||||
* @param bool $is_wc_gateways_list_page Indicates if we're on the WooCommerce gateways list page.
|
||||
* @param bool $is_ppcp_settings_page Indicates if we're on a PPCP Settings page.
|
||||
* @param bool $is_available Indicates if ApplePay is available to be enabled.
|
||||
* @param bool $is_server_supported Indicates if this server is supported for ApplePay.
|
||||
* @param bool $is_merchant_validated Indicates if the merchant is validated for ApplePay.
|
||||
* @param ApplePayButton $button The button.
|
||||
*/
|
||||
public function __construct(
|
||||
AppleProductStatus $product_status,
|
||||
bool $is_wc_gateways_list_page,
|
||||
bool $is_ppcp_settings_page,
|
||||
bool $is_available,
|
||||
bool $is_server_supported,
|
||||
bool $is_merchant_validated,
|
||||
ApplePayButton $button
|
||||
) {
|
||||
$this->product_status = $product_status;
|
||||
$this->is_wc_gateways_list_page = $is_wc_gateways_list_page;
|
||||
$this->is_ppcp_settings_page = $is_ppcp_settings_page;
|
||||
$this->is_available = $is_available;
|
||||
$this->is_server_supported = $is_server_supported;
|
||||
$this->is_merchant_validated = $is_merchant_validated;
|
||||
$this->button = $button;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds availability notice if applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function execute(): void {
|
||||
if ( ! $this->should_display() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We need to check is active before checking failure requests, otherwise failure status won't be set.
|
||||
$is_active = $this->product_status->is_active();
|
||||
|
||||
if ( $this->product_status->has_request_failure() ) {
|
||||
$this->add_seller_status_failure_notice();
|
||||
} elseif ( ! $is_active ) {
|
||||
$this->add_not_available_notice();
|
||||
}
|
||||
|
||||
if ( ! $this->is_available ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $this->is_server_supported ) {
|
||||
$this->add_server_not_supported_notice();
|
||||
}
|
||||
|
||||
if ( ! $this->button->is_enabled() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $this->is_merchant_validated ) {
|
||||
$this->add_merchant_not_validated_notice();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the message should display.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function should_display(): bool {
|
||||
if ( ! $this->product_status->is_onboarded() ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! $this->is_wc_gateways_list_page && ! $this->is_ppcp_settings_page ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds seller status failure notice.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_seller_status_failure_notice(): void {
|
||||
add_filter(
|
||||
Repository::NOTICES_FILTER,
|
||||
/**
|
||||
* Adds seller status notice.
|
||||
*
|
||||
* @param array $notices The notices.
|
||||
* @return array
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
$message = sprintf(
|
||||
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
|
||||
__(
|
||||
'<p>Notice: We could not determine your PayPal seller status to list your available features. Disconnect and reconnect your PayPal account through our %1$sonboarding process%2$s to resolve this.</p><p>Don\'t worry if you cannot use the %1$sonboarding process%2$s; most functionalities available to your account should work.</p>',
|
||||
'woocommerce-paypal-payments'
|
||||
),
|
||||
'<a href="https://woocommerce.com/document/woocommerce-paypal-payments/#connect-paypal-account" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
// Name the key so it can be overridden in other modules.
|
||||
$notices['error_product_status'] = new Message( $message, 'warning', true, 'ppcp-notice-wrapper' );
|
||||
return $notices;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds not available notice.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_not_available_notice(): void {
|
||||
add_filter(
|
||||
Repository::NOTICES_FILTER,
|
||||
/**
|
||||
* Adds ApplePay not available notice.
|
||||
*
|
||||
* @param array $notices The notices.
|
||||
* @return array
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
$message = sprintf(
|
||||
__(
|
||||
'Apple Pay is not available on your PayPal seller account.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
|
||||
$notices[] = new Message( $message, 'warning', true, 'ppcp-notice-wrapper' );
|
||||
return $notices;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds ApplePay server not supported notice.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_server_not_supported_notice(): void {
|
||||
add_filter(
|
||||
Repository::NOTICES_FILTER,
|
||||
/**
|
||||
* Adds ApplePay server not supported notice.
|
||||
*
|
||||
* @param array $notices The notices.
|
||||
* @return array
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
$message = sprintf(
|
||||
__(
|
||||
'Apple Pay is not supported on this server. Please contact your hosting provider to enable it.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
|
||||
$notices[] = new Message( $message, 'error', true, 'ppcp-notice-wrapper' );
|
||||
return $notices;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds ApplePay merchant not validated notice.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function add_merchant_not_validated_notice(): void {
|
||||
add_filter(
|
||||
Repository::NOTICES_FILTER,
|
||||
/**
|
||||
* Adds ApplePay merchant not validated notice.
|
||||
*
|
||||
* @param array $notices The notices.
|
||||
* @return array
|
||||
*
|
||||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
$message = sprintf(
|
||||
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag for the well-known file, %3$s and %4$s are the opening and closing of HTML <a> tag for the help document.
|
||||
__(
|
||||
'Apple Pay Validation Error. Please ensure the presentment of the correct %1$sdomain association file%2$s for Apple to validate your domain. %3$sLearn more%4$s about the Apple Pay requirements',
|
||||
'woocommerce-paypal-payments'
|
||||
),
|
||||
'<a href="/.well-known/apple-developer-merchantid-domain-association" target="_blank">',
|
||||
'</a>',
|
||||
'<a href="https://woocommerce.com/document/woocommerce-paypal-payments/#apple-pay" target="_blank">',
|
||||
'</a>'
|
||||
);
|
||||
|
||||
$notices[] = new Message( $message, 'error', true, 'ppcp-notice-wrapper' );
|
||||
return $notices;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -45,7 +45,6 @@ class GooglepayModule implements ModuleInterface {
|
|||
function( Settings $settings = null ) use ( $c ): void {
|
||||
$apm_status = $c->get( 'googlepay.helpers.apm-product-status' );
|
||||
assert( $apm_status instanceof ApmProductStatus );
|
||||
|
||||
$apm_status->clear( $settings );
|
||||
}
|
||||
);
|
||||
|
|
|
@ -107,7 +107,6 @@ class AvailabilityNotice {
|
|||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
|
||||
$message = sprintf(
|
||||
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
|
||||
__(
|
||||
|
@ -142,7 +141,6 @@ class AvailabilityNotice {
|
|||
* @psalm-suppress MissingClosureParamType
|
||||
*/
|
||||
static function ( $notices ): array {
|
||||
|
||||
$message = sprintf(
|
||||
__(
|
||||
'Google Pay is not available on your PayPal seller account.',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue