My photo
Mumbai, Maharashtra, India
Learning new technology to create products for the future.

WooCommerce: Multiple PayPal Accounts (Solved)

The WooCommerce plugin comes with its own free version of PayPal Standard. PayPal can be enabled via the WooCommerce settings and once your PayPal email is entered your WooCommerce shop is ready to take PayPal payments.

Now, there is extensive documentation online which explains, with a little bit of code, how to switch PayPal account programmatically and conditionally i.e. for a given product ID or product category slug. For example, you may want to use a PayPal account for consulting services, another for online courses and another for physical products.

By adding this simple code and hooking into woocommerce_paypal_args is indeed possible to use different / multiple PayPal Standard accounts in a single WooCommerce installation.

However, there is an outstanding problem with “IPN Validation“: once you tell WooCommerce to use a different PayPal email account, the WooCommerce order is correctly placed, but its status goes “on hold” because IPN validation on the PayPal end fails (and that’s because you’re using a different PayPal account).

So, here’s the fully working version, included the IPN validation fix. Please read the disclaimer below and – only then – enjoy!

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

PHP Snippet: Use Different PayPal Standard 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 PayPal refund handling (better if you remove PayPal API keys from WooCommerce as refunds will now need to be done on your PayPal account) 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 PayPal email 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 PayPal Account @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, BusinessBloomer.com
 * @testedwith    WooCommerce 4.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// -------------------
// 1. Switch PayPal email for Product ID

add_filter( 'woocommerce_paypal_args' , 'bbloomer_switch_paypal_email_based_product', 9999, 2 );

function bbloomer_switch_paypal_email_based_product( $paypal_args, $order ) {

    foreach ( $order->get_items() as $item_id => $item ) {

        // ENTER PRODUCT ID HERE
        if ( 123456 == $item->get_product_id() ) {

            // ENTER OTHER PAYPAL EMAIL HERE
            $paypal_args['business'] = 'another-paypal@example.com';
            break;

        }

    }

    return $paypal_args;

}

// -------------------
// 2. Avoid IPN Failure after switching PayPal email for Product ID

require_once WC()->plugin_path() . '/includes/gateways/paypal/includes/class-wc-gateway-paypal-ipn-handler.php';

class WC_Gateway_Paypal_IPN_Handler_Switch extends WC_Gateway_Paypal_IPN_Handler { 
        
    protected function validate_receiver_email( $order, $receiver_email ) {

        if ( strcasecmp( trim( $receiver_email ), trim( $this->receiver_email ) ) !== 0 ) {

            // ENTER HERE SAME PAYPAL EMAIL USED ABOVE
            if ( $receiver_email != 'another-paypal@example.com' ) {

                WC_Gateway_Paypal::log( "IPN Response is for another account: {$receiver_email}. Your email is {$this->receiver_email}" );
                $order->update_status( 'on-hold', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'woocommerce' ), $receiver_email ) );
                exit;

            }

        }

    }
        
}

new WC_Gateway_Paypal_IPN_Handler_Switch();

The post WooCommerce: Multiple PayPal Accounts (Solved) appeared first on Business Bloomer.

Comments

POPULAR POSTS

5 Ways to Keep Your Business Data Secure in 2020

9 Best Social Proof WordPress Plugins (Easy to Use)