Home » WooCommerce: Multiple Stripe Accounts (Solved)

WooCommerce: Multiple Stripe Accounts (Solved)

by Tutor Aspire

The WooCommerce plugin fully integrates with the WooCommerce Stripe Payment Gateway plugin, developed by Automattic itself. With this free plugin, Stripe payment gateway can be enabled via the WooCommerce settings and once your Stripe “Live Publishable Key” and “Live Secret Key” are set, your WooCommerce shop is ready to take credit card payments powered by Stripe.

Now, there is some documentation online which explains, with a little bit of code, how to switch Stripe account programmatically and conditionally i.e. for a given product ID or product category slug – same as what we’ve seen recently with PayPal Standard (here’s the tutorial for using different PayPal accounts inside a single WooCommerce installation). For example, you may want to use a Stripe account for digital sales and a different one for physical products.

Unlike PayPal Standard, however, online documentation and snippets are quite out of date and require, often, to create a custom Class which is always a difficult task in PHP programming. Thankfully, there are new WooCommerce Stripe hooks and therefore it’s possible to use different / multiple Stripe accounts in a single WooCommerce installation.

Please read the disclaimer below and – only then – enjoy!

My goal is to use my Stripe keys for all payments apart from a given product ID which will need to deposit funds into a different Stripe account.

PHP Snippet: Use Different Stripe Account For a Product ID @ WooCommerce Checkout

Disclaimer: the snippet below MAY CAUSE side effects – use at your own risk. For example, it can mess with Stripe refund handling (better if you manually create refunds from Stripe as opposed to using WooCommerce order admin) or generate other unknown errors. Customizing payment gateways is very dangerous and should be done only if you are aware of the potential consequences. Please test this thoroughly.
Please note: the snippet below looks for a Product ID inside the order and switches Stripe keys if successful. In case the order contains several products you’d need to find a workaround, for example by allowing only 1 product in the Cart.
/**
 * @snippet       Switch Stripe Account @ WooCommerce Checkout
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire, BusinessBloomer.com
 * @testedwith    WooCommerce 4.6
 * @donate $9     https://www.tutoraspire.com
 */

// -------------------
// 1. Create function to find Product ID

function tutoraspire_product_id_in_cart( $id ) {
$product_cart_id = WC()->cart->generate_cart_id( $id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) return true;
return false;
}

// -------------------
// 2. Change Stripe keys on the go

add_filter( 'wc_stripe_params', 'tutoraspire_conditional_publishable_key', 9999 );

function tutoraspire_conditional_publishable_key( $params ) {

// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $params;

// STRIPE Live Publishable Key HERE
$params[ 'key' ] = 'pk_live_................';

return $params;
}

add_filter( 'wc_stripe_payment_request_params', 'tutoraspire_conditional_publishable_key_request', 9999 );

function tutoraspire_conditional_publishable_key_request( $params ) {

// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $params;

// STRIPE Live Publishable Key HERE
$params[ 'stripe' ][ 'key' ] = 'pk_live_................';

return $params;
}

add_filter( 'woocommerce_stripe_request_headers', 'tutoraspire_conditional_private_key_headers', 9999 );

function tutoraspire_conditional_private_key_headers( $headers_args ) {

// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $headers_args;

// STRIPE Live Secret Key HERE
$headers_args[ 'Authorization' ] = 'Basic ' . base64_encode( 'sk_live_..........' . ':' );

return $headers_args;
}

You may also like