【发布时间】: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