* PaymentMethodEventTracker class file.
declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\FraudProtection;
defined( 'ABSPATH' ) || exit;
* Tracks payment method events for fraud protection analysis.
* This class provides methods to track events for adding payment methods in My Account page
* for fraud protection. Event-specific data is passed to the SessionDataCollector which
* handles session data storage internally.
* @internal This class is part of the internal API and is subject to change without notice.
class PaymentMethodEventTracker {
* Session data collector instance.
* @var SessionDataCollector
private SessionDataCollector $session_data_collector;
* Initialize with dependencies.
* @param SessionDataCollector $session_data_collector The session data collector instance.
final public function init( SessionDataCollector $session_data_collector ): void {
$this->session_data_collector = $session_data_collector;
* Track add payment method page loaded event.
* Collects session data when the add payment method page is initially loaded.
* This captures the initial session state before any user interactions.
public function track_add_payment_method_page_loaded(): void {
$this->session_data_collector->collect( 'add_payment_method_page_loaded', array() );
* Track payment method added event.
* Collects session data when a payment method is added.
* @param int $token_id The newly created token ID.
* @param \WC_Payment_Token $token The payment token object.
public function track_payment_method_added( $token_id, $token ): void {
$event_data = $this->build_payment_method_event_data( 'added', $token );
$this->session_data_collector->collect( 'payment_method_added', $event_data );
* Build payment method event-specific data.
* Extracts relevant information from the payment token object including
* token type, gateway ID, user ID, and card details for card tokens.
* This data will be merged with session data during collection.
* @param string $action Action type (added, updated, set_default, deleted, add_failed).
* @param \WC_Payment_Token $token The payment token object.
* @return array Payment method event data.
private function build_payment_method_event_data( string $action, \WC_Payment_Token $token ): array {
'token_id' => $token->get_id(),
'token_type' => $token->get_type(),
'gateway_id' => $token->get_gateway_id(),
'user_id' => $token->get_user_id(),
'is_default' => $token->is_default(),
// Add card-specific details if this is a credit card token.
if ( $token instanceof \WC_Payment_Token_CC ) {
$event_data['card_type'] = $token->get_card_type();
$event_data['card_last4'] = $token->get_last4();
$event_data['expiry_month'] = $token->get_expiry_month();
$event_data['expiry_year'] = $token->get_expiry_year();