Ensure WC payment token does not exist before creating it

This commit is contained in:
Emili Castells Guasch 2023-09-04 16:33:04 +02:00
parent fa60c3c8f5
commit e819dc68df
5 changed files with 93 additions and 45 deletions

View file

@ -56,10 +56,14 @@ return array(
'vaulting.payment-token-factory' => function( ContainerInterface $container ): PaymentTokenFactory {
return new PaymentTokenFactory();
},
'vaulting.payment-token-helper' => function( ContainerInterface $container ): PaymentTokenHelper {
return new PaymentTokenHelper();
},
'vaulting.payment-tokens-migration' => function( ContainerInterface $container ): PaymentTokensMigration {
return new PaymentTokensMigration(
$container->get( 'vaulting.payment-token-factory' ),
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'vaulting.payment-token-helper' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},

View file

@ -0,0 +1,33 @@
<?php
/**
* Payment Tokens helper methods.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
/**
* Class PaymentTokenHelper
*/
class PaymentTokenHelper {
/**
* Checks if given PayPal token exist as WC Payment Token.
*
* @param array $wc_tokens WC Payment Tokens.
* @param string $token_id PayPal Token ID.
* @return bool
*/
public function token_exist( array $wc_tokens, string $token_id ): bool {
foreach ( $wc_tokens as $wc_token ) {
if ( $wc_token->get_token() === $token_id ) {
return true;
}
}
return false;
}
}

View file

@ -35,6 +35,13 @@ class PaymentTokensMigration {
*/
private $payment_token_repository;
/**
* The payment token helper.
*
* @var PaymentTokenHelper
*/
private $payment_token_helper;
/**
* The logger.
*
@ -47,16 +54,19 @@ class PaymentTokensMigration {
*
* @param PaymentTokenFactory $payment_token_factory The payment token factory.
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param PaymentTokenHelper $payment_token_helper The payment token helper.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
PaymentTokenFactory $payment_token_factory,
PaymentTokenRepository $payment_token_repository,
PaymentTokenHelper $payment_token_helper,
LoggerInterface $logger
) {
$this->payment_token_factory = $payment_token_factory;
$this->payment_token_repository = $payment_token_repository;
$this->logger = $logger;
$this->payment_token_helper = $payment_token_helper;
}
/**
@ -72,7 +82,7 @@ class PaymentTokensMigration {
foreach ( $tokens as $token ) {
if ( isset( $token->source()->card ) ) {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id, CreditCardGateway::ID );
if ( $this->token_exist( $wc_tokens, $token ) ) {
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token->id() ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}
@ -97,7 +107,7 @@ class PaymentTokensMigration {
}
} elseif ( $token->source()->paypal ) {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id, PayPalGateway::ID );
if ( $this->token_exist( $wc_tokens, $token ) ) {
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token->id() ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}
@ -126,21 +136,4 @@ class PaymentTokensMigration {
}
}
}
/**
* Checks if given PayPal token exist as WC Payment Token.
*
* @param array $wc_tokens WC Payment Tokens.
* @param PaymentToken $token PayPal Token ID.
* @return bool
*/
private function token_exist( array $wc_tokens, PaymentToken $token ): bool {
foreach ( $wc_tokens as $wc_token ) {
if ( $wc_token->get_token() === $token->id() ) {
return true;
}
}
return false;
}
}