首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >条形500钩子误差500

条形500钩子误差500
EN

Stack Overflow用户
提问于 2013-11-18 18:23:31
回答 3查看 10.2K关注 0票数 4

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

代码语言:javascript
复制
<?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;
        }
}
?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-18 18:41:26

检查此代码:

代码语言:javascript
复制
// 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中的任何内容都是无效的JSON或包含无效的id

因此,当您尝试json_decode($body)时,您正在尝试json_decode在POST或GET中的内容,而不是从Stripe获得的内容。$event_json->id不存在或无效,所以$event_id不存在或无效,所以当您调用Stripe_Event::retrieve($event_id);时,Stripe就会崩溃。

在进行var_dump($event_json); die();调用之前先尝试Stripe_Event,并查看请求中的内容。

编辑:确保您是POSTing (或包含查询字符串),语法上是有效的JSON。换句话说,您的用户将如何到达此页面?确保输入包含有效的JSON并与您的期望相匹配(例如,有一个id参数,等等)。

票数 1
EN

Stack Overflow用户

发布于 2014-08-26 04:09:37

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

代码语言:javascript
复制
`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`

发送一个测试从条纹webhooks部分条管理。您将收到一条消息"Test已成功发送“,单击此按钮可查看响应,该响应应该是请求对象的数组。

票数 1
EN

Stack Overflow用户

发布于 2014-08-25 17:49:33

您应该在末尾添加这一行:

http_response_code(200);// PHP5.4或更高版本

在所有的exit;之前

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20055002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档