无法在 Stripe webhook 中检索会话的变量值 [英] Unable to retrieve a session's variable values in a Stripe webhook

查看:32
本文介绍了无法在 Stripe webhook 中检索会话的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的网站上设置 Stripe 订阅网络钩子.我按照文档进行了操作,现在 Webhook 已设置在我的网站中.

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.

现在我希望更新我网站数据库中的记录,在我的一个用户订阅我的会员计划后,记录中的一列被设置为另一个值.我试图在包含电子邮件地址的会话 PHPSESSID 中检索会话值.

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.

使用 SQL 'WHERE' 子句,我尝试了以下操作:

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();

请注意:

  • SQL 语法在我的 phpmyadmin 站点上完美运行.
  • mysqli 连接到我网站的数据库.
  • Webhook 也完全没有问题.

唯一的问题是我无法在处理付款后在 webhook 上运行 SQL 查询.经过一些调试,我发现 PHPSESSID 会话中的电子邮件变量没有传递到UPDATE xxxxxx SET Plan = 'Professional'"行.

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?

推荐答案

解决了!

我发现在 webhook JSON 日志上,有一个用户完成付款后的客户 ID.因此,我可以使用 ID 来获取电子邮件地址.

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.

代码如下:

$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

这篇关于无法在 Stripe webhook 中检索会话的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆