Refactor onboarding link update endpoint refactor.

This commit is contained in:
Pedro Silva 2023-09-26 13:58:33 +01:00
parent 26a2b80b1b
commit 43136ecf3c
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
10 changed files with 168 additions and 85 deletions

View file

@ -147,6 +147,19 @@ class ApplePayButton implements ButtonInterface {
*/
public function initialize(): void {
add_filter( 'ppcp_onboarding_options', array( $this, 'add_apple_onboarding_option' ), 10, 1 );
add_filter(
'ppcp_partner_referrals_option',
function ( array $option ): array {
if ( $option['valid'] ) {
return $option;
}
if ( $option['field'] === 'ppcp-onboarding-apple' ) {
$option['valid'] = true;
$option['value'] = ( $option['value'] ? '1' : '' );
}
return $option;
}
);
add_filter(
'ppcp_partner_referrals_data',
function ( array $data ): array {
@ -206,7 +219,7 @@ class ApplePayButton implements ButtonInterface {
$checked = '';
}
return $options . '<li><label><input type="checkbox" id="ppcp-onboarding-apple" ' . $checked . '> ' .
return $options . '<li><label><input type="checkbox" id="ppcp-onboarding-apple" ' . $checked . ' data-onboarding-option="ppcp-onboarding-apple"> ' .
__( 'Onboard with ApplePay', 'woocommerce-paypal-payments' ) . '
</label></li>';

View file

@ -127,6 +127,7 @@ class Button implements ButtonInterface {
*/
public function initialize(): void {
add_filter( 'ppcp_onboarding_options', array( $this, 'add_onboarding_options' ), 10, 1 );
add_filter( 'ppcp_partner_referrals_option', array( $this, 'filter_partner_referrals_option' ), 10, 1 );
add_filter( 'ppcp_partner_referrals_data', array( $this, 'add_partner_referrals_data' ), 10, 1 );
}
@ -150,11 +151,28 @@ class Button implements ButtonInterface {
}
return $options
. '<li><label><input type="checkbox" id="ppcp-onboarding-google" ' . $checked . '> '
. '<li><label><input type="checkbox" id="ppcp-onboarding-google" ' . $checked . ' data-onboarding-option="ppcp-onboarding-google"> '
. __( 'Onboard with GooglePay', 'woocommerce-paypal-payments' )
. '</label></li>';
}
/**
* Filters a partner referrals option.
*
* @param array $option The option data.
* @return array
*/
public function filter_partner_referrals_option( array $option ): array {
if ( $option['valid'] ) {
return $option;
}
if ( $option['field'] === 'ppcp-onboarding-google' ) {
$option['valid'] = true;
$option['value'] = ( $option['value'] ? '1' : '' );
}
return $option;
}
/**
* Adds to partner referrals data.
*

View file

@ -13,69 +13,87 @@ const ppcp_onboarding = {
reload: function() {
const buttons = document.querySelectorAll(ppcp_onboarding.BUTTON_SELECTOR);
if (0 === buttons.length) {
return;
if (buttons.length > 0) {
// Add event listeners to buttons preventing link clicking if PayPal init failed.
buttons.forEach(
(element) => {
if (element.hasAttribute('data-ppcp-button-initialized')) {
return;
}
element.addEventListener(
'click',
(e) => {
if (!element.hasAttribute('data-ppcp-button-initialized') || 'undefined' === typeof window.PAYPAL) {
e.preventDefault();
}
}
);
}
);
// Clear any previous PayPal scripts.
[ppcp_onboarding.PAYPAL_JS_ID, 'signup-js', 'biz-js'].forEach(
(scriptID) => {
const scriptTag = document.getElementById(scriptID);
if (scriptTag) {
scriptTag.parentNode.removeChild(scriptTag);
}
if ('undefined' !== typeof window.PAYPAL) {
delete window.PAYPAL;
}
}
);
// Load PayPal scripts.
const paypalScriptTag = document.createElement('script');
paypalScriptTag.id = ppcp_onboarding.PAYPAL_JS_ID;
paypalScriptTag.src = PayPalCommerceGatewayOnboarding.paypal_js_url;
document.body.appendChild(paypalScriptTag);
if (ppcp_onboarding._timeout) {
clearTimeout(ppcp_onboarding._timeout);
}
ppcp_onboarding._timeout = setTimeout(
() => {
buttons.forEach((element) => { element.setAttribute('data-ppcp-button-initialized', 'true'); });
if ('undefined' !== window.PAYPAL.apps.Signup) {
window.PAYPAL.apps.Signup.render();
}
},
1000
);
}
// Add event listeners to buttons preventing link clicking if PayPal init failed.
buttons.forEach(
(element) => {
if (element.hasAttribute('data-ppcp-button-initialized')) {
return;
}
const $onboarding_inputs = function () {
return jQuery('*[data-onboarding-option]');
};
const onboarding_options = function () {
let options = {};
$onboarding_inputs().each((index, el) => {
const opt = jQuery(el).data('onboardingOption');
options[opt] = el.checked;
});
return options;
}
const disable_onboarding_options = function () {
$onboarding_inputs().each((index, el) => {
el.setAttribute('disabled', 'disabled');
});
}
const enable_onboarding_options = function () {
$onboarding_inputs().each((index, el) => {
el.removeAttribute('disabled');
});
}
const update_onboarding_options = function () {
const spinner = '<span class="spinner is-active" style="float: none;"></span>';
element.addEventListener(
'click',
(e) => {
if (!element.hasAttribute('data-ppcp-button-initialized') || 'undefined' === typeof window.PAYPAL) {
e.preventDefault();
}
}
);
}
);
// Clear any previous PayPal scripts.
[ppcp_onboarding.PAYPAL_JS_ID, 'signup-js', 'biz-js'].forEach(
(scriptID) => {
const scriptTag = document.getElementById(scriptID);
if (scriptTag) {
scriptTag.parentNode.removeChild(scriptTag);
}
if ('undefined' !== typeof window.PAYPAL) {
delete window.PAYPAL;
}
}
);
// Load PayPal scripts.
const paypalScriptTag = document.createElement('script');
paypalScriptTag.id = ppcp_onboarding.PAYPAL_JS_ID;
paypalScriptTag.src = PayPalCommerceGatewayOnboarding.paypal_js_url;
document.body.appendChild(paypalScriptTag);
if (ppcp_onboarding._timeout) {
clearTimeout(ppcp_onboarding._timeout);
}
ppcp_onboarding._timeout = setTimeout(
() => {
buttons.forEach((element) => { element.setAttribute('data-ppcp-button-initialized', 'true'); });
if ('undefined' !== window.PAYPAL.apps.Signup) {
window.PAYPAL.apps.Signup.render();
}
},
1000
);
const onboard_pui = document.querySelector('#ppcp-onboarding-pui');
const spinner = '<span class="spinner is-active" style="float: none;"></span>';
onboard_pui?.addEventListener('click', (event) => {
event.preventDefault();
onboard_pui.setAttribute('disabled', 'disabled');
disable_onboarding_options();
buttons.forEach((element) => {
element.removeAttribute('href');
element.setAttribute('disabled', 'disabled');
@ -90,7 +108,7 @@ const ppcp_onboarding = {
credentials: 'same-origin',
body: JSON.stringify({
nonce: PayPalCommerceGatewayOnboarding.pui_nonce,
checked: onboard_pui.checked
settings: onboarding_options()
})
}).then((res)=>{
return res.json();
@ -99,7 +117,6 @@ const ppcp_onboarding = {
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, '');
@ -110,9 +127,13 @@ const ppcp_onboarding = {
}
}
});
onboard_pui.removeAttribute('disabled');
enable_onboarding_options();
});
})
}
$onboarding_inputs().on('click', (event) => {
event.preventDefault();
update_onboarding_options();
});
},
loginSeller: function(env, authCode, sharedId) {

View file

@ -18,7 +18,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\PayUponInvoiceEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
use WooCommerce\PayPalCommerce\Onboarding\OnboardingRESTController;
@ -187,8 +187,8 @@ return array(
$logger
);
},
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : PayUponInvoiceEndpoint {
return new PayUponInvoiceEndpoint(
'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint {
return new UpdateSignupLinksEndpoint(
$container->get( 'wcgateway.settings' ),
$container->get( 'button.request-data' ),
$container->get( 'onboarding.signup-link-cache' ),

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Onboarding\Assets;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -155,8 +156,8 @@ class OnboardingAssets {
'error_messages' => array(
'no_credentials' => __( 'API credentials must be entered to save the settings.', 'woocommerce-paypal-payments' ),
),
'pui_endpoint' => \WC_AJAX::get_endpoint( 'ppc-pui' ),
'pui_nonce' => wp_create_nonce( 'ppc-pui' ),
'pui_endpoint' => \WC_AJAX::get_endpoint( UpdateSignupLinksEndpoint::ENDPOINT ),
'pui_nonce' => wp_create_nonce( UpdateSignupLinksEndpoint::ENDPOINT ),
);
}

View file

@ -14,14 +14,17 @@ 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\Helper\OnboardingUrl;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
/**
* Class PayUponInvoiceEndpoint
* Class UpdateSignupLinksEndpoint
*/
class PayUponInvoiceEndpoint implements EndpointInterface {
class UpdateSignupLinksEndpoint implements EndpointInterface {
const ENDPOINT = 'ppc-update-signup-links';
/**
* The settings.
@ -66,7 +69,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
protected $logger;
/**
* PayUponInvoiceEndpoint constructor.
* UpdateSignupLinksEndpoint constructor.
*
* @param Settings $settings The settings.
* @param RequestData $request_data The request data.
@ -97,7 +100,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
* @return string
*/
public static function nonce(): string {
return 'ppc-pui';
return self::ENDPOINT;
}
/**
@ -116,13 +119,23 @@ class PayUponInvoiceEndpoint implements EndpointInterface {
try {
$data = $this->request_data->read_request( $this->nonce() );
$this->settings->set( 'ppcp-onboarding-pui', $data['checked'] );
foreach ( $data['settings'] ?? array() as $field => $value ) {
$option = apply_filters( 'ppcp_partner_referrals_option', array(
'field' => $field,
'value' => $value,
'valid' => false,
) );
if ( $option['valid'] ) {
$this->settings->set( $field, $value );
}
}
$this->settings->persist();
foreach ( $this->signup_link_ids as $key ) {
if ( $this->signup_link_cache->has( $key ) ) {
$this->signup_link_cache->delete( $key );
}
( new OnboardingUrl( $this->signup_link_cache, $key, get_current_user_id() ) )->delete();
}
foreach ( $this->signup_link_ids as $key ) {

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Onboarding;
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets;
@ -96,7 +97,7 @@ class OnboardingModule implements ModuleInterface {
);
add_action(
'wc_ajax_ppc-pui',
'wc_ajax_' . UpdateSignupLinksEndpoint::ENDPOINT,
static function () use ( $c ) {
$endpoint = $c->get( 'onboarding.endpoint.pui' );
$endpoint->handle_request();

View file

@ -95,7 +95,7 @@ class OnboardingOptionsRenderer {
$checked = '';
}
return '<li><label><input type="checkbox" id="ppcp-onboarding-pui" ' . $checked . '> ' .
return '<li><label><input type="checkbox" id="ppcp-onboarding-pui" ' . $checked . ' data-onboarding-option="ppcp-onboarding-pui"> ' .
__( 'Onboard with Pay upon Invoice', 'woocommerce-paypal-payments' ) . '
</label></li>';
}

View file

@ -200,10 +200,12 @@ return array(
return $ppcp_tab ? $ppcp_tab : $section;
},
'wcgateway.settings' => static function ( ContainerInterface $container ): Settings {
$default_button_locations = $container->get( 'wcgateway.button.default-locations' );
return new Settings( $default_button_locations );
},
'wcgateway.settings' => SingletonDecorator::make(
static function ( ContainerInterface $container ): Settings {
$default_button_locations = $container->get( 'wcgateway.button.default-locations' );
return new Settings( $default_button_locations );
}
),
'wcgateway.notice.connect' => static function ( ContainerInterface $container ): ConnectAdminNotice {
$state = $container->get( 'onboarding.state' );
$settings = $container->get( 'wcgateway.settings' );

View file

@ -142,6 +142,20 @@ class PayUponInvoice {
$this->settings->persist();
}
add_filter(
'ppcp_partner_referrals_option',
function ( array $option ): array {
if ( $option['valid'] ) {
return $option;
}
if ( $option['field'] === 'ppcp-onboarding-pui' ) {
$option['valid'] = true;
$option['value'] = ( $option['value'] ? '1' : '' );
}
return $option;
}
);
add_filter(
'ppcp_partner_referrals_data',
function ( array $data ): array {