Do not display PayPal button is payment already exist

This commit is contained in:
Emili Castells Guasch 2025-06-06 12:26:38 +02:00
parent 87a8dcc00b
commit 2f625873f8
No known key found for this signature in database
4 changed files with 85 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(
@ -111,4 +112,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\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
@ -360,11 +361,19 @@ class SavePaymentMethodsModule implements ServiceModule, ExtendingModule, Execut
add_action(
'woocommerce_add_payment_method_form_bottom',
function () {
function () use ( $c ) {
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
return;
}
// Do not display PayPal button if the user already has a PayPal payment token.
$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,53 @@
<?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.
*/
class PaymentMethodTokensChecker {
/**
* Payment method tokens endpoint.
*
* @var PaymentTokensEndpoint
*/
private PaymentTokensEndpoint $payment_method_tokens_endpoint;
/**
* PaymentMethodTokensChecker constructor.
*
* @param PaymentTokensEndpoint $payment_method_tokens_endpoint 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 {
$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;
}
}