Disclaimer: Update October 2020 – Due to the increasing amount of requests I get, please keep in mind that this is an old post and WooCommerce has evolved a lot since then. Many hooks and methods used here may not work anymore. I will update the content once I have more free time to play around with the new hooks. Thanks for your understanding.
Last month I finished a long WordPress project which involved a lot of custom work. Among the scope features required by the client, there is one that really got on my nerves until I come up with the solution.
The task was to collect payments into several different Stripe accounts based on some conditions ( product ID, product category or whatever ).
As you know, WooCommerce supports only one Stripe account via the Stripe plugin so the only possible way to do this is with hooks.
After searching and testing numerous hooks, I found out that you need to use 3 filter hooks in order to change the Stripe keys on the fly:
- wc_stripe_payment_request_params – which has an array $params where $params[‘stripe’][‘key’] contains the publishable key
- wc_stripe_params – which also has an array $params where $params[‘key’] contains the publishable key.
- woocommerce_stripe_request_headers – which has an array $headers_args, where $headers_args[ ‘Authorization’ ] field here contains the base-64 encoded secret key.
Now based on your specific scenario, you need to use all 3 hooks in order to modify the stripe keys. To ease the process I’ve created a small helper class that you can use in your project.
Just include the file in your project and call
new Woo_Conditional_Stripe_Keys( $publishable, $secret )
within your logic to change the keys. For example, it can be used within woocommerce_available_payment_gateways
filter to switch payments on the fly.