php - 无法在 Stripe webhook 中检索会话的变量值
问题描述
我正在我的网站中设置 Stripe 订阅 webhook。我仔细阅读了文档,现在我的网站中设置了 webhook。
现在,我希望更新我网站数据库上的一条记录,在我的一个用户订阅我的会员计划后,该记录上的一列被设置为另一个值。我试图在包含电子邮件地址的会话 PHPSESSID 中检索会话值。
使用 SQL 'WHERE' 子句,我尝试了以下操作:
$sql = "UPDATE xxxxxx SET Plan = 'Professional' WHERE EmailAddress = '$email'";
if ($conn->query($sql) === TRUE)
{
echo "You have subscribed to the professional plan";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
请注意:
- SQL 语法在我的 phpmyadmin 站点上完美运行。
- mysqli 连接到我网站的数据库。
- webhook 也完全没有问题。
唯一的问题是处理付款后我无法在 webhook 上运行 SQL 查询。经过一点调试,我发现 PHPSESSID 会话中的 email 变量没有传递到“UPDATE xxxxxx SET Plan = 'Professional'”行。
<?php
session_name("PHPSESSID");
session_start();
$email = $_SESSION["email_address"];
require_once('stripe-php/init.php');
\Stripe\Stripe::setApiKey('xxxxxx');
$endpoint_secret = 'xxxxxx';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
http_response_code(400);
exit();
} catch(\Stripe\Error\SignatureVerification $e) {
http_response_code(400);
exit();
}
// Handle the checkout.session.completed event
if ($event->type == 'checkout.session.completed')
{
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxx";
$dbname = "xxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$amount = $event['data']['object']['display_items'][0]['amount'];
switch ($amount)
{
case 499:
{
$sql = "UPDATE xxxxxx SET Plan = 'Professional' WHERE EmailAddress = '$email'";
if ($conn->query($sql) === TRUE)
{
echo "You have subscribed to the professional plan";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
break;
case 899:
{
$sql = "UPDATE xxxxxx SET Plan = 'Premium' WHERE EmailAddress = 'xxxxxx'";
if ($conn->query($sql) === TRUE)
{
echo "You have subscribed to the Premium plan";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
break;
default:
{
$sql = "UPDATE xxxxxx SET Plan = 'Premium' WHERE EmailAddress = '$email'";
if ($conn->query($sql) === TRUE)
{
echo "You have subscribed to the Premium plan on free trial.";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
}
}
http_response_code(200);
?>
关于如何传递变量值的任何建议?还是我在这里做错了整个 webhook 的事情?
解决方案
解决了!
我意识到在 webhook JSON 日志中,有一个用户完成付款后的客户 ID。因此,我可以使用 ID 来获取电子邮件地址。
这是代码:
$amount = $event['data']['object']['display_items'][0]['amount'];
// Retrieve the customer ID
$customerToken = $event['data']['object']['customer'];
switch ($amount)
{
case 499:
{
// Fetch the email address
$customerInfo = \Stripe\Customer::retrieve($customerToken);
$email = $customerInfo->email;
// Use the email variable on the SQL query
$sql = "UPDATE JobDesktop.UserDatabase SET Plan = 'Professional' WHERE EmailAddress = '$email'";
if ($conn->query($sql) === TRUE)
{
echo "You have subscribed to the professional plan";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
// The rest of the code down below
推荐阅读
- angular - mat-dialog 弹出窗口将页面导航回默认路由
- python - 在使用 python 检索 selenium webdriver 元素文本时需要帮助
- java - 将 AOP 功能添加到 Spring 项目后,没有找到合格的 Bean 错误
- javascript - React PerfectScrollBar 在容器底部滚动以获取动态生成的内容
- bash - 理解 diff -u 的输出
- php - 如何使用 taxonomy-term.html.twig 为 drupal 8 主题分类术语
- python - 虽然不会停止
- python - 如何将 %r 从格式化时间 MySQL 查询传递到 Python 代码?
- django - Django allauth google extra_data
- javascript - 如何在浏览器内存中保存信息并在按下时显示而不使用react刷新页面?