尝试连接到 Stripe API 谁能带我逐步了解这个 webhook 正在做什么 (php)

Trying to connect to Stripe API could someone take me through what this webhook is doing step by step (php)

正如标题所说,我正在尝试使用 webhooks 来自动化一些在客户完成订阅过程时发生的 Stripe 事件。

在我的 webhooks.php 文件中,有一些我不理解的代码:

/* 这是我的评论 */

// 这些是来自 Stripe 文档的评论

/* First snippet of code - Not sure what this does? */
$payload = @file_get_contents('php://input');

/* Second snippet of code - Not sure what this does? 
what is the $event variable refering to and why is $payload being 
passed into it? */

$event = null;
try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch (\UnexpectedValueException $e) {
    // Invalid payload
    echo '⚠️  Webhook error while parsing basic request.';
    http_response_code(400);
    exit();
}

/* Third snippet of code - I understand the switch and case keywords to 
basically be if and else statements so if the type of the $event variable is 
equal to 'customer.subscription.trial_will_end' then run '$subscription = 
$event -> data -> object'. However what is the $subscription variable being 
assigned to in this case (what does $event -> data -> object mean?) */

// Handle the event
switch ($event->type) {
    case 'customer.subscription.trial_will_end':
        $subscription = $event->data->object; // contains a \Stripe\Subscription
        // Then define and call a method to handle the trial ending.
        // handleTrialWillEnd($subscription);
        break;
    case 'customer.subscription.created':
        $subscription = $event->data->object; // contains a \Stripe\Subscription
        // Then define and call a method to handle the subscription being created.
        // handleSubscriptionCreated($subscription);
        break;
    case 'customer.subscription.deleted':
        $subscription = $event->data->object; // contains a \Stripe\Subscription
        // Then define and call a method to handle the subscription being deleted.
        // handleSubscriptionDeleted($subscription);
        break;
    case 'customer.subscription.updated':
        $subscription = $event->data->object; // contains a \Stripe\Subscription
        // Then define and call a method to handle the subscription being updated.
        // handleSubscriptionUpdated($subscription);
        break;
    default:
        // Unexpected event type
        echo 'Received unknown event type';
}

/* Fourth snippet of code - Why do we need to set this? */
http_response_code(200);
/* First snippet of code - Not sure what this does? */
$payload = @file_get_contents('php://input');

它读取此脚本正在接收的 HTTP POST 请求的请求正文(webhook 处理程序正在接收 Stripe 发送给您的服务器的 HTTP 请求)。这是一个标准的 PHP 成语。

/* Second snippet of code - Not sure what this does? 
what is the $event variable refering to and why is $payload being 
passed into it? */

$event 是有意义的 Event(https://stripe.com/docs/api/events/object) 对象,它是 webhook 请求的主体。 Stripe 的 PHP 库有一个函数 constructEvent 来获取传入的请求主体并将其 parse/convert 到一个你可以使用的对象,这就是代码正在做的事情。

However what is the $subscription variable being 
assigned to in this case (what does $event -> data -> object mean?) */

事件包含一个 'payload' 对象,它是事件所涉及的实际 API 对象。例如,如果创建了订阅,您会收到一个 customer.subscription.created 事件,并且该有效负载是订阅对象。这些都包含在 Stripe 的文档中。 https://stripe.com/docs/api/events/object#event_object-data-object /// https://stripe.com/docs/api/events/types#event_types-customer.subscription.created

/* Fourth snippet of code - Why do we need to set this? */

因为在收到来自 Stripe 的传入请求后,您必须回复以让他们知道您收到了。 https://stripe.com/docs/webhooks/build#acknowledge-events-immediately