问题
I am setting up a Stripe subscription webhook in my website. I followed through the documentation and the webhook is now set in my website.
Now I am looking to update a record on my website's database where a column on the record is set to another value after one of my users subscribes to my membership plan. I tried to retrieve a session value in the session PHPSESSID where it contains an email address.
Using the SQL 'WHERE' clause, I tried the following:
$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();
Please note that:
- the SQL syntax works perfectly on my phpmyadmin site.
- The mysqli is connected to my website's database.
- The webhook also works with no problem at all.
The only problem is I cannot run the SQL query on the webhook after the payment is processed. After a little bit of debugging, I found out that the email variable in the PHPSESSID session isn't passed to the "UPDATE xxxxxx SET Plan = 'Professional'" line.
<?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);
?>
Any suggestions on how to pass the value on the variable? Or am I doing the whole webhook thing wrong here?
回答1:
Solved it!
I realized that on the webhook JSON log, there is a customer ID after a user has completed the payment. So, I can use the ID to fetch the email address.
Here's the code:
$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
来源:https://stackoverflow.com/questions/56864252/unable-to-retrieve-a-sessions-variable-values-in-a-stripe-webhook