This is the code I'm using.
<?php
require_once('init.php');
if ($_POST) {
\Stripe\Stripe::setApiKey("xxxxxxxxxxxxx");
$error = '';
$success = '';
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
$charge = \Stripe\Stripe_Charge::create(array("amount" => 100, //995
"currency" => "eur",
"card" => $_POST['stripeToken']));
$success = 'Your payment was successful.';
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
?>
Why am I getting the error
"Fatal error: Class 'Stripe\Stripe_Charge' not found in"
I was pulling my hair out on this one as well, but finally found the solution.
From what I found out older versions of PHP (<5.3) did not use namespaces. To get around this PSR-0* converted underscores to directory names. This created a messy interface for Composer. Now with PHP 5.3 and higher namespaces have been added. Composer now uses PSR-4 which supports namespaces but does not convert underscores to directories. Therefore, if you are using the newer preferred PSR-4, you need to use the new method. Therefore, your line:
needs to become:
You can double check to see what version of PSR your Stripe module is using by opening the
/vendor/stripe/stripe-php/composer.jsonfile. Mine shows:which clearly states that I am using PSR-4.
Here is some good reading on the subject: https://mattstauffer.co/blog/a-brief-introduction-to-php-namespacing
*PSR stands for PHP Standards Recommendation