Merge pull request #658 from woocommerce/PCP-704-1.9.0-test3-remove-page-reload-a

[PUI] Remove page reload after clicking on "Onboard with Pay Upon Invoice" checkbox
This commit is contained in:
Emili Castells 2022-06-01 09:55:48 +02:00 committed by GitHub
commit 2d518503a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 20 deletions

View file

@ -87,7 +87,19 @@ const ppcp_onboarding = {
}).then((res)=>{
return res.json();
}).then((data)=>{
location.reload();
if (!data.success) {
alert('Could not update signup buttons: ' + JSON.stringify(data));
return;
}
buttons.forEach((element) => {
for (let [key, value] of Object.entries(data.data.signup_links)) {
key = 'connect-to' + key.replace(/-/g, '');
if(key === element.id) {
element.setAttribute('href', value);
}
}
});
});
})
},

View file

@ -191,6 +191,9 @@ return array(
return new PayUponInvoiceEndpoint(
$container->get( 'wcgateway.settings' ),
$container->get( 'button.request-data' ),
$container->get( 'onboarding.signup-link-cache' ),
$container->get( 'onboarding.render' ),
$container->get( 'onboarding.signup-link-ids' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
@ -213,6 +216,14 @@ return array(
'onboarding.signup-link-cache' => static function( ContainerInterface $container ): Cache {
return new Cache( 'ppcp-paypal-signup-link' );
},
'onboarding.signup-link-ids' => static function ( ContainerInterface $container ): array {
return array(
'production-ppcp',
'production-express_checkout',
'sandbox-ppcp',
'sandbox-express_checkout',
);
},
'onboarding.render' => static function ( ContainerInterface $container ) : OnboardingRenderer {
$partner_referrals = $container->get( 'api.endpoint.partner-referrals-production' );
$partner_referrals_sandbox = $container->get( 'api.endpoint.partner-referrals-sandbox' );

View file

@ -11,8 +11,10 @@ namespace WooCommerce\PayPalCommerce\Onboarding\Endpoint;
use Exception;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
@ -35,6 +37,27 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
*/
protected $request_data;
/**
* The signup link cache.
*
* @var Cache
*/
protected $signup_link_cache;
/**
* The onboarding renderer.
*
* @var OnboardingRenderer
*/
protected $onboarding_renderer;
/**
* Signup link ids.
*
* @var array
*/
protected $signup_link_ids;
/**
* The logger.
*
@ -45,14 +68,27 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
/**
* PayUponInvoiceEndpoint constructor.
*
* @param Settings $settings The settings.
* @param RequestData $request_data The request data.
* @param LoggerInterface $logger The logger.
* @param Settings $settings The settings.
* @param RequestData $request_data The request data.
* @param Cache $signup_link_cache The signup link cache.
* @param OnboardingRenderer $onboarding_renderer The onboarding renderer.
* @param array $signup_link_ids Signup link ids.
* @param LoggerInterface $logger The logger.
*/
public function __construct( Settings $settings, RequestData $request_data, LoggerInterface $logger ) {
$this->settings = $settings;
$this->request_data = $request_data;
$this->logger = $logger;
public function __construct(
Settings $settings,
RequestData $request_data,
Cache $signup_link_cache,
OnboardingRenderer $onboarding_renderer,
array $signup_link_ids,
LoggerInterface $logger
) {
$this->settings = $settings;
$this->request_data = $request_data;
$this->signup_link_cache = $signup_link_cache;
$this->onboarding_renderer = $onboarding_renderer;
$this->logger = $logger;
$this->signup_link_ids = $signup_link_ids;
}
/**
@ -71,18 +107,33 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
* @throws NotFoundException When order not found or handling failed.
*/
public function handle_request(): bool {
$signup_links = array();
try {
$data = $this->request_data->read_request( $this->nonce() );
$this->settings->set( 'ppcp-onboarding-pui', $data['checked'] );
$this->settings->persist();
foreach ( $this->signup_link_ids as $key ) {
if ( $this->signup_link_cache->has( $key ) ) {
$this->signup_link_cache->delete( $key );
}
}
foreach ( $this->signup_link_ids as $key ) {
$parts = explode( '-', $key );
$is_production = 'production' === $parts[0];
$products = 'ppcp' === $parts[1] ? array( 'PPCP' ) : array( 'EXPRESS_CHECKOUT' );
$signup_links[ $key ] = $this->onboarding_renderer->get_signup_link( $is_production, $products );
}
} catch ( Exception $exception ) {
$this->logger->error( $exception->getMessage() );
}
wp_send_json_success(
array(
$this->settings->get( 'ppcp-onboarding-pui' ),
'onboarding_pui' => $this->settings->get( 'ppcp-onboarding-pui' ),
'signup_links' => $signup_links,
)
);

View file

@ -95,7 +95,7 @@ class OnboardingRenderer {
->data();
$environment = $is_production ? 'production' : 'sandbox';
$product = isset( $data['products']['PPCP'] ) ? 'ppcp' : 'express-checkout';
$product = 'PPCP' === $data['products'][0] ? 'ppcp' : 'express_checkout';
if ( $this->cache->has( $environment . '-' . $product ) ) {
return $this->cache->get( $environment . '-' . $product );
}

View file

@ -220,7 +220,18 @@ return array(
$bearer = $container->get( 'api.bearer' );
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
$signup_link_cache = $container->get( 'onboarding.signup-link-cache' );
return new SettingsListener( $settings, $fields, $webhook_registrar, $cache, $state, $bearer, $page_id, $signup_link_cache );
$signup_link_ids = $container->get( 'onboarding.signup-link-ids' );
return new SettingsListener(
$settings,
$fields,
$webhook_registrar,
$cache,
$state,
$bearer,
$page_id,
$signup_link_cache,
$signup_link_ids
);
},
'wcgateway.order-processor' => static function ( ContainerInterface $container ): OrderProcessor {

View file

@ -88,6 +88,13 @@ class SettingsListener {
*/
protected $signup_link_cache;
/**
* Signup link ids
*
* @var array
*/
protected $signup_link_ids;
/**
* SettingsListener constructor.
*
@ -99,6 +106,7 @@ class SettingsListener {
* @param Bearer $bearer The bearer.
* @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page.
* @param Cache $signup_link_cache The signup link cache.
* @param array $signup_link_ids Signup link ids.
*/
public function __construct(
Settings $settings,
@ -108,7 +116,8 @@ class SettingsListener {
State $state,
Bearer $bearer,
string $page_id,
Cache $signup_link_cache
Cache $signup_link_cache,
array $signup_link_ids
) {
$this->settings = $settings;
@ -119,6 +128,7 @@ class SettingsListener {
$this->bearer = $bearer;
$this->page_id = $page_id;
$this->signup_link_cache = $signup_link_cache;
$this->signup_link_ids = $signup_link_ids;
}
/**
@ -271,13 +281,7 @@ class SettingsListener {
) ) {
$this->webhook_registrar->unregister();
$keys = array(
'production-ppcp',
'production-express-checkout',
'sandbox-ppcp',
'sandbox-express-checkout',
);
foreach ( $keys as $key ) {
foreach ( $this->signup_link_ids as $key ) {
if ( $this->signup_link_cache->has( $key ) ) {
$this->signup_link_cache->delete( $key );
}

View file

@ -37,6 +37,7 @@ class SettingsListenerTest extends ModularTestCase
$state->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$bearer = Mockery::mock(Bearer::class);
$signup_link_cache = Mockery::mock(Cache::class);
$signup_link_ids = array();
$testee = new SettingsListener(
$settings,
@ -46,7 +47,8 @@ class SettingsListenerTest extends ModularTestCase
$state,
$bearer,
PayPalGateway::ID,
$signup_link_cache
$signup_link_cache,
$signup_link_ids
);
$_GET['section'] = PayPalGateway::ID;