Merge pull request #1902 from woocommerce/PCP-2426-add-button-to-reload-feature-eligibility-status-from-connection-tab

Add button to reload feature eligibility status from Connection tab (2426)
This commit is contained in:
Emili Castells 2023-12-13 16:54:22 +01:00 committed by GitHub
commit f3b1a406e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 196 additions and 5 deletions

View file

@ -1,6 +1,7 @@
<?php
/**
* ApmApplies helper.
* Checks if ApplePay is available for a given country and currency.
*
* @package WooCommerce\PayPalCommerce\ApplePay\Helper
*/
@ -15,7 +16,7 @@ namespace WooCommerce\PayPalCommerce\Applepay\Helper;
class ApmApplies {
/**
* The matrix which countries and currency combinations can be used for DCC.
* The matrix which countries and currency combinations can be used for ApplePay.
*
* @var array
*/
@ -38,7 +39,7 @@ class ApmApplies {
/**
* ApmApplies constructor.
*
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for DCC.
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for ApplePay.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
*/

View file

@ -1,6 +1,7 @@
<?php
/**
* Properties of the GooglePay module.
* ApmApplies helper.
* Checks if GooglePay is available for a given country and currency.
*
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
*/
@ -15,7 +16,7 @@ namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
class ApmApplies {
/**
* The matrix which countries and currency combinations can be used for DCC.
* The matrix which countries and currency combinations can be used for GooglePay.
*
* @var array
*/
@ -38,7 +39,7 @@ class ApmApplies {
/**
* DccApplies constructor.
*
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for DCC.
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used for GooglePay.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
*/

View file

@ -353,5 +353,41 @@ document.addEventListener(
}, 'card'));
});
}
// Logic to handle the "Check available features" button.
((props) => {
const $btn = jQuery(props.button);
$btn.click(async () => {
$btn.prop('disabled', true);
const response = await fetch(
props.endpoint,
{
method: 'POST',
credentials: 'same-origin',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(
{
nonce: props.nonce,
}
)
}
);
const responseData = await response.json();
if (!responseData.success) {
alert(responseData.data.message);
$btn.prop('disabled', false);
} else {
window.location.reload();
}
});
})(PayPalCommerceGatewaySettings.ajax.refresh_feature_status);
}
);

View file

@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\Common\Pattern\SingletonDecorator;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
@ -1003,6 +1004,13 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.endpoint.refresh-feature-status' => static function ( ContainerInterface $container ) : RefreshFeatureStatusEndpoint {
return new RefreshFeatureStatusEndpoint(
$container->get( 'wcgateway.settings' ),
new Cache( 'ppcp-timeout' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'wcgateway.transaction-url-sandbox' => static function ( ContainerInterface $container ): string {
return 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';

View file

@ -10,9 +10,11 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
/**
* Class SettingsPageAssets
@ -237,6 +239,13 @@ class SettingsPageAssets {
'disabled_sources' => $this->disabled_sources,
'all_funding_sources' => $this->all_funding_sources,
'components' => array( 'buttons', 'funding-eligibility', 'messages' ),
'ajax' => array(
'refresh_feature_status' => array(
'endpoint' => \WC_AJAX::get_endpoint( RefreshFeatureStatusEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( RefreshFeatureStatusEndpoint::nonce() ),
'button' => '.ppcp-refresh-feature-status',
),
),
)
)
);

View file

@ -0,0 +1,115 @@
<?php
/**
* Controls the endpoint for refreshing feature status.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Endpoint
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Endpoint;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
/**
* Class RefreshFeatureStatusEndpoint
*/
class RefreshFeatureStatusEndpoint {
const ENDPOINT = 'ppc-refresh-feature-status';
const CACHE_KEY = 'refresh_feature_status_timeout';
const TIMEOUT = 60;
/**
* The settings.
*
* @var ContainerInterface
*/
protected $settings;
/**
* The cache.
*
* @var Cache
*/
protected $cache;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* RefreshFeatureStatusEndpoint constructor.
*
* @param ContainerInterface $settings The settings.
* @param Cache $cache The cache.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
ContainerInterface $settings,
Cache $cache,
LoggerInterface $logger
) {
$this->settings = $settings;
$this->cache = $cache;
$this->logger = $logger;
}
/**
* Returns the nonce.
*
* @return string
*/
public static function nonce(): string {
return self::ENDPOINT;
}
/**
* Handles the incoming request.
*/
public function handle_request(): void {
$now = time();
$last_request_time = $this->cache->get( self::CACHE_KEY ) ?: 0;
$seconds_missing = $last_request_time + self::TIMEOUT - $now;
if ( ! $this->verify_nonce() ) {
wp_send_json_error(
array(
'message' => __( 'Expired request.', 'woocommerce-paypal-payments' ),
)
);
}
if ( $seconds_missing > 0 ) {
wp_send_json_error(
array(
'message' => sprintf(
// translators: %1$s is the number of seconds remaining.
__( 'Wait %1$s seconds before trying again.', 'woocommerce-paypal-payments' ),
$seconds_missing
),
)
);
}
$this->cache->set( self::CACHE_KEY, $now, self::TIMEOUT );
do_action( 'woocommerce_paypal_payments_clear_apm_product_status', $this->settings );
wp_send_json_success();
}
/**
* Verifies the nonce.
*
* @return bool
*/
private function verify_nonce(): bool {
$json = json_decode( file_get_contents( 'php://input' ) ?: '', true );
return wp_verify_nonce( $json['nonce'] ?? '', self::nonce() ) !== false;
}
}

View file

@ -390,6 +390,16 @@ return function ( ContainerInterface $container, array $fields ): array {
'</a>'
),
),
'refresh_feature_status' => array(
'title' => __( 'Refresh feature availability status', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' => '<button type="button" class="button ppcp-refresh-feature-status">' . esc_html__( 'Check available features', 'woocommerce-paypal-payments' ) . '</button>',
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
),
'ppcp_dcc_status' => array(
'title' => __( 'Advanced Credit and Debit Card Payments', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',

View file

@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
@ -255,6 +256,16 @@ class WCGatewayModule implements ModuleInterface {
}
);
add_action(
'wc_ajax_' . RefreshFeatureStatusEndpoint::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'wcgateway.endpoint.refresh-feature-status' );
assert( $endpoint instanceof RefreshFeatureStatusEndpoint );
$endpoint->handle_request();
}
);
add_action(
'woocommerce_paypal_payments_gateway_migrate',
static function () use ( $c ) {