首页 > 解决方案 > 尝试连接到 Stripe API 有人可以带我逐步了解这个 webhook 正在做什么(php)

问题描述

正如标题所说,我正在尝试使用 webhook 来自动化一些在客户完成订阅过程时发生的 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);

标签: phpstripe-paymentswebhooks

解决方案


/* 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是有意义的事件 ( https://stripe.com/docs/api/events/object ) 对象,它是 webhook 请求的主体。Stripe 的 PHP 库具有constructEvent获取传入请求正文并将其解析/转换为您可以使用的对象的功能,这就是该代码正在做的事情。

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

事件包含一个“有效负载”对象,它是事件所涉及的实际 API 对象。例如,如果创建了 Subscription,您会收到一个customer.subscription.created事件,并且该有效负载是 Subscription 对象。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


推荐阅读