web4me
web4me

Reputation: 15

Fatal error with Stripe Checkout: Class 'Stripe' not found

I'm trying to implement Stripe Checkout into my website. I am using Composer, everything seems to go right, but when I add my private key into init.php line 17:

 Stripe::setApiKey($stripe['private']);

PHP shows me the following error:

Fatal error: Class 'Stripe' not found in/Applications/MAMP/htdocs/stripe_payment/app/init.php on line 17

Here is the full file:

<?php

session_start();


//composer auto load
require_once 'vendor/autoload.php';

$_SESSION['user_id'] = 3;

$stripe = [
'publishable' => '..... my test key.....',
 'private' => '..... my test key.....'
   ];

//when added brakes the code
Stripe::setApiKey($stripe['private']);

$db = new PDO('mysql:host=localhost;dbname=stripe_custom;','root','root');

$userQuery = $db->prepare("
    SELECT id, username, email, premium
FROM users
WHERE id = :user_id

    ");
$userQuery->execute(['user_id' => $_SESSION['user_id']]);

$user = $userQuery->fetchObject();

?>

I presume that this is something small, but I'm a beginner and I can't figure it out. What am I doing wrong?

Upvotes: 1

Views: 2377

Answers (1)

koopajah
koopajah

Reputation: 25652

The latest releases of Stripe's PHP bindings (2.*) are now using Namespaces. This means that most of the API calls have now changed and for example:

Stripe::setApiKey("sk_test_XXX");
Stripe_Customer::create();

would become:

\Stripe\Stripe::setApiKey("sk_test_XXX");
\Stripe\Customer::create();

Otherwise, if you don't want to have to update your code you need to make sure that you download the legacy version (1.18.0).

Upvotes: 1

Related Questions