
本文介绍了如何在使用 Stripe 预构建 Checkout 时获取客户数据(特别是 Customer ID),以便将其存储在数据库中。重点讲解了通过 Stripe Webhooks 监听 checkout.session.completed 事件来获取 Customer ID 的方法,并提供了相关文档链接,帮助开发者更好地集成 Stripe Checkout 并管理客户数据。
Stripe 预构建 Checkout 提供了一种快速简便的方式来接受付款和创建订阅。然而,在许多情况下,您需要获取有关已成功完成付款的客户的信息,例如 Customer ID,以便将其存储在您的数据库中并用于后续操作。由于预构建 Checkout 会自动处理客户创建,因此需要采用特定的方法来获取这些信息。
使用 Webhooks 监听 checkout.session.completed 事件
获取 Customer ID 的推荐方法是使用 Stripe Webhooks。Webhooks 允许您在 Stripe 中发生特定事件时接收实时通知。对于预构建 Checkout,您需要监听 checkout.session.completed 事件。
当 Checkout Session 成功完成时,Stripe 会向您配置的 Webhook 端点发送一个包含 Checkout Session 信息的事件。此信息包括 Customer ID。
设置 Webhook 端点
首先,您需要在您的服务器上设置一个 Webhook 端点来接收 Stripe 发送的事件。以下是一个使用 PHP 的示例:
<?php
require_once 'vendor/autoload.php';
// Replace with your Stripe secret key
\Stripe\Stripe::setApiKey('sk_test_...');
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$endpoint_secret = 'whsec_...'; // Replace with your Webhook signing secret
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed') {
$session = $event->data->object;
// Get the Customer ID
$customer_id = $session->customer;
// Get the checkout session ID
$checkout_session_id = $session->id;
// TODO: Store the Customer ID and checkout_session_id in your database
// Example:
// $mysqli = new mysqli("localhost", "user", "password", "database");
// $stmt = $mysqli->prepare("INSERT INTO customers (customer_id, checkout_session_id) VALUES (?, ?)");
// $stmt->bind_param("ss", $customer_id, $checkout_session_id);
// $stmt->execute();
// $stmt->close();
error_log("Customer ID: " . $customer_id);
error_log("Checkout Session ID: " . $checkout_session_id);
}
http_response_code(200); // Return a 200 OK response to acknowledge receipt of the event重要注意事项:
配置 Stripe Webhook
在 Stripe 控制台中,转到 "开发者" -> "Webhooks",然后点击 "添加端点"。
测试 Webhook
您可以使用 Stripe CLI 或通过在 Stripe 控制台中触发 Checkout Session 来测试您的 Webhook。
使用已存在的 Customer ID
如果您希望在创建 Checkout Session 时使用现有的 Customer ID,可以在 \Stripe\Checkout\Session::create() 方法中传递 customer 参数:
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price'=>"price_1Jt.....vImqj",
'quantity'=>1,
]],
'mode' => 'subscription',
'success_url' => $YOUR_DOMAIN . '/success.php',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
'customer' => 'cus_...' // Replace with your existing Customer ID
]);总结
通过监听 checkout.session.completed 事件,您可以轻松地获取 Stripe 预构建 Checkout 创建的客户的 Customer ID,并将其存储在您的数据库中。 确保验证 Webhook 签名以确保安全性,并添加适当的错误处理机制。 此外,如果已经存在客户ID,可以在创建Checkout Session时直接使用。 这样,您就可以更好地管理客户数据并构建更强大的集成。 详细信息请参考 Stripe 官方文档:https://www.php.cn/link/8cbf1695be0572428dbb70f41f833783 和 https://www.php.cn/link/755945a59ff256394631b079277ab8bc。
以上就是从 Stripe 预构建 Checkout 获取客户数据的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号