未找到致命错误 PHP Class 但已包含在项目中

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

我正在尝试在我的虚拟服务器上测试 Stripe Checkout,但是当我尝试 return 时出现此错误:

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

按照建议 here 我验证了文件 ./config.php 是否存在于:

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

它 returns bool(true) 作为一个 PhP 初学者我不确定我做错了什么。

charge.php

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

  $customer = \Stripe\Customer::create(array(
      'email' => 'customer@example.com',
      'source'  => $token
  ));

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

  echo '<h1>Successfully charged .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']);
?>

目录:

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

更新 好的,Matthew 建议的更改出现新错误。但是我不确定应该在哪里设置 API 键 Stripe::setApiKey(<API-KEY>)?它已经设置在 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

我不确定,但看起来是作曲家的原因,

如果您还没有检查路径,请先在 composer.json

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

注意:如果问题是因为这个,你应该重新安装websocket
或者你可以安排此结构的路径

昨天我给 stripe 支持发了邮件。我永远也想不出答案......!所以这里的问题是我在 config.php 中设置 API 键的方式。这段代码不会做我们想的......它试图检索一个环境变量集,变量名作为 API 键,这将只是 return 一个空字符串......

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']);
?>

应该是

<?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']);
?>

基本上,只需删除每个箭头后的 getenv。对我来说它有效!创建客户并收取金额!没有什么花哨。简单的解决方案。