[英]Stripe Webhook Error 500
我正在嘗試建立一個Stripe Webhook,一旦發生“ charge.succeeded”事件,該Webhook就會發送電子郵件。 當我在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
定義的,它想從提交到您的頁面而不是Stripe的POST或GET信息中讀取信息。 看這里 。 POST或GET中的任何內容顯然都是無效的JSON或包含無效的id
。
因此,當您嘗試json_decode($body)
,您將嘗試json_decode POST或GET中的內容,而不是從Stripe獲得的內容。 $event_json->id
不存在或無效,因此$event_id
不存在或無效,因此當您調用Stripe_Event::retrieve($event_id);
時,Stripe翻轉出來Stripe_Event::retrieve($event_id);
。
嘗試var_dump($event_json); die();
var_dump($event_json); die();
在進行Stripe_Event
調用之前,請先查看請求中的內容。
編輯:確保您正在發布(或包括查詢字符串)語法上有效的JSON。 換句話說,您的用戶將如何訪問此頁面? 確保無論它們來自何處,輸入都包含有效的JSON並符合您的期望(即,具有id
參數等)。
對於仍在尋找答案的用戶,請從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已成功發送”,單擊此消息以查看響應,該響應應該是請求對象的數組。
您應該在最后添加以下行:
http_response_code(200); // PHP 5.4或更高版本
在所有exit;
之前 小號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.