【问题标题】:Stripe Webhook Error 500条纹 Webhook 错误 500
【发布时间】:2013-12-02 00:41:58
【问题描述】:

我正在尝试设置一个 Stripe webhook,一旦发生“charge.succeeded”事件,它就会发送一封电子邮件。当我在 Stripe 上测试 webhook 时,我不断收到普遍的“错误 500”。我对 Stripe 很陌生,我真的被困在这里了。

<?php

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

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

// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

// This will send receipts on successful charges
if ($event_json->type == 'charge.succeeded') {

        // This is where we e-mail the invoice.

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';                 // Specify main and backup server
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'abc@gmail.com';                            // SMTP username
        $mail->Password = 'password!';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

        $mail->From = 'abc@gmail.com';
        $mail->FromName = 'John Doe';
        $mail->addAddress('email@stanford.edu, John Doe');  // Add a recipient


        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters

        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = 'Your webhook works!!!!!';

        $mail->Body    = "The message sent!";


        if(!$mail->send()) {
           echo 'Message could not be sent. Contact us at hello@beerboy.co.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
}
?>

【问题讨论】:

  • Stripe 是响应 500,还是您自己的服务器在这样做?我怀疑$body = @file_get_contents('php://input'); 让它不开心。您是否检查过日志以了解错误发生的位置?
  • 我得到了这个...致命错误:未捕获的异常“Stripe_InvalidRequestError”,带有消息“无法确定要请求的 URL:Stripe_Event 实例的 ID 无效:stripe/lib/Stripe/ApiResource.php:46 .我不确定它是否抛出了这个,因为那里没有事件 ID,因为它应该得到那个前条纹。

标签: php json stripe-payments phpmailer webhooks


【解决方案1】:

检查此代码:

// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

$body 是使用php://input 定义的,它希望从提交给您的页面而不是Stripe 的POST 或GET 信息中读取。 See here。 POST 或 GET 中的任何内容显然都是无效的 JSON 或包含无效的 id

所以,当您尝试json_decode($body) 时,您是在尝试 json_decode POST 或 GET 中的内容,而不是您从 Stripe 获得的内容。 $event_json-&gt;id 不存在或无效,所以$event_id 不存在或无效,所以当你调用Stripe_Event::retrieve($event_id); 时,Stripe 会翻转。

在拨打Stripe_Event 之前尝试var_dump($event_json); die();,看看您的请求中有什么。

编辑:确保您发布(或包括查询字符串)语法有效的 JSON。换句话说,您的用户将如何访问此页面?确保无论它们来自何处,输入都包含有效的 JSON 并符合您的期望(即,具有 id 参数等)。

【讨论】:

  • 我刚刚得到“NULL”...那么我该如何补救呢? ://
  • 好吧,给你。如果$event_json 被证明为空或无效,那么你接下来的几个语句就注定了。您应该在此处验证您的输入并确保它们不为空、匹配预期的语法等。请注意,您必须在 GET 或 POST 中有一些 JSON,否则您的 @987654335 将无法@ 将工作。如果您尝试通过直接调用(非 GET 或 POST)来测试此页面,您将总是抛出错误。
  • 我可能错了,但似乎请求来自 Stripe,我的代码应该只是处理它。 “Webhook 数据在请求正文中以 JSON 格式发送。”stripe.com/docs/webhooks#receiving-a-webhook
  • 你如何测试这个?如果您没有向其发送任何数据,或者如果数据不是具有有效id 参数的有效 JSON,则它将不起作用。
  • @JonathanOsborn 如果这个答案有帮助,您是否介意接受“有帮助”?
【解决方案2】:

对于仍在寻找答案的人,请从file_get_contents() 函数中删除“@”符号:

`Stripe::setApiKey("sk_test_5cgfJ8yqBHE8L6radSAUhoo7");
$input = file_get_contents("php://input");
$event_json = json_decode($input);
var_dump($event_json);
http_response_code(200); // PHP 5.4 or greater`

从 stripe webhooks 部分为 stripe admin 发送测试。您将收到一条消息“测试 webhook 发送成功”,单击此查看响应,该响应应该是请求对象的数组。

【讨论】:

    【解决方案3】:

    你应该在最后添加这一行:

    http_response_code(200); // PHP 5.4 或更高版本

    在所有exit;s 之前

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 2018-04-10
      • 2022-10-18
      • 2016-03-07
      • 2016-09-12
      • 1970-01-01
      • 2016-03-09
      • 2021-04-03
      • 2022-01-18
      相关资源
      最近更新 更多