Refactor Venmo and ApplePay payment tokens.

This commit is contained in:
Pedro Silva 2024-01-26 14:22:20 +00:00
parent b3b6693aa2
commit 0d4fbc4c0b
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
10 changed files with 234 additions and 70 deletions

View file

@ -0,0 +1,31 @@
<?php
/**
* WooCommerce Payment token for ApplePay.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WC_Payment_Token;
/**
* Class PaymentTokenApplePay
*/
class PaymentTokenApplePay extends WC_Payment_Token {
/**
* Token Type String.
*
* @var string
*/
protected $type = 'ApplePay';
/**
* Extra data.
*
* @var string[]
*/
protected $extra_data = array();
}

View file

@ -19,12 +19,16 @@ class PaymentTokenFactory {
*
* @param string $type The type of WC payment token.
*
* @return void|PaymentTokenPayPal
* @return void|PaymentTokenPayPal|PaymentTokenVenmo|PaymentTokenApplePay
*/
public function create( string $type ) {
switch ( $type ) {
case 'paypal':
return new PaymentTokenPayPal();
case 'venmo':
return new PaymentTokenVenmo();
case 'apple_pay':
return new PaymentTokenApplePay();
}
}
}

View file

@ -21,12 +21,19 @@ class PaymentTokenHelper {
*
* @param WC_Payment_Token[] $wc_tokens WC Payment Tokens.
* @param string $token_id Payment Token ID.
* @param ?string $class_name Class name of the token.
* @return bool
*/
public function token_exist( array $wc_tokens, string $token_id ): bool {
public function token_exist( array $wc_tokens, string $token_id, string $class_name = null ): bool {
foreach ( $wc_tokens as $wc_token ) {
if ( $wc_token->get_token() === $token_id ) {
return true;
if ( null !== $class_name ) {
if ( $wc_token instanceof $class_name ) {
return true;
}
} else {
return true;
}
}
}

View file

@ -28,8 +28,7 @@ class PaymentTokenPayPal extends WC_Payment_Token {
* @var string[]
*/
protected $extra_data = array(
'email' => '',
'payment_source' => '',
'email' => '',
);
/**
@ -49,22 +48,4 @@ class PaymentTokenPayPal extends WC_Payment_Token {
public function set_email( $email ) {
$this->add_meta_data( 'email', $email, true );
}
/**
* Get the payment source.
*
* @return string The payment source.
*/
public function get_payment_source() {
return $this->get_meta( 'payment_source' );
}
/**
* Set the payment source.
*
* @param string $payment_source The payment source.
*/
public function set_payment_source( string $payment_source ) {
$this->add_meta_data( 'payment_source', $payment_source, true );
}
}

View file

@ -0,0 +1,51 @@
<?php
/**
* WooCommerce Payment token for Venmo.
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WC_Payment_Token;
/**
* Class PaymentTokenVenmo
*/
class PaymentTokenVenmo extends WC_Payment_Token {
/**
* Token Type String.
*
* @var string
*/
protected $type = 'Venmo';
/**
* Extra data.
*
* @var string[]
*/
protected $extra_data = array(
'email' => '',
);
/**
* Get PayPal account email.
*
* @return string PayPal account email.
*/
public function get_email() {
return $this->get_meta( 'email' );
}
/**
* Set PayPal account email.
*
* @param string $email PayPal account email.
*/
public function set_email( $email ) {
$this->add_meta_data( 'email', $email, true );
}
}

View file

@ -107,7 +107,7 @@ class PaymentTokensMigration {
}
} elseif ( $token->source()->paypal ) {
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $id, PayPalGateway::ID );
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token->id() ) ) {
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token->id(), PaymentTokenPayPal::class ) ) {
$this->logger->info( 'Token already exist for user ' . (string) $id );
continue;
}

View file

@ -81,6 +81,12 @@ class VaultingModule implements ModuleInterface {
if ( $type === 'WC_Payment_Token_PayPal' ) {
return PaymentTokenPayPal::class;
}
if ( $type === 'WC_Payment_Token_Venmo' ) {
return PaymentTokenVenmo::class;
}
if ( $type === 'WC_Payment_Token_ApplePay' ) {
return PaymentTokenApplePay::class;
}
return $type;
}
@ -102,10 +108,7 @@ class VaultingModule implements ModuleInterface {
// Exclude ApplePay tokens from payment pages.
if ( is_checkout() || is_cart() || is_product() ) {
foreach ( $tokens as $index => $token ) {
if (
$token instanceof PaymentTokenPayPal
&& $token->get_payment_source() === 'apple_pay'
) {
if ( $token instanceof PaymentTokenApplePay ) {
unset( $tokens[ $index ] );
}
}
@ -129,24 +132,18 @@ class VaultingModule implements ModuleInterface {
return $item;
}
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
assert( $payment_token instanceof PaymentTokenPayPal );
if ( $payment_token instanceof PaymentTokenPayPal ) {
$item['method']['brand'] = 'PayPal / ' . $payment_token->get_email();
return $item;
}
$email = $payment_token->get_email();
$payment_source = $payment_token->get_payment_source();
$brand_parts = array();
if ( $payment_token instanceof PaymentTokenVenmo ) {
$item['method']['brand'] = 'Venmo / ' . $payment_token->get_email();
return $item;
}
if ( $payment_source !== 'paypal' ) {
$brand_parts[] = ucwords( $payment_source );
}
if ( $email ) {
$brand_parts[] = $email;
} else {
$brand_parts[] = '#' . ( (string) $payment_token->get_id() );
}
$item['method']['brand'] = implode( ' / ', array_filter( $brand_parts ) );
if ( $payment_token instanceof PaymentTokenApplePay ) {
$item['method']['brand'] = 'ApplePay #' . ( (string) $payment_token->get_id() );
return $item;
}