Bic Mitchun
Bic Mitchun

Reputation: 518

Fatal Error PHP Class not found but it's included in the project

I'm trying to test Stripe Checkout on my virtual server but when i try return this error:

Fatal error: Class 'Stripe\Customer' not found in /home/user/public_html/charge.php on line 8

As suggested here I verified if the file ./config.php was existing with:

var_dump(file_exists("./config.php"));

It returns bool(true) being a PhP beginner im not sure what i'm doing wrong.

charge.php

<?php
  require_once('./config.php');
  
  var_dump(file_exists("./config.php"));
  
  $token  = $_POST['stripeToken'];

  $customer = \Stripe\Customer::create(array(
      'email' => '[email protected]',
      'source'  => $token
  ));

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => 2000,
      'currency' => 'usd'
  ));

  echo '<h1>Successfully charged $20.00!</h1>';


?>

config.php

<?php
require_once('vendor/stripe/init.php');
$stripe = array(
  secret_key      => getenv('sk_test_PidozakrX1NIEf8YM9TBDMl8'),
  publishable_key => getenv('pk_test_pCzekEHR4Io5YFJhrzFE7Koe')
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

Directory:

>public_html
   >vendor
       >stripe
          >lib
              >Charge.php
              >Customer.php
              >Stripe.php

UPDATE Okay so with changes suggested by Matthew getting new error. But i'm not sure where should I set the API key Stripe::setApiKey(<API-KEY>)? It's aleady set in config.php...

Fatal error: Uncaught exception 'Stripe\Error\Authentication' with message 'No API key provided. (HINT: set your API key using "Stripe::setApiKey()".in /home/user/public_html/vendor/stripe/lib/ApiRequestor.php:127 Stack trace: #0 /home/user/public_html/vendor/stripe/lib/ApiRequestor.php(59): Stripe\ApiRequestor->_requestRaw('post', '/v1/customers', Array, Array) #1 /home/user/public_html/vendor/stripe/lib/ApiResource.php(115): Stripe\ApiRequestor->request('post', '/v1/customers', Array, Array) #2 /home/user/public_html/vendor/stripe/lib/ApiResource.php(155): Stripe\ApiResource::_staticRequest('post', '/v1/customers', Array, NULL) #3 /home/user/public_html/vendor/stripe/lib/Customer.php(37): Stripe\ApiResource::_create(Array, NULL) #4 /home/user/public_html/charge.php(9): Stripe\Customer::create(Array)

5 {main} thrown in /home/user/public_html/vendor/stripe/lib/ApiRequestor.php on line 127

Upvotes: 0

Views: 1741

Answers (2)

Bic Mitchun
Bic Mitchun

Reputation: 518

Yesterday I emailed stripe support. I could of never figured the answer out...! So turns out the issue here is the way I've setup the API keys in config.php. This code will not do what we think... It's trying to retrieve an environment variable set with the variable name as the API key which will just return an empty string...

config.php

<?php
require_once('vendor/stripe/init.php');
$stripe = array(
  secret_key      => getenv('sk_test_PidozakrX1NIEf8YM9TBDMl8'),
  publishable_key => getenv('pk_test_pCzekEHR4Io5YFJhrzFE7Koe')
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

should be

<?php
require_once('vendor/stripe/init.php');
$stripe = array(
  secret_key      => ('sk_test_PidozakrX1NIEf8YM9TBDMl8'),
  publishable_key => ('pk_test_pCzekEHR4Io5YFJhrzFE7Koe')
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

Basically, just remove getenv after each arrow. For me it works! The customer is created and the amount is charged! Nothing fancy. Simple solution.

Upvotes: 0

vysd
vysd

Reputation: 11

I'm not sure but it looks like due to the composer,

If you haven't check path yet , please first look them in composer.json

{
"autoload": {
    "psr-0": {
        "Sub-folder/path": "class's parent directory "
    }
}

NOTE: If the problem occurs due to this one , You should re-install websocket
OR you can arrange path to this structure

Upvotes: 1

Related Questions