Merge branch 'PCP-591-save-and-display-vaulted-payment-methods-in-woo-commerce-native-endpoint' into PCP-991-detach-vaulting-from-wc-subscriptions-support

This commit is contained in:
Emili Castells Guasch 2023-04-24 10:47:52 +02:00
commit 5913ab8540
4 changed files with 32 additions and 28 deletions

View file

@ -11,6 +11,8 @@ namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -62,14 +64,14 @@ class PaymentTokensMigration {
* @param int $id WooCommerce customer id.
*/
public function migrate_payment_tokens_for_user( int $id ):void {
$tokens = (array) get_user_meta( $id, 'ppcp-vault-token', true );
$tokens_migrated = 0;
if ( ! is_main_site() ) {
$tokens = $this->payment_token_repository->all_for_user_id( $id );
}
$tokens = $this->payment_token_repository->all_for_user_id( $id );
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id );
foreach ( $tokens as $token ) {
if ( $this->token_exist( $wc_tokens, $token ) ) {
continue;
}
if ( isset( $token->source()->card ) ) {
$payment_token_acdc = $this->payment_token_factory->create( 'acdc' );
assert( $payment_token_acdc instanceof PaymentTokenACDC );
@ -89,9 +91,6 @@ class PaymentTokensMigration {
);
continue;
}
$tokens_migrated++;
} elseif ( $token->source()->paypal ) {
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
assert( $payment_token_paypal instanceof PaymentTokenPayPal );
@ -114,13 +113,24 @@ class PaymentTokensMigration {
);
continue;
}
}
}
}
$tokens_migrated++;
/**
* 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;
}
}
if ( $tokens_migrated > 0 && count( $tokens ) === $tokens_migrated ) {
update_user_meta( $id, 'ppcp_tokens_migrated', true );
}
return false;
}
}

View file

@ -178,15 +178,9 @@ class VaultingModule implements ModuleInterface {
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$customers = new WP_User_Query(
array(
'fields' => 'ID',
'limit' => -1,
'meta_key' => 'ppcp-vault-token',
'meta_query' => array(
array(
'key' => 'ppcp_tokens_migrated',
'compare' => 'NOT EXISTS',
),
),
'fields' => 'ID',
'limit' => -1,
'meta_key' => 'ppcp-vault-token',
)
);
// phpcs:enable

View file

@ -99,7 +99,7 @@ class DCCProductStatus {
}
if ( $this->cache->has( self::DCC_STATUS_CACHE_KEY ) ) {
return (bool) $this->cache->get( self::DCC_STATUS_CACHE_KEY );
return $this->cache->get( self::DCC_STATUS_CACHE_KEY ) === 'true';
}
if ( $this->current_status_cache === true ) {
@ -135,7 +135,7 @@ class DCCProductStatus {
$this->settings->set( 'products_dcc_enabled', true );
$this->settings->persist();
$this->current_status_cache = true;
$this->cache->set( self::DCC_STATUS_CACHE_KEY, true, 3 * MONTH_IN_SECONDS );
$this->cache->set( self::DCC_STATUS_CACHE_KEY, 'true', 3 * MONTH_IN_SECONDS );
return true;
}
}
@ -144,7 +144,7 @@ class DCCProductStatus {
if ( $this->dcc_applies->for_country_currency() ) {
$expiration = 3 * HOUR_IN_SECONDS;
}
$this->cache->set( self::DCC_STATUS_CACHE_KEY, false, $expiration );
$this->cache->set( self::DCC_STATUS_CACHE_KEY, 'false', $expiration );
$this->current_status_cache = false;
return false;

View file

@ -88,7 +88,7 @@ class PayUponInvoiceProductStatus {
}
if ( $this->cache->has( self::PUI_STATUS_CACHE_KEY ) ) {
return (bool) $this->cache->get( self::PUI_STATUS_CACHE_KEY );
return $this->cache->get( self::PUI_STATUS_CACHE_KEY ) === 'true';
}
if ( $this->current_status_cache === true ) {
@ -127,11 +127,11 @@ class PayUponInvoiceProductStatus {
$this->settings->set( 'products_pui_enabled', true );
$this->settings->persist();
$this->current_status_cache = true;
$this->cache->set( self::PUI_STATUS_CACHE_KEY, true, 3 * MONTH_IN_SECONDS );
$this->cache->set( self::PUI_STATUS_CACHE_KEY, 'true', 3 * MONTH_IN_SECONDS );
return true;
}
}
$this->cache->set( self::PUI_STATUS_CACHE_KEY, false, 3 * MONTH_IN_SECONDS );
$this->cache->set( self::PUI_STATUS_CACHE_KEY, 'false', 3 * MONTH_IN_SECONDS );
$this->current_status_cache = false;
return false;