Create WC payment token if it exist in PayPal

This commit is contained in:
Emili Castells Guasch 2024-03-21 12:36:45 +01:00
parent 3de845a934
commit 082550b363
5 changed files with 99 additions and 7 deletions

View file

@ -45,7 +45,8 @@ return array(
$funding_source_renderer,
$container->get( 'wc-subscriptions.helpers.real-time-account-updater' ),
$container->get( 'wc-subscriptions.helper' ),
$container->get( 'api.endpoint.payment-tokens' )
$container->get( 'api.endpoint.payment-tokens' ),
$container->get( 'save-payment-methods.wc-payment-tokens' )
);
},
'wc-subscriptions.repository.payment-token' => static function ( ContainerInterface $container ): PaymentTokenRepository {

View file

@ -24,6 +24,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\SavePaymentMethods\WooCommercePaymentTokens;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenApplePay;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenPayPal;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
@ -141,6 +142,13 @@ class RenewalHandler {
*/
private $payment_tokens_endpoint;
/**
* WooCommerce payments tokens factory.
*
* @var WooCommercePaymentTokens
*/
private $wc_payment_tokens;
/**
* RenewalHandler constructor.
*
@ -157,6 +165,7 @@ class RenewalHandler {
* @param RealTimeAccountUpdaterHelper $real_time_account_updater_helper Real Time Account Updater helper.
* @param SubscriptionHelper $subscription_helper Subscription helper.
* @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint.
* @param WooCommercePaymentTokens $wc_payment_tokens WooCommerce payments tokens factory.
*/
public function __construct(
LoggerInterface $logger,
@ -171,7 +180,8 @@ class RenewalHandler {
FundingSourceRenderer $funding_source_renderer,
RealTimeAccountUpdaterHelper $real_time_account_updater_helper,
SubscriptionHelper $subscription_helper,
PaymentTokensEndpoint $payment_tokens_endpoint
PaymentTokensEndpoint $payment_tokens_endpoint,
WooCommercePaymentTokens $wc_payment_tokens
) {
$this->logger = $logger;
@ -187,6 +197,7 @@ class RenewalHandler {
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->subscription_helper = $subscription_helper;
$this->payment_tokens_endpoint = $payment_tokens_endpoint;
$this->wc_payment_tokens = $wc_payment_tokens;
}
/**
@ -259,9 +270,28 @@ class RenewalHandler {
$customer_tokens = array();
}
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, PayPalGateway::ID );
if ( $customer_tokens && empty( $wc_tokens ) ) {
foreach ( $customer_tokens as $customer_token ) {
if ( $customer_token['payment_source']->name() === 'paypal' ) {
$this->wc_payment_tokens->create_payment_token_paypal(
$user_id,
$customer_token['id'],
$customer_token['payment_source']->properties()->email_address ?? ''
);
}
}
}
$customer_token_ids = array();
foreach ( $customer_tokens as $customer_token ) {
$customer_token_ids[] = $customer_token['id'];
}
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, PayPalGateway::ID );
foreach ( $wc_tokens as $token ) {
if ( ! in_array( $token->get_token(), $customer_tokens, true ) ) {
if ( ! in_array( $token->get_token(), $customer_token_ids, true ) ) {
$token->delete();
continue;
}
@ -309,9 +339,27 @@ class RenewalHandler {
$customer_tokens = array();
}
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $user_id, PayPalGateway::ID );
if ( $customer_tokens && empty( $wc_tokens ) ) {
foreach ( $customer_tokens as $customer_token ) {
if ( $customer_token['payment_source']->name() === 'card' ) {
$this->wc_payment_tokens->create_payment_token_card(
get_current_user_id(),
$customer_token['id']
);
}
}
}
$customer_token_ids = array();
foreach ( $customer_tokens as $customer_token ) {
$customer_token_ids[] = $customer_token['id'];
}
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_order->get_customer_id(), CreditCardGateway::ID );
foreach ( $wc_tokens as $token ) {
if ( ! in_array( $token->get_token(), $customer_tokens, true ) ) {
if ( ! in_array( $token->get_token(), $customer_token_ids, true ) ) {
$token->delete();
}
}