ARTICLE AD BOX
I have a WordPress (WooCommerce) website with Stripe integrated and connected to a Stripe account.
I want to use two Stripe accounts based on the customer’s billing country. For example, if a customer selects the United States in the billing address, the payment should be processed through the default Stripe account. If the billing country is anything other than the United States, the payment should be processed through a second Stripe account.
I am currently trying to implement custom functionality to achieve this behavior.
// 1. Create function to find Product ID function bbloomer_product_id_in_cart( $id ) { if ( ! WC()->cart ) return false; $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_upe_params', 'bbloomer_conditional_publishable_key_upe', 9999 ); function bbloomer_conditional_publishable_key_upe( $params ) { // PRODUCT ID HERE if ( WC()->customer && WC()->customer->get_billing_country() == 'US' ) return $params; // STRIPE Live Publishable Key HERE $params[ 'key' ] = 'pk_live_................'; return $params; } add_filter( 'wc_stripe_params', 'bbloomer_conditional_publishable_key', 9999 ); function bbloomer_conditional_publishable_key( $params ) { // PRODUCT ID HERE if ( WC()->customer && WC()->customer->get_billing_country() == 'US' ) return $params; // STRIPE Live Publishable Key HERE $params[ 'key' ] = 'pk_live_................'; return $params; } add_filter( 'wc_stripe_payment_request_params', 'bbloomer_conditional_publishable_key_request', 9999 ); function bbloomer_conditional_publishable_key_request( $params ) { // PRODUCT ID HERE if ( WC()->customer && WC()->customer->get_billing_country() == 'US' ) return $params; // STRIPE Live Publishable Key HERE $params[ 'stripe' ][ 'key' ] = 'pk_live_................'; return $params; } add_filter( 'woocommerce_stripe_request_headers', 'bbloomer_conditional_private_key_headers', 9999 ); function bbloomer_conditional_private_key_headers( $params ) { // PRODUCT ID HERE if ( WC()->customer && WC()->customer->get_billing_country() == 'US' ) return $params; // STRIPE Live Secret Key HERE $params[ 'Authorization' ] = 'Basic ' . base64_encode( 'sk_live_..........' . ':' ); return $params; }The issue occurs when the billing country is changed during checkout.
If the billing country is already set to US, the payment goes to the default Stripe account. If another country is already selected when checkout loads, it correctly uses the second Stripe account.
However, when the user changes the country from US to another country on the checkout page, the Stripe keys switch successfully (as i checked in console), but the payment fails with the error:
There was an error processing the payment: Unable to process this payment, please try again or use an alternative method.
