To retrieve and store token details, you need to implement a response handler. In the response handler a Payment Token entity is created and stored in the payment extension attributes.
Following sample is an example of the response handler implementation:
There are two types of paymentTokenFactory:
CreditCardTokenFactory: used for credit cards
AccountPaymentTokenFactory: used for payment accounts, like PayPal
Depending on your payment integration, you need to specify one of them to create a payment token.
Also, you can use \Magento\Vault\Api\Data\PaymentTokenInterfaceFactory in your code as common dependency. But in that case you must specify preference for this interface in di.xml, because PaymentTokenInterfaceFactory does not have default preference.
The important thing is the setGatewayToken() method. This method gets the gateway token: a hashed value based on some credit card details. Different
payment providers use different algorithms to create this hash. In most cases, exactly this token is used to perform place order actions.
The created response handler must be added to the handler chain in the DI configuration file di.xml.
Example of the Braintriee di.xml:
The persistence layer for Payment Token is implemented in the Vault Module.