【问题标题】:How to properly require stripe library to avoid Error 500 on stripe Webhook如何正确要求条带库以避免条带 Webhook 上的错误 500
【发布时间】:2020-01-10 17:08:57
【问题描述】:

我正在尝试使用条纹 Webhooks,但有点挣扎。 我的网站根目录中有一个端点文件 ipn.php。 我也安装了条带源代码。

通过条带接口,我向这个端点发送了一个测试 Webhook。

这段代码运行良好:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

$input = @file_get_contents('php://input');
$event = json_decode($input);

http_response_code(200);

if($event->type == "charge.succeeded") {
   echo $event->data->object->id;
}

?>

当我测试时 但只要我添加这两行:

require_once('stripe/lib/Stripe.php');
Stripe::setApiKey("whsec_myAPIKeyHere");

我收到了这样的回复:
致命错误:未捕获的错误:在 /home/ftpHostName/www/myWebsiteWithout.com/ipn.php:11 中找不到“Stripe”类 堆栈跟踪:

0 {主}

11行的/home/ftpHostName/www/myWebsiteWithout.com/ipn.php:11中抛出

我之前没有做过任何php,我认为问题来自于require_once('stripe/lib/Stripe.php');

Stripe 文档说要使用 Composer(我不知道如何使用)或下载源代码(我就是这样做的)。 https://stripe.com/docs/libraries#php

另外,在错误中,我不明白我得到的 url:为什么 FTP 主机名出现在这里?

【问题讨论】:

  • stripe 文件夹的路径是什么?
  • 您好,stripe 文件夹位于我网站的根目录,就像我的 ipn.php 文件一样。我也试过这条路:require_once('./stripe/lib/Stripe.php');但有同样的问题,
  • 在下面查看我的答案
  • 包括init.php 而不是Stripe.php 就像你在上面做的那样,这在他们的文档中描述,github.com/stripe/stripe-php#manual-installation

标签: php stripe-payments webhooks


【解决方案1】:

发生这种情况的原因是它没有找到 Stripe 类。

这很可能是因为包含该类的文件的路径 - stripe/lib/Stripe.php 不正确。

在不知道条带目录在哪里的情况下,很难确切说明您需要放置什么路径,但如果您的条带目录与ipn.php 位于同一路径中,例如:

ipn.php
stripe/
stripe/lib/
stripe/lib/Stripe.php

那么你可以这样做:

require_once (__DIR__ . '/stripe/lib/Stripe.php');

所以你的文件看起来像这样:

<?php
require_once (__DIR__ . '/stripe/lib/Stripe.php');
ini_set('display_errors',1);
error_reporting(E_ALL);

Stripe::setApiKey("whsec_myAPIKeyHere");

$input = @file_get_contents('php://input');
$event = json_decode($input);

http_response_code(200);

if($event->type == "charge.succeeded") {
   echo $event->data->object->id;
}

?>

其中__DIR__ 返回工作文件的当前目录。

【讨论】:

  • 感谢您的回答,但不幸的是我遇到了同样的错误。
  • 您能否进入您的Stripe.php 文件并在页面顶部(在echo __DIR__; die();,然后在浏览器中访问Stripe.php 文件。跨度>
  • 实际上,您能否确认您在页面顶部(在 require_once (__DIR__ . '/stripe/lib/Stripe.php');。没有半途而废
  • /home/ftpHostName/www/myWebsiteWithout.com/stripe/lib
  • 好的,我终于找到了解决方案:Stripe 类位于命名空间 Stripe 中。所以我只是这样做了 \Stripe\Stripe::setApiKey("whsec_myAPIKeyHere");我不明白为什么 Stripe 文档不正确...谢谢您的帮助我打开了 Stripe.php 感谢您
【解决方案2】:

您应该包含 Stripe 的 PHP SDK 附带的 init.php,而不是尝试直接加载 Stripe.php,例如require_once (__DIR__ . '/stripe-php/init.php')

Stripe 描述了这个here

【讨论】:

    猜你喜欢
    • 2022-10-18
    • 2021-11-29
    • 2013-12-02
    • 2017-02-09
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2018-11-25
    • 2018-11-29
    相关资源
    最近更新 更多