
本文档旨在指导开发者如何在使用 Stripe 预构建 Checkout 集成到网站后,获取客户数据(特别是 Customer ID),并将其存储到 MySQL 数据库中。重点介绍如何通过 Stripe Webhooks 监听 checkout.session.completed 事件,并从中提取所需的客户信息。
Stripe 预构建 Checkout 提供了一种快速且安全的方式来接受付款,但有时我们需要在自己的数据库中存储客户信息,例如 Customer ID。由于 Checkout 会自动根据用户提供的邮箱等信息创建客户,我们需要一种方法来获取这个 Customer ID。本文将介绍如何通过 Stripe Webhooks 实现这一目标。
首先,我们需要创建一个 Checkout Session。以下是一个示例 PHP 代码:
<?php
require_once 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51J...........esLwtMQx7IXNxp00epljtC43');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'mydomain.com';
$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',
]);
header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);注意: sk_test_51J...........esLwtMQx7IXNxp00epljtC43 替换为你自己的 Stripe 测试密钥。mydomain.com 替换为你自己的域名。
在创建 Checkout Session 时,我们没有显式地传递 customer 参数。这意味着 Stripe 将根据用户在 Checkout 流程中提供的信息自动创建一个新的 Customer 对象。
Webhooks 允许你的应用程序在 Stripe 上发生特定事件时接收实时通知。 为了获取 Customer ID,我们需要监听 checkout.session.completed 事件。
首先,你需要创建一个 Webhook endpoint,Stripe 将向其发送事件数据。 这个 endpoint 应该是一个可以接收 POST 请求的 URL。
<?php
require_once 'vendor/autoload.php';
// Replace with your actual secret key
$stripeSecretKey = 'whsec_YOUR_WEBHOOK_SECRET';
// The library needs to have its signature validation disabled if using
// a Webhook signing secret.
\Stripe\Stripe::setApiKey('sk_test_51J...........esLwtMQx7IXNxp00epljtC43');
// Use setWebhookSignatureValidation if you want to use a Webhook signing secret
\Stripe\Stripe::setWebhookSignatureValidation($stripeSecretKey);
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $stripeSecretKey
);
} 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;
// TODO: Store the Customer ID in your database
// Example:
// $mysqli = new mysqli("localhost", "user", "password", "database");
// $stmt = $mysqli->prepare("INSERT INTO customers (customer_id) VALUES (?)");
// $stmt->bind_param("s", $customer_id);
// $stmt->execute();
echo "Customer ID: " . $customer_id . "\n";
}
http_response_code(200); // Return a 200 OK response注意: whsec_YOUR_WEBHOOK_SECRET 替换为你自己的 Webhook Secret。 sk_test_51J...........esLwtMQx7IXNxp00epljtC43 替换为你自己的 Stripe 测试密钥。 确保你的 Webhook endpoint 可以公开访问。
当 Checkout Session 成功完成时,Stripe 会向你的 Webhook endpoint 发送一个 checkout.session.completed 事件。 在你的 Webhook endpoint 中,你需要解析这个事件并提取 Customer ID。
在上面的示例代码中,我们已经演示了如何处理 checkout.session.completed 事件,并从中提取 Customer ID。
最后,你需要将提取到的 Customer ID 存储到你的 MySQL 数据库中。 在上面的示例代码中,我们提供了一个简单的示例,展示了如何使用 PHP 和 MySQLi 扩展来将 Customer ID 插入到 customers 表中。 你需要根据你的实际数据库结构和需求进行修改。
注意: 请务必对数据库操作进行适当的错误处理和安全措施,以防止 SQL 注入等安全问题。
如果你希望在创建 Checkout Session 时使用现有的 Customer ID,可以在创建 Checkout Session 时传递 customer 参数。
<?php
require_once 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51J...........esLwtMQx7IXNxp00epljtC43');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'mydomain.com';
// Replace with your existing Customer ID
$existing_customer_id = 'cus_XXXXXXXXXXXXXXX';
$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' => $existing_customer_id, // Pass the existing Customer ID
]);
header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);注意: cus_XXXXXXXXXXXXXXX 替换为你自己的 Customer ID。
如果传递了 customer 参数,Stripe 将不会创建新的 Customer 对象,而是使用指定的 Customer 对象。
通过使用 Stripe Webhooks,我们可以轻松地获取 Stripe Checkout 创建的 Customer ID,并将其存储到我们的数据库中。 这使得我们可以更好地管理客户信息,并将其与其他业务数据集成。 请务必仔细阅读 Stripe 官方文档,并根据你的实际需求进行调整。 此外,请始终关注安全性,并采取必要的措施来保护你的数据。
以上就是使用 Stripe Checkout 获取客户数据并集成到数据库的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号