Browse: Home / Snippets /

Fix multiple sites sharing one Stripe account

Contents


Snippet #

/* Requires LD Stripe addon v1.3.0.17 */

add_filter( 'learndash_stripe_process_webhook', function( $process, $payload ) {

    global $wpdb;

    $stored_plan_ids = get_transient( 'learndash_stripe_plan_ids' );

    if ( false === $stored_plan_ids ) {

        $stored_plan_ids = $wpdb->get_col( $wpdb->prepare(

            "SELECT meta_value FROM `{$wpdb->prefix}postmeta` WHERE meta_key = %s",

            'stripe_plan_id'

        ) );



        set_transient( 'learndash_stripe_plan_ids', $stored_plan_ids, HOUR_IN_SECONDS );

    }



    if ( $payload->data->object->object == 'checkout.session' ) {

        if ( strpos( $payload->data->object->cancel_url, home_url() ) === false && strpos( $payload->data->object->success_url, home_url() ) === false ) {

            return false;

        }

    } elseif ( $payload->data->object->object == 'invoice' ) {

        foreach ( $payload->data->object->lines->data as $data ) {

            if ( ! in_array( $data->plan->id, $stored_plan_ids ) ) {

                return false;

            }

        }

    } elseif ( $payload->data->object->object == 'subscription' ) {

        if ( in_array( $payload->data->object->plan->id, $stored_plan_ids ) ) {

            return false;

        }

    } elseif ( ! in_array( $payload->data->object->object, [ 'checkout.session', 'invoice', 'subscription' ] ) ) {

        return false;

    }

    

    return $process;

}, 10, 2 );