Skip to content

Commit

Permalink
Add mechanism to refresh feature status.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosilva-pt committed Dec 12, 2023
1 parent be28f81 commit d2abeb5
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 5 deletions.
5 changes: 3 additions & 2 deletions modules/ppcp-applepay/src/Helper/ApmApplies.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* ApmApplies helper.
* Checks if ApplePay is available for a given country and currency.
*
* @package WooCommerce\PayPalCommerce\ApplePay\Helper
*/
Expand All @@ -15,7 +16,7 @@
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
*/
Expand All @@ -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.
*/
Expand Down
7 changes: 4 additions & 3 deletions modules/ppcp-googlepay/src/Helper/ApmApplies.php
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -15,7 +16,7 @@
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
*/
Expand All @@ -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.
*/
Expand Down
37 changes: 37 additions & 0 deletions modules/ppcp-wc-gateway/resources/js/gateway-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,5 +353,42 @@ document.addEventListener(
}, 'card'));
});
}


(() => {
const props = PayPalCommerceGatewaySettings.ajax.refresh_feature_status;
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();
}
});

})();

}
);
8 changes: 8 additions & 0 deletions modules/ppcp-wc-gateway/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -1003,6 +1004,13 @@ static function ( ContainerInterface $container ): AuthorizeOrderActionNotice {
$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';
Expand Down
9 changes: 9 additions & 0 deletions modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
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
Expand Down Expand Up @@ -237,6 +239,13 @@ private function register_paypal_admin_assets(): void {
'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',
),
),
)
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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 ( $seconds_missing > 0 ) {
$response = array(
'message' => sprintf(
// translators: %1$s is the number of seconds remaining.
__( 'Wait %1$s seconds before trying again.', 'woocommerce-paypal-payments' ),
$seconds_missing
),
);
wp_send_json_error( $response );
}

$this->cache->set( self::CACHE_KEY, $now, self::TIMEOUT );
do_action( 'woocommerce_paypal_payments_clear_apm_product_status', $this->settings );
wp_send_json_success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@
'</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',
Expand Down
11 changes: 11 additions & 0 deletions modules/ppcp-wc-gateway/src/WCGatewayModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -255,6 +256,16 @@ static function () use ( $c ) {
}
);

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 ) {
Expand Down

0 comments on commit d2abeb5

Please sign in to comment.