php - 尝试连接到 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);
解决方案
/* 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
推荐阅读
- c++ - 使用 grpc 构建项目时 gitlab 中的错误
- javascript - 我的 OR 运算符似乎无法正常工作 - 初学者
- javascript - 如何递归嵌套对象值,以使父对象包含相应的命名集合?
- android - 在 Android Studio 中测量高度无法正常工作
- jquery - 我无法在 vscode 上设置 jQuery
- javascript - 如何自定义内置的 javascript 函数?
- powerbi - 无法将变量放入导出的 Excel 数据集中的值框中
- java - 如何让每次点击时显示插页式广告请更正我的代码
- python-3.x - 展开直到找到匹配的 PyParsing Or 运算符
- sql - 不插入成员的触发器不起作用