Cannot redeclare class Stripe\Stripe

Problem statement

You are installing WP Full Stripe.
When you activate the plugin, WordPress throws one of the following fatal errors:

Error 1:
Fatal error: Cannot redeclare class Stripe\Stripe in /wp-content/plugins//classes/stripe/lib/Stripe.php on line 6

Error 2:
Fatal error: Another plugin has loaded an incompatible Stripe API client. Deactivate all other Stripe plugins, and try to activate Full Stripe again.

This is what’s happening:
WP Full Stripe loads its own copy of the Stripe PHP library, and the other plugin tries to load its own Stripe PHP library as well, and PHP throws an error.

The error is the result of a fundamental flaw in WordPress’ design: there is no central management for PHP libraries used by several plugins.

The solution

There are cases when this issue can be resolved, in other cases there is no solution.
It depends on the Stripe PHP library versions required by the affected plugins, and the loading order of the plugins.

As a first step, you need to know which Stripe PHP library versions are required by the plugins.

WP Full Stripe defines the version number in the wp-full-stripe.php file in the root of the plugin directory, you just have to find the WP_FULL_STRIPE_API_VERSION constant at the beginnig of the file (see last line of the code snippet):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/*
Plugin Name: WP Full Stripe
Plugin URI: https://paymentsplugin.com
Description: Complete Stripe payments integration for WordPress
Author: Mammothology
Version: 3.12.0
Author URI: https://paymentsplugin.com
Text Domain: wp-full-stripe
Domain Path: /languages
*/
 
define( 'WP_FULL_STRIPE_MIN_PHP_VERSION', '5.3.0' );
define( 'WP_FULL_STRIPE_MIN_WP_VERSION', '4.0.0' );
define( 'WP_FULL_STRIPE_STRIPE_API_VERSION', '5.1.1' );

For the other plugins, the safest bet is to find the VERSION file in the Stripe PHP library directory.

The loading order is determined by the alphabetical order of the plugin slugs.

Armed with the version numbers and loading order, consider the following cases:

  1. The other plugin is loaded first, and it requires an older Stripe version than WP Full Stripe
    In this case there is no solution. The oher plugin has to load the Stripe library, but it will be too old for WP Full Stripe.
  2. The other plugin is loaded first, and it requires a newer Stripe version than WP Full Stripe
    This might work, you have to comment out loading the Stripe library in the /wp-full-stripe.php file in the root folder of WP Full Stripe.
  3. WP Full Stripe is loaded first, and it requires an older Stripe version than the other plugin
    In this case there is no solution. WP Full Stripe has to load the Stripe library, but it will be too old for the other plugin.
  4. WP Full Stripe is loaded first, and it requires a newer Stripe version than the other plugin
    This might work, you have to comment out loading the Stripe library in the other plugin.

How to not load the Stripe PHP library in Full Stripe

You have to comment out the code which verifies the version number of the Stripe PHP library, if it’s already loaded.

It’s in the wp-full-stripe.php file in the plugin root directory:

1
2
3
4
5
6
7
8
9
//Stripe PHP library
if ( ! class_exists( '\Stripe\Stripe' ) ) {
  require_once( dirname( __FILE__ ) . '/vendor/stripe/stripe-php/init.php' );
} else {
  if ( substr( \Stripe\Stripe::VERSION, 0, strpos( \Stripe\Stripe::VERSION, '.' ) ) != substr( WP_FULL_STRIPE_STRIPE_API_VERSION, 0, strpos( WP_FULL_STRIPE_STRIPE_API_VERSION, '.' ) ) ) {
     $reflector = new ReflectionClass( '\Stripe\Stripe' );
     wp_die( plugin_basename( __FILE__ ) . ': ' . __( 'Another plugin has loaded an incompatible Stripe API client. Deactivate all other Stripe plugins, and try to activate Full Stripe again.', 'wp-full-stripe' ) . ' ' . \Stripe\Stripe::VERSION . ' != ' . STRIPE_API_VERSION . ', ' . $reflector->getFileName() );
  }
}

Comment out the contents of the “else” block:

1
2
3
4
5
6
7
8
9
10
11
//Stripe PHP library
if ( ! class_exists( '\Stripe\Stripe' ) ) {
  require_once( dirname( __FILE__ ) . '/vendor/stripe/stripe-php/init.php' );
} else {
/*
  if ( substr( \Stripe\Stripe::VERSION, 0, strpos( \Stripe\Stripe::VERSION, '.' ) ) != substr( WP_FULL_STRIPE_STRIPE_API_VERSION, 0, strpos( WP_FULL_STRIPE_STRIPE_API_VERSION, '.' ) ) ) {
     $reflector = new ReflectionClass( '\Stripe\Stripe' );
     wp_die( plugin_basename( __FILE__ ) . ': ' . __( 'Another plugin has loaded an incompatible Stripe API client. Deactivate all other Stripe plugins, and try to activate Full Stripe again.', 'wp-full-stripe' ) . ' ' . \Stripe\Stripe::VERSION . ' != ' . STRIPE_API_VERSION . ', ' . $reflector->getFileName() );
  }
*/
}

IMPORTANT: WP Full Stripe installations using Stripe PHP library versions other than the exact version required by the plugin are not supported by Mammothology.

in Troubleshooting