Merge pull request #1719 from woocommerce/PCP-2006-google-pay-settings-improvements

Google Pay Settings improvements (2006)
This commit is contained in:
Emili Castells 2023-10-17 14:24:57 +02:00 committed by GitHub
commit c07e8eaa87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 1784 additions and 243 deletions

View file

@ -13,84 +13,102 @@ 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');
jQuery(spinner).insertAfter(element);
});
fetch(PayPalCommerceGatewayOnboarding.pui_endpoint, {
fetch(PayPalCommerceGatewayOnboarding.update_signup_links_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify({
nonce: PayPalCommerceGatewayOnboarding.pui_nonce,
checked: onboard_pui.checked
nonce: PayPalCommerceGatewayOnboarding.update_signup_links_nonce,
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

@ -19,7 +19,10 @@ document.addEventListener(
if (! toggleElement.checked) {
group.forEach( (elementToHide) => {
document.querySelector(elementToHide).style.display = 'none';
const element = document.querySelector(elementToHide);
if (element) {
element.style.display = 'none';
}
})
}
toggleElement.addEventListener(
@ -27,7 +30,10 @@ document.addEventListener(
(event) => {
if (! event.target.checked) {
group.forEach( (elementToHide) => {
document.querySelector(elementToHide).style.display = 'none';
const element = document.querySelector(elementToHide);
if (element) {
element.style.display = 'none';
}
});
return;

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;
@ -145,18 +146,18 @@ class OnboardingAssets {
*/
public function get_script_data() {
return array(
'endpoint' => \WC_AJAX::get_endpoint( LoginSellerEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( $this->login_seller_endpoint::nonce() ),
'paypal_js_url' => 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js',
'sandbox_state' => State::get_state_name( $this->state->sandbox_state() ),
'production_state' => State::get_state_name( $this->state->production_state() ),
'current_state' => State::get_state_name( $this->state->current_state() ),
'current_env' => $this->environment->current_environment(),
'error_messages' => array(
'endpoint' => \WC_AJAX::get_endpoint( LoginSellerEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( $this->login_seller_endpoint::nonce() ),
'paypal_js_url' => 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js',
'sandbox_state' => State::get_state_name( $this->state->sandbox_state() ),
'production_state' => State::get_state_name( $this->state->production_state() ),
'current_state' => State::get_state_name( $this->state->current_state() ),
'current_env' => $this->environment->current_environment(),
'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' ),
'update_signup_links_endpoint' => \WC_AJAX::get_endpoint( UpdateSignupLinksEndpoint::ENDPOINT ),
'update_signup_links_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,26 @@ 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>';
}