Skip to main content
The Payment Token API allows payment gateways to securely store payment method tokens and let customers manage them from their account. Introduced in WooCommerce 2.6, this API provides a standardized way to handle tokenization.

Overview

The Payment Token API consists of three main components:
  • WC_Payment_Tokens: Static class for managing tokens (get, delete, set defaults)
  • WC_Payment_Token: Abstract base class for creating token types
  • WC_Payment_Token_CC and WC_Payment_Token_eCheck: Built-in token types

Token Types

WooCommerce includes two token types out of the box:

Credit Card Tokens (WC_Payment_Token_CC)

Stores credit card information:
  • Card type (Visa, MasterCard, etc.)
  • Last 4 digits
  • Expiration month and year

eCheck Tokens (WC_Payment_Token_eCheck)

Stores bank account information:
  • Last 4 digits of account number
You can also create custom token types by extending WC_Payment_Token.

Adding Tokenization to Your Gateway

1

Extend the correct base class

WooCommerce provides specialized base classes for common token types:
See /plugins/woocommerce/includes/gateways/class-wc-payment-gateway-cc.php for the CC implementation.
2

Declare tokenization support

Add 'tokenization' to your gateway’s $supports array:
See /plugins/woocommerce/includes/abstracts/abstract-wc-payment-gateway.php:131 for the supports property.
3

Implement add_payment_method()

Handle adding payment methods from the My Account page:
See /plugins/woocommerce/includes/abstracts/abstract-wc-payment-gateway.php:698-703 for the base implementation.
4

Save tokens during checkout

In your process_payment() method, check if the customer wants to save the payment method:
5

Use saved tokens

Retrieve and use saved tokens when processing payments:

Working with Credit Card Tokens

The WC_Payment_Token_CC class handles credit card tokens.

Creating a Credit Card Token

See /plugins/woocommerce/includes/payment-tokens/class-wc-payment-token-cc.php for the complete implementation.

Credit Card Token Methods

string
Get the card type (visa, mastercard, etc.)
void
Set the card type
string
Get the last 4 digits
void
Set the last 4 digits
string
Get expiration month (MM format)
void
Set expiration month (MM format)
string
Get expiration year (YYYY format)
void
Set expiration year (YYYY format)

Supported Card Types

Add custom types with the woocommerce_credit_card_type_labels filter.

WC_Payment_Tokens Static Methods

The WC_Payment_Tokens class provides static methods for managing tokens.

Get Customer Tokens

See /plugins/woocommerce/includes/class-wc-payment-tokens.php:68-89 for the implementation.

Get Default Token

Get Order Tokens

Get Single Token

Delete Token

Set Default Token

See /plugins/woocommerce/includes/class-wc-payment-tokens.php:200-211 for the implementation.

Base Token Methods

All token types inherit these methods from WC_Payment_Token:

Token Properties

See /plugins/woocommerce/includes/abstracts/abstract-wc-payment-token.php:96-155 for getter methods.

Setting Token Data

Set methods do not save to the database. You must call save(), create(), or update() to persist changes.

Saving Tokens

Validation

Creating Custom Token Types

Extend WC_Payment_Token to create custom token types:
The $type property must match the class name suffix: WC_Payment_Token_{$type}

Payment Gateway Integration

When extending WC_Payment_Gateway_CC, tokenization forms are automatically displayed:
See /plugins/woocommerce/includes/gateways/class-wc-payment-gateway-cc.php:28-37 for the payment fields implementation.

Security Considerations

  • Never store complete card numbers or CVV codes
  • Always validate token ownership before use
  • Use HTTPS for all payment pages
  • Follow PCI DSS compliance guidelines
  • Store only tokens from your payment provider

Validating Token Ownership

Display Token Information

Tokens provide a formatted display name:
See /plugins/woocommerce/includes/payment-tokens/class-wc-payment-token-cc.php:50-60 for the display name implementation.

Complete Example

Here’s a complete example of tokenization support:

Next Steps

Payment Gateway API

Learn about creating payment gateways

WC_Payment_Token Class Reference

View complete API documentation