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
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 See
'tokenization' to your gateway’s $supports array:/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
TheWC_Payment_Token_CC class handles credit card tokens.
Creating a Credit Card Token
/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
woocommerce_credit_card_type_labels filter.
WC_Payment_Tokens Static Methods
TheWC_Payment_Tokens class provides static methods for managing tokens.
Get Customer Tokens
/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
/plugins/woocommerce/includes/class-wc-payment-tokens.php:200-211 for the implementation.
Base Token Methods
All token types inherit these methods fromWC_Payment_Token:
Token Properties
/plugins/woocommerce/includes/abstracts/abstract-wc-payment-token.php:96-155 for getter methods.
Setting Token Data
Saving Tokens
Validation
Creating Custom Token Types
ExtendWC_Payment_Token to create custom token types:
The
$type property must match the class name suffix: WC_Payment_Token_{$type}Payment Gateway Integration
When extendingWC_Payment_Gateway_CC, tokenization forms are automatically displayed:
/plugins/woocommerce/includes/gateways/class-wc-payment-gateway-cc.php:28-37 for the payment fields implementation.
Security Considerations
Validating Token Ownership
Display Token Information
Tokens provide a formatted display name:/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