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

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WP_Error;
@ -96,8 +97,11 @@ class PaymentTokensEndpoint {
/**
* Returns all payment tokens for the given customer.
*
* @param string $customer_id
* @param string $customer_id PayPal customer id.
* @return array
*
* @throws RuntimeException When something went wrong with the request.
* @throws PayPalApiException When something went wrong getting the payment tokens.
*/
public function payment_tokens_for_customer( string $customer_id ): array {
$bearer = $this->bearer->bearer();
@ -124,7 +128,16 @@ class PaymentTokensEndpoint {
$tokens = array();
$payment_tokens = $json->payment_tokens ?? array();
foreach ( $payment_tokens as $payment_token ) {
$tokens[] = $payment_token->id;
$name = array_key_first( (array) $payment_token->payment_source ) ?? '';
if ( $name ) {
$tokens[] = array(
'id' => $payment_token->id,
'payment_source' => new PaymentSource(
$name,
$payment_token->payment_source->$name
),
);
}
}
return $tokens;

View file

@ -136,6 +136,7 @@ return array(
$container->get( 'wcgateway.endpoint.capture-card-payment' ),
$container->get( 'api.prefix' ),
$container->get( 'api.endpoint.payment-tokens' ),
$container->get( 'save-payment-methods.wc-payment-tokens' ),
$logger
);
},

View file

@ -20,6 +20,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\SavePaymentMethods\WooCommercePaymentTokens;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
use WooCommerce\PayPalCommerce\Vaulting\VaultedCreditCardHandler;
@ -162,6 +163,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $payment_tokens_endpoint;
/**
* WooCommerce payment tokens factory.
*
* @var WooCommercePaymentTokens
*/
private $wc_payment_tokens;
/**
* The logger.
*
@ -188,6 +196,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param CaptureCardPayment $capture_card_payment Capture card payment.
* @param string $prefix The prefix.
* @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint.
* @param WooCommercePaymentTokens $wc_payment_tokens WooCommerce payment tokens factory.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -207,6 +216,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
CaptureCardPayment $capture_card_payment,
string $prefix,
PaymentTokensEndpoint $payment_tokens_endpoint,
WooCommercePaymentTokens $wc_payment_tokens,
LoggerInterface $logger
) {
$this->id = self::ID;
@ -226,6 +236,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->capture_card_payment = $capture_card_payment;
$this->prefix = $prefix;
$this->payment_tokens_endpoint = $payment_tokens_endpoint;
$this->wc_payment_tokens = $wc_payment_tokens;
$this->logger = $logger;
if ( $state->current_state() === State::STATE_ONBOARDED ) {
@ -436,10 +447,28 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$customer_tokens = array();
}
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( get_current_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'];
}
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
foreach ( $tokens as $token ) {
if ( $token->get_id() === (int) $card_payment_token_id ) {
if ( ! in_array( $token->get_token(), $customer_tokens, true ) ) {
if ( ! in_array( $token->get_token(), $customer_token_ids, true ) ) {
$token->delete();
continue;
}

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();
}
}