mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
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:
commit
f3b1a406e2
8 changed files with 196 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* ApmApplies helper.
|
* ApmApplies helper.
|
||||||
|
* Checks if ApplePay is available for a given country and currency.
|
||||||
*
|
*
|
||||||
* @package WooCommerce\PayPalCommerce\ApplePay\Helper
|
* @package WooCommerce\PayPalCommerce\ApplePay\Helper
|
||||||
*/
|
*/
|
||||||
|
@ -15,7 +16,7 @@ namespace WooCommerce\PayPalCommerce\Applepay\Helper;
|
||||||
class ApmApplies {
|
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
|
* @var array
|
||||||
*/
|
*/
|
||||||
|
@ -38,7 +39,7 @@ class ApmApplies {
|
||||||
/**
|
/**
|
||||||
* ApmApplies constructor.
|
* 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 $currency 3-letter currency code of the shop.
|
||||||
* @param string $country 2-letter country code of the shop.
|
* @param string $country 2-letter country code of the shop.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Properties of the GooglePay module.
|
* ApmApplies helper.
|
||||||
|
* Checks if GooglePay is available for a given country and currency.
|
||||||
*
|
*
|
||||||
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
|
* @package WooCommerce\PayPalCommerce\Googlepay\Helper
|
||||||
*/
|
*/
|
||||||
|
@ -15,7 +16,7 @@ namespace WooCommerce\PayPalCommerce\Googlepay\Helper;
|
||||||
class ApmApplies {
|
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
|
* @var array
|
||||||
*/
|
*/
|
||||||
|
@ -38,7 +39,7 @@ class ApmApplies {
|
||||||
/**
|
/**
|
||||||
* DccApplies constructor.
|
* 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 $currency 3-letter currency code of the shop.
|
||||||
* @param string $country 2-letter country code of the shop.
|
* @param string $country 2-letter country code of the shop.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -353,5 +353,41 @@ document.addEventListener(
|
||||||
}, 'card'));
|
}, '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);
|
||||||
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\Common\Pattern\SingletonDecorator;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
|
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
use WooCommerce\PayPalCommerce\WcGateway\Admin\FeesRenderer;
|
||||||
|
@ -1003,6 +1004,13 @@ return array(
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$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 {
|
'wcgateway.transaction-url-sandbox' => static function ( ContainerInterface $container ): string {
|
||||||
return 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
|
return 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
|
||||||
|
|
|
@ -10,9 +10,11 @@ declare(strict_types=1);
|
||||||
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
|
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
|
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SettingsPageAssets
|
* Class SettingsPageAssets
|
||||||
|
@ -237,6 +239,13 @@ class SettingsPageAssets {
|
||||||
'disabled_sources' => $this->disabled_sources,
|
'disabled_sources' => $this->disabled_sources,
|
||||||
'all_funding_sources' => $this->all_funding_sources,
|
'all_funding_sources' => $this->all_funding_sources,
|
||||||
'components' => array( 'buttons', 'funding-eligibility', 'messages' ),
|
'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',
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -390,6 +390,16 @@ return function ( ContainerInterface $container, array $fields ): array {
|
||||||
'</a>'
|
'</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(
|
'ppcp_dcc_status' => array(
|
||||||
'title' => __( 'Advanced Credit and Debit Card Payments', 'woocommerce-paypal-payments' ),
|
'title' => __( 'Advanced Credit and Debit Card Payments', 'woocommerce-paypal-payments' ),
|
||||||
'type' => 'ppcp-text',
|
'type' => 'ppcp-text',
|
||||||
|
|
|
@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
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(
|
add_action(
|
||||||
'woocommerce_paypal_payments_gateway_migrate',
|
'woocommerce_paypal_payments_gateway_migrate',
|
||||||
static function () use ( $c ) {
|
static function () use ( $c ) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue