gpt4 book ai didi

php - Stripe Webhook 错误 500

转载 作者:行者123 更新时间:2023-12-02 03:41:02 24 4
gpt4 key购买 nike

我正在尝试设置一个 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;
}
}
?>

最佳答案

检查此代码:

// 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 定义,它想从提交给你的页面的 POST 或 GET 信息中读取,而不是 Stripe 。 See here . POST 或 GET 中的任何内容显然都是无效的 JSON 或包含无效的 id .

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

试试 var_dump($event_json); die();在您制作 Stripe_Event 之前打电话看看你的要求是什么。

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

关于php - Stripe Webhook 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20055002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com