Php 调试条带Webhook事件
我花了周末的时间试图找出Stripe Webhook,但仍然没有找到调试响应的方法。这是我当前的代码:Php 调试条带Webhook事件,php,stripe-payments,webhooks,Php,Stripe Payments,Webhooks,我花了周末的时间试图找出Stripe Webhook,但仍然没有找到调试响应的方法。这是我当前的代码: http_response_code(200); // set stripe api key Stripe::setApiKey(env('STRIPE_SECRET')); $endpoint_secret = 'whsec_XXX'; $payload = @file_get_contents('php://input')
http_response_code(200);
// set stripe api key
Stripe::setApiKey(env('STRIPE_SECRET'));
$endpoint_secret = 'whsec_XXX';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event_json = json_decode($payload);
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Error\SignatureVerification $e) {
// Invalid signature
http_response_code(400);
exit();
}
$event_id = $event_json->id;
if(isset($event_json->id)) {
try {
// to verify this is a real event, we re-retrieve the event from Stripe
$event = \Stripe\Event::retrieve($event_id);
$invoice = $event->data->object;
// successful payment, both one time and recurring payments
if($event->type == 'charge.succeeded') {
$customer = \Stripe\Customer::retrieve($invoice->customer);
$email = $customer->email;
\Mail::send('emails.new-userlike',
array(
'user' => $customer
), function($message) {
$message->from('info@friendships.me', 'friendships.me');
$message->to('info@friendships.me')->subject('Test');
});
}
// failed payment
if($event->type == 'charge.failed') {
// send a failed payment notice email here
}
} catch (Exception $e) {
// something failed, perhaps log a notice or email the site admin
}
}
到目前为止,这将导致错误500
但这不是问题所在,我已经让它工作了。问题是,我需要检查SEPA订阅的费用。失败的或费用。成功的响应,只有在成功的费用下,才创建订阅
如何访问此webhook中的订阅id?或者更好,如何调试响应?因为即使这样也不会发送响应:
http_response_code(200);
$payload = @file_get_contents('php://input');
$event_json = json_decode($payload);
print_r("test");
我首先从最简单的webhook处理程序开始
<?php
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event = json_decode($input);
http_response_code(200); // PHP 5.4 or greater
// echo the event id, evt_xxxyyyzzz
echo $event->id;
if($event->type == "charge.succeeded") {
// you want the id of the charge rather than the event, ch_xxxyyzz
echo $event->data->object->id;
}
?>
基本处理程序工作了,但我没有收到任何响应。不过我现在让它工作了。在我打开另一个问题之前:我是否无法使用\Mail::send发送邮件?我在整个系统中都使用它,但在webhook中尝试时会抛出一个错误500。一个简单的PHP Mail()可以正常工作。Stripe的SDK不包含邮件类AFAIK。您使用的是像Laravel这样的框架还是来自另一家公司的SDK?现在一切正常,谢谢!:)我有一大堆错误,但没有想到错误。log:/Woohoo!很高兴听到一切都很好:)首先,试着找出它为什么会导致500人死亡。您可以从自己的机器向dev服务器发送一些虚拟webhook事件JSON,只是为了调试。然后找出问题所在。