Merge pull request #3445 from woocommerce/PCP-4791-vaulting-my-account-pay-pal-gateway-and-button-should-not-be-displayed-when-customer-already-has-vaulted-pay-pal-account

PayPal button should not be displayed when customer already has vaulted PayPal account (4791)
This commit is contained in:
Emili Castells 2025-08-12 15:21:08 +02:00 committed by GitHub
commit e2839f63f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 80 additions and 18 deletions

View file

@ -82,25 +82,26 @@ import {
renderFields( cardFields );
}
const placeOrderButton = document.querySelector( '#place_order' );
const placeOrderButton =
document.querySelector( '#place_order' );
placeOrderButton?.addEventListener( 'click', ( event ) => {
const cardPaymentToken = document.querySelector(
'input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked'
)?.value;
if (
getCurrentPaymentMethod() !==
'ppcp-credit-card-gateway' ||
( cardPaymentToken && cardPaymentToken !== 'new' )
) {
return;
}
placeOrderButton.disabled = true;
event.preventDefault();
cardFields.submit().catch( ( error ) => {
console.error( error );
placeOrderButton.disabled = false;
} );
const cardPaymentToken = document.querySelector(
'input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked'
)?.value;
if (
getCurrentPaymentMethod() !==
'ppcp-credit-card-gateway' ||
( cardPaymentToken && cardPaymentToken !== 'new' )
) {
return;
}
placeOrderButton.disabled = true;
event.preventDefault();
cardFields.submit().catch( ( error ) => {
console.error( error );
placeOrderButton.disabled = false;
} );
} );
} );
}, 1000 );
} );

View file

@ -13,6 +13,7 @@ use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\SavePaymentMethodsApplies;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Service\PaymentMethodTokensChecker;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
@ -108,4 +109,7 @@ return array(
$container->get( 'api.endpoint.payment-method-tokens' )
);
},
'save-payment-methods.service.payment-method-tokens-checker' => static function ( ContainerInterface $container ): PaymentMethodTokensChecker {
return new PaymentMethodTokensChecker( $container->get( 'api.endpoint.payment-tokens' ) );
},
);

View file

@ -22,6 +22,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Service\PaymentMethodTokensChecker;
use WooCommerce\PayPalCommerce\Vaulting\WooCommercePaymentTokens;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
@ -339,13 +340,21 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
}
);
// Do not display PayPal button if the user already has a PayPal payment token.
add_action(
'woocommerce_add_payment_method_form_bottom',
function () {
function () use ( $c ) {
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
return;
}
$payment_method_tokens_checked = $c->get( 'save-payment-methods.service.payment-method-tokens-checker' );
assert( $payment_method_tokens_checked instanceof PaymentMethodTokensChecker );
$customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true );
if ( $payment_method_tokens_checked->has_paypal_payment_token( $customer_id ) ) {
return;
}
echo '<div id="ppc-button-' . esc_attr( PayPalGateway::ID ) . '-save-payment-method"></div>';
}
);

View file

@ -0,0 +1,48 @@
<?php
/**
* Service for checking payment method tokens.
*
* @package WooCommerce\PayPalCommerce\SavePaymentMethods\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavePaymentMethods\Service;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokensEndpoint;
class PaymentMethodTokensChecker {
/**
* Payment method tokens endpoint.
*
* @var PaymentTokensEndpoint
*/
private PaymentTokensEndpoint $payment_method_tokens_endpoint;
public function __construct( PaymentTokensEndpoint $payment_method_tokens_endpoint ) {
$this->payment_method_tokens_endpoint = $payment_method_tokens_endpoint;
}
/**
* Checks if customer has a saved PayPal payment token.
*
* @param string $customer_id PayPal customer ID.
* @return bool
*/
public function has_paypal_payment_token( string $customer_id ): bool {
if ( ! $customer_id ) {
return false;
}
$tokens = $this->payment_method_tokens_endpoint->payment_tokens_for_customer( $customer_id );
foreach ( $tokens as $token ) {
$payment_source = $token['payment_source']->name() ?? '';
if ( $payment_source === 'paypal' ) {
return true;
}
}
return false;
}
}