尝试连接到 Stripe API 有人可以带我逐步了解这个 webhook 正在做啥(php)

Posted

技术标签:

【中文标题】尝试连接到 Stripe API 有人可以带我逐步了解这个 webhook 正在做啥(php)【英文标题】:Trying to connect to Stripe API could someone take me through what this webhook is doing step by step (php)尝试连接到 Stripe API 有人可以带我逐步了解这个 webhook 正在做什么(php) 【发布时间】:2021-11-15 18:39:43 【问题描述】:

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

在我的 webhooks.php 文件中,这些是我不明白的代码:

/* 这是我的 cmets */

// 这些是 Stripe 文档中的 cmets

/* 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);

【问题讨论】:

如果你想知道$event->data->object是什么,那么在上面做一个简单的print_r($event->data->object)就可以看到内容 【参考方案1】:
/* 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 来获取传入的请求正文并将其解析/转换为您可以使用的对象,这就是代码正在做的事情。

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

事件包含一个“有效负载”对象,它是事件所涉及的实际 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

【讨论】:

以上是关于尝试连接到 Stripe API 有人可以带我逐步了解这个 webhook 正在做啥(php)的主要内容,如果未能解决你的问题,请参考以下文章

从 Firebase Cloud Function 连接到 Stripe 时出错

通过 API 添加 Stripe 订阅

如何将 php 连接到 google SMTP 并发送邮件? [复制]

将 Notion API 连接到 Power BI 时出现错误 400

使用 Stripe 付款创建复杂订单

Maven Spring Boot 尝试连接到 MySQL