First of all, a bit of history and theory, as I believe it is important to understand everything.
Recently, many clients of the modulosdepago.es plugin in its PRO version, which accepts card tokenization and recurring payments with WooCommerce Subscriptions, are migrating to my plugin from WooCommerce.com
The problem they basically have is that the subscriptions created previously do not load, meaning they cannot be charged. Up to this point, everything is normal, as when WooCommerce saves tokenized cards, it saves them for a particular Gateway ID, and my gateway plugin should not have access to those tokens if the ID is different.
Basically, when WooCommerce saves cards through its API, it saves them in two tables
In the table woocommerce_payment_tokens

This is where it saves the tokens themselves.
and the table woocommerce_payments_tokensmeta

is where it saves all the data related to those cards.
Well, you can see that in the table woocommerce_payment_tokens there is a column that says gateway_id, In this column WooCommerce saves the Gateway ID. If the modulosdepago.es plugin had been developed using the WooCommerce API, all user tokens would be found here and nothing else would need to be done, as fortunately, both the gateway_id of modulosdepago.es and that of WooCommerce.com are the same. So it would be a completely transparent migration.
As I mentioned, the modulosdepago.es extension has not been developed following WooCommerce standards, and the Tokenization API has not been used. This has forced me to create a script so that all users who want to start enjoying all the advantages of the official WooCommerce.com plugin for Redsys can do so without problems.
What does the modulosdepago.es extension do? Well, it’s quite simple, instead of using the tokenization API, it saves the token with the user metadata.
Exactly what it does is save it in the metas _ds_merchant_identifier and redsys_card of the users in the table {prefijo_}usermeta

This way “works”, but has consequences, such as the user not being able to manage the cards from their WooCommerce account, or WooCommerce not knowing basic things like the expiration date. As you will see, it is not the most suitable way to do it, especially when there is a specific API for it.
What does my script consist of? Well, it simply goes through all users looking for these two data points and adds them correctly to WooCommerce through its Tokenization API. This way, users can manage their cards from their accounts, for example.

In this case, the script must add fake data, such as the expiration date, which it sets to December 2022, or the card type which it sets as unknown, simply because this data is not saved by the modulosdepago.es plugin, and therefore cannot be added to the WooCommerce API (the WooCommerce.com one saves all of them).
Once the process is complete, everything works normally, all users have access to their cards, and WooCommerce is able to manage them (which is very important, logically).
Before continuing, DO NOT use this script if you are going to continue with the modulosdepago.es plugin. It will be of no use to you as the plugin will continue saving the data and using it in its way. This script is only worth using if you want to start using the official WooCommerce.com plugin for Redsys that uses the WooCommerce API.
How is the script used?
It’s very simple, you just need to upload a file with the name you want, for example update-tokens.php to the root directory of your installation. Once uploaded, you just need to visit the address https://tusitio.tld/update-tokens.php and the script will start running. If when it finishes the message “Congratulations! All user Tokens migrated” does not appear, refresh the page until it does. This may be due to having many users and timeouts occurring.
Please make a backup before proceeding with the script. Due to the way it works, nothing should happen, but when dealing with the database, it’s always good to make a backup before starting.
The code that the file to be uploaded should have is:
Remember that if you are carrying out this migration, and have a problem, you can open a ticket for help at WooCommerce.com
Also remember that my plugin is constantly audited by Automattic (developers of WooCommerce) and strictly follows all WordPress, WooCommerce standards and always uses the WooCommerce API. You will never have compatibility issues and all the code follows the Coding Standards of WordPress VIP, the most demanding in terms of code quality in the WordPress world.







