Use WC_Payment_Token_CC instead of custom one for cards

This commit is contained in:
Emili Castells Guasch 2023-05-31 11:46:29 +02:00
parent 43594105a2
commit 76ac995c82
4 changed files with 5 additions and 89 deletions

View file

@ -1,72 +0,0 @@
<?php
/**
* WooCommerce Payment token for PayPal ACDC (Advanced Credit and Debit Card).
*
* @package WooCommerce\PayPalCommerce\Vaulting
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Vaulting;
use WC_Payment_Token;
/**
* Class PaymentTokenACDC
*/
class PaymentTokenACDC extends WC_Payment_Token {
/**
* Token Type String.
*
* @var string
*/
protected $type = 'ACDC';
/**
* Stores Credit Card payment token data.
*
* @var array
*/
protected $extra_data = array(
'last4' => '',
'card_type' => '',
);
/**
* Returns the last four digits.
*
* @param string $context The context.
* @return mixed|null
*/
public function get_last4( $context = 'view' ) {
return $this->get_prop( 'last4', $context );
}
/**
* Set the last four digits.
*
* @param string $last4 Last four digits.
*/
public function set_last4( $last4 ) {
$this->set_prop( 'last4', $last4 );
}
/**
* Returns the card type (mastercard, visa, ...).
*
* @param string $context The context.
* @return string Card type
*/
public function get_card_type( $context = 'view' ) {
return $this->get_prop( 'card_type', $context );
}
/**
* Set the card type (mastercard, visa, ...).
*
* @param string $type Credit card type (mastercard, visa, ...).
*/
public function set_card_type( $type ) {
$this->set_prop( 'card_type', $type );
}
}

View file

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

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Vaulting;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Payment_Token_CC;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
@ -76,14 +77,14 @@ class PaymentTokensMigration {
continue;
}
$payment_token_acdc = $this->payment_token_factory->create( 'acdc' );
assert( $payment_token_acdc instanceof PaymentTokenACDC );
$payment_token_acdc = new WC_Payment_Token_CC();
$payment_token_acdc->set_token( $token->id() );
$payment_token_acdc->set_user_id( $id );
$payment_token_acdc->set_gateway_id( CreditCardGateway::ID );
$payment_token_acdc->set_last4( $token->source()->card->last_digits );
$payment_token_acdc->set_card_type( $token->source()->card->brand );
$payment_token_acdc->set_expiry_year( '0000' );
$payment_token_acdc->set_expiry_month( '00' );
try {
$payment_token_acdc->save();

View file

@ -88,10 +88,6 @@ class VaultingModule implements ModuleInterface {
* @psalm-suppress MissingClosureParamType
*/
function ( $type ) {
if ( $type === 'WC_Payment_Token_ACDC' ) {
return PaymentTokenACDC::class;
}
if ( $type === 'WC_Payment_Token_PayPal' ) {
return PaymentTokenPayPal::class;
}
@ -112,13 +108,6 @@ class VaultingModule implements ModuleInterface {
return $item;
}
if ( strtolower( $payment_token->get_type() ) === 'acdc' ) {
assert( $payment_token instanceof PaymentTokenACDC );
$item['method']['brand'] = $payment_token->get_card_type() . ' ...' . $payment_token->get_last4();
return $item;
}
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
assert( $payment_token instanceof PaymentTokenPayPal );
$item['method']['brand'] = $payment_token->get_email();