【发布时间】:2019-04-14 10:58:40
【问题描述】:
我花了我的周末试图找出 Stripe Webhooks,但仍然没有找到调试响应的方法。这是我当前的代码:
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 订阅是否有 charge.failed 或 charge.succeeded 响应,并且只有在成功收费后,才能创建订阅。
如何在这个 webhook 中访问订阅 ID?或者更好的是,我如何调试响应?因为即使这样也没有发送响应:
http_response_code(200);
$payload = @file_get_contents('php://input');
$event_json = json_decode($payload);
print_r("test");
【问题讨论】:
-
首先,尝试找出导致 500 的原因。您可以从自己的机器发送一些虚拟的 webhook 事件 JSON 到开发服务器,仅用于调试目的。然后找出问题所在。
标签: php stripe-payments webhooks