mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 06:52:50 +08:00
Refactor Venmo and ApplePay payment tokens.
This commit is contained in:
parent
b3b6693aa2
commit
0d4fbc4c0b
10 changed files with 234 additions and 70 deletions
31
modules/ppcp-vaulting/src/PaymentTokenApplePay.php
Normal file
31
modules/ppcp-vaulting/src/PaymentTokenApplePay.php
Normal 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();
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
|
51
modules/ppcp-vaulting/src/PaymentTokenVenmo.php
Normal file
51
modules/ppcp-vaulting/src/PaymentTokenVenmo.php
Normal 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 );
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue