无法在Stripe Webhook中检索会话的变量值

时间:2019-07-03 06:37:07

标签: php sql stripe-payments webhooks

我正在我的网站中设置一个Stripe订阅网络挂钩。我仔细阅读了文档,然后在我的网站上设置了网络挂钩。

现在,我要更新网站数据库中的记录,其中一个用户订阅了我的会员计划后,记录中的列设置为另一个值。我试图在会话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);
?>

关于如何将值传递给变量的任何建议?还是我在这里把整个网络挂钩做错了?

1 个答案:

答案 0 :(得分:0)

解决了!

我意识到在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