条纹webhook事件不起作用

时间:2014-12-06 13:07:41

标签: php stripe-payments webhooks

我想在客户注册计划后发送电子邮件

我已经设置了条带中的webhook,但它没有发送电子邮件

我做错了什么,这是webhook文件

<?php

require 'core/lib/Stripe.php';

Stripe::setApiKey("YOUR_SECRET_KEY");

// 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') {

    $To = 'myemail@email.com'; 
    $Subject = 'Stripe Message'; 
    $Message = 'Webhook worked'; 
    $Headers = "From: coolio@yahoo.com \r\n" . 
    "Reply-To: coolio@yahoo.com \r\n" . 
    "Content-type: text/html; charset=UTF-8 \r\n"; 
    mail($To, $Subject, $Message, $Headers);

}

http_response_code(200); // PHP 5.4 or greater
?>

当我查看Stripe中的事件时,它没有显示任何响应

1 个答案:

答案 0 :(得分:0)

您可能希望从Stripe获取事件以测试有效性并处理异常(请注意,这会故意通过测试webhooks失败):

try {
    // Check against Stripe to confirm that the ID is valid
    $event_id = $event_json->id;
    $event = Stripe_Event::retrieve($event_id);
}
catch (Stripe_InvalidRequestError $e) {
    // If the event is invalid, log an error
    error_log($e->getMessage(),0);
}

然后你应该能够像这样获得事件类型属性:

    if (isset($event) && $event->type == "charge.succeeded") {
      // Send your email
    }