ApplePay Vaulting Integration

This commit is contained in:
Pedro Silva 2024-01-24 08:47:48 +00:00
parent 827bd2568d
commit 96b83c9d0d
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
7 changed files with 86 additions and 12 deletions

View file

@ -28,7 +28,8 @@ class PaymentTokenPayPal extends WC_Payment_Token {
* @var string[]
*/
protected $extra_data = array(
'email' => '',
'email' => '',
'payment_source' => '',
);
/**
@ -48,4 +49,22 @@ 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

@ -86,6 +86,37 @@ class VaultingModule implements ModuleInterface {
}
);
add_filter(
'woocommerce_get_customer_payment_tokens',
/**
* Filter available payment tokens depending on context.
*
* @psalm-suppress MissingClosureParamType
* @psalm-suppress MissingClosureReturnType
*/
function( $tokens, $customer_id, $gateway_id ) {
if ( ! is_array( $tokens ) ) {
return $tokens;
}
// 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'
) {
unset( $tokens[ $index ] );
}
}
}
return $tokens;
},
10,
3
);
add_filter(
'woocommerce_payment_methods_list_item',
/**
@ -100,8 +131,22 @@ class VaultingModule implements ModuleInterface {
if ( strtolower( $payment_token->get_type() ) === 'paypal' ) {
assert( $payment_token instanceof PaymentTokenPayPal );
$item['method']['brand'] = $payment_token->get_email();
$email = $payment_token->get_email();
$payment_source = $payment_token->get_payment_source();
$brand_parts = array();
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 ) );
return $item;
}