【问题标题】:Fatal Error PHP Class not found but it's included in the project找不到致命错误 PHP 类,但它包含在项目中
【发布时间】:2016-11-05 15:37:23
【问题描述】:

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

致命错误:在 'Stripe\Customer' 类中找不到 /home/user/public_html/charge.php 在第 8 行

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

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

它返回 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 $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']);
?>

目录:

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

更新 好的,马修建议的更改出现了新错误。但我不确定我应该在哪里设置 API 密钥Stripe::setApiKey(&lt;API-KEY&gt;)?已经设置在config.php...

致命错误:未捕获的异常“Stripe\Error\Authentication”与 message '未提供 API 密钥。 (提示:使用设置您的 API 密钥 "Stripe::setApiKey()".in /home/user/public_html/vendor/stripe/lib/ApiRequestor.php:127 堆栈 跟踪:#0 /home/user/public_html/vendor/stripe/lib/ApiRequestor.php(59): Stripe\ApiRequestor->_requestRaw('post', '/v1/customers', 数组, 阵列)#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', 数组, 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} 在第 127 行的 /home/user/public_html/vendor/stripe/lib/ApiRequestor.php 中抛出

【问题讨论】:

  • 如果你只做 \Stripe\Customer::create(... 吗?
  • 我希望这些不是您的实时 Stripe 凭据...
  • @Machavity:当然不是懒惰!你饿了吧?
  • @MatthewArkin:感谢您的尝试!仍然返回相同的错误...Fatal error: Class 'Stripe\Customer' not found
  • \vendor\stripe\lib\Customer::create 不是您访问命名空间类的方式 - 它应该是 Matthew 在上面发布的内容。

标签: php stripe-payments


【解决方案1】:

昨天我通过电子邮件发送了条带支持。我永远也想不出答案……!所以这里的问题是我在config.php 中设置API 密钥的方式。这段代码不会像我们想的那样......它试图检索一个环境变量集,变量名作为 API 密钥,它只会返回一个空字符串......

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。对我来说它有效!创建客户并收取金额!没有什么花哨。简单的解决方案。

【讨论】:

    【解决方案2】:

    我不确定,但似乎是由于作曲家,

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

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

    注意:如果由于这个问题而出现问题,你应该重新安装 websocket
    或者你可以安排这个结构的路径

    【讨论】:

    • 嘿,我给了你尝试的道具,但我已经通过电子邮件发送了 stripe,结果发现答案很简单。我正在发布答案。
    猜你喜欢
    • 2015-03-20
    • 2016-05-11
    • 2013-11-03
    • 2016-09-17
    • 2016-01-29
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    相关资源
    最近更新 更多