0

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

10
  • does it work if you just do \Stripe\Customer::create(... ? Commented Jul 4, 2016 at 1:04
  • I hope those aren't your live Stripe credentials... Commented Jul 4, 2016 at 1:20
  • @Machavity: of couse not loll! You hungry huh? Commented Jul 4, 2016 at 1:24
  • @MatthewArkin: thank you for trying! Still return same error... Fatal error: Class 'Stripe\Customer' not found Commented Jul 4, 2016 at 1:27
  • \vendor\stripe\lib\Customer::create is not how you access namespaced classes - it should be what Matthew posted above. Commented Jul 4, 2016 at 1:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

hey I give you props for trying but I have emailed stripe and turns out the answer was pretty simple. I'm posting the answer.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.