mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
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:
commit
e2839f63f6
4 changed files with 80 additions and 18 deletions
|
@ -82,7 +82,8 @@ import {
|
||||||
renderFields( cardFields );
|
renderFields( cardFields );
|
||||||
}
|
}
|
||||||
|
|
||||||
const placeOrderButton = document.querySelector( '#place_order' );
|
const placeOrderButton =
|
||||||
|
document.querySelector( '#place_order' );
|
||||||
placeOrderButton?.addEventListener( 'click', ( event ) => {
|
placeOrderButton?.addEventListener( 'click', ( event ) => {
|
||||||
const cardPaymentToken = document.querySelector(
|
const cardPaymentToken = document.querySelector(
|
||||||
'input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked'
|
'input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked'
|
||||||
|
|
|
@ -13,6 +13,7 @@ use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\SavePaymentMethodsApplies;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\SavePaymentMethodsApplies;
|
||||||
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Service\PaymentMethodTokensChecker;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
|
@ -108,4 +109,7 @@ return array(
|
||||||
$container->get( 'api.endpoint.payment-method-tokens' )
|
$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' ) );
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -22,6 +22,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentTokenForGuest;
|
||||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
|
||||||
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Service\PaymentMethodTokensChecker;
|
||||||
use WooCommerce\PayPalCommerce\Vaulting\WooCommercePaymentTokens;
|
use WooCommerce\PayPalCommerce\Vaulting\WooCommercePaymentTokens;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
|
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(
|
add_action(
|
||||||
'woocommerce_add_payment_method_form_bottom',
|
'woocommerce_add_payment_method_form_bottom',
|
||||||
function () {
|
function () use ( $c ) {
|
||||||
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
|
if ( ! is_user_logged_in() || ! is_add_payment_method_page() ) {
|
||||||
return;
|
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>';
|
echo '<div id="ppc-button-' . esc_attr( PayPalGateway::ID ) . '-save-payment-method"></div>';
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue