在 WordPress 插件中的 Stripe customer.subscription.created 事件后不更新用户角色

发布于 2025-01-12 06:19:58 字数 5293 浏览 0 评论 0原文

我正在创建一个 Wordpress 插件来处理我的网站的不同付费会员级别。 我已成功使用 Stripe 创建订阅,并在 Stripe 仪表板上收到“200 OK”响应。事件 customer.subscription.created 已成功触发。
如果用户选择 5 欧元/月的订阅,他将可以访问我网站上的一些特定帖子。我需要将他们的角色从“订阅者”更改为“自学者”才能实现这一点,但是当我检查数据库时,用户角色尚未更新,即使订阅已成功创建。 这是我的代码:

function stripe_webhook() {

    if(isset($_GET['webhook-listener']) && $_GET['webhook-listener'] == 'stripe') {

        if (is_user_logged_in()){ 

            $current_user = wp_get_current_user();

            require_once(plugin_dir_path(__FILE__) . 'stripe-library/init.php');

            global $italianglot_options;
            $italianglot_options = get_option('italianglot_settings');

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $publishable = $italianglot_options['test_publishable_key'];
            } else {
                $publishable = $italianglot_options['live_publishable_key'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $secret_key = $italianglot_options['test_secret_key']; 
            } else {
                $secret_key = $italianglot_options['live_secret_key'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $price_selflearner_monthly = $italianglot_options['test_selflearner_monthly']; 
            } else {
                $price_selflearner_monthly = $italianglot_options['live_selflearner_monthly'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $price_selflearner_yearly = $italianglot_options['test_selflearner_yearly']; 
            } else {
                $price_selflearner_yearly = $italianglot_options['live_selflearner_yearly'];
            }

            Stripe::setApiKey($secret_key);

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $endpoint_secret = $italianglot_options['test_endpoint_secret'];
            }else{
                $endpoint_secret = $italianglot_options['live_endpoint_secret'];
            }

            $body = @file_get_contents('php://input');
            $event_json = null;
            $event_json = json_decode($body);

            if(isset($event_json->id)) {
                try {

                    $event_id = $event_json->id;
                    $event = Stripe_Event::retrieve($event_id);
                    
                    switch ($event->type) {
                        case 'customer.subscription.trial_will_end':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs three days before a subscription's trial period is scheduled to end, or when a trial is ended immediately (using trial_end=now).
                            // handleTrialWillEnd($subscription);
                            break;
                        case 'customer.subscription.created':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a customer is signed up for a new plan.

                            $priceId = $subscription->items->data->price->id;
                        
                            if ($priceId == $price_selflearner_monthly || $priceId == $price_selflearner_yearly){
                                $current_user->set_role('self_learner');
                            }
                            
                           
                            break;
                        case 'customer.subscription.deleted':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a customer's subscription ends.
                            
                            break;
                        case 'customer.subscription.updated':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a subscription changes (e.g., switching from one plan to another, or changing the status from trial to active).
                            
                            break;
                        default:
                            // Unexpected event type
                            echo 'Received unknown event type';
                    }    

                } catch(\UnexpectedValueException $e) {
                // Invalid payload
                echo '⚠️  Webhook error while parsing basic request.';
                http_response_code(400);
                exit();
                }
            }    
        }    
    }
}

我什至不知道如何调试这段代码。我尝试检查将某些变量写入文件的值,但该文件始终为空。就好像 Case 'customer.subscription.cerated' 内的任何内容都没有被执行。

我用这段代码写在一个errors.txt 文件中:

$text = "PRICE ID: " . $priceId;
$file = plugin_dir_path( __FILE__ ) . '/errors.txt'; 
$open = fopen( $file, "a" ); 
$write = fputs( $open, $text); 
fclose( $open );

我希望有人能找出我的代码不起作用的原因。非常感谢!

I'm creating a Wordpress Plugin to handle different Paid Membership levels for my website.
I've managed to create a subscription with Stripe and on my Stripe Dashboard I get a "200 OK" response. The event customer.subscription.created is triggered successfully.
If the user has chosen a 5€/month subscription, he will get access to some specific posts on my website. I need to change their role from "subscriber" to "self learner" for that to happen, but when I check the Database, the user role hasn't been updated, even if the subscription has been successfully created.
This is my code:

function stripe_webhook() {

    if(isset($_GET['webhook-listener']) && $_GET['webhook-listener'] == 'stripe') {

        if (is_user_logged_in()){ 

            $current_user = wp_get_current_user();

            require_once(plugin_dir_path(__FILE__) . 'stripe-library/init.php');

            global $italianglot_options;
            $italianglot_options = get_option('italianglot_settings');

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $publishable = $italianglot_options['test_publishable_key'];
            } else {
                $publishable = $italianglot_options['live_publishable_key'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $secret_key = $italianglot_options['test_secret_key']; 
            } else {
                $secret_key = $italianglot_options['live_secret_key'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $price_selflearner_monthly = $italianglot_options['test_selflearner_monthly']; 
            } else {
                $price_selflearner_monthly = $italianglot_options['live_selflearner_monthly'];
            }

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $price_selflearner_yearly = $italianglot_options['test_selflearner_yearly']; 
            } else {
                $price_selflearner_yearly = $italianglot_options['live_selflearner_yearly'];
            }

            Stripe::setApiKey($secret_key);

            if(isset($italianglot_options['test_mode']) && $italianglot_options['test_mode']) { 
                $endpoint_secret = $italianglot_options['test_endpoint_secret'];
            }else{
                $endpoint_secret = $italianglot_options['live_endpoint_secret'];
            }

            $body = @file_get_contents('php://input');
            $event_json = null;
            $event_json = json_decode($body);

            if(isset($event_json->id)) {
                try {

                    $event_id = $event_json->id;
                    $event = Stripe_Event::retrieve($event_id);
                    
                    switch ($event->type) {
                        case 'customer.subscription.trial_will_end':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs three days before a subscription's trial period is scheduled to end, or when a trial is ended immediately (using trial_end=now).
                            // handleTrialWillEnd($subscription);
                            break;
                        case 'customer.subscription.created':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a customer is signed up for a new plan.

                            $priceId = $subscription->items->data->price->id;
                        
                            if ($priceId == $price_selflearner_monthly || $priceId == $price_selflearner_yearly){
                                $current_user->set_role('self_learner');
                            }
                            
                           
                            break;
                        case 'customer.subscription.deleted':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a customer's subscription ends.
                            
                            break;
                        case 'customer.subscription.updated':
                            $subscription = $event->data->object; // contains a \Stripe\Subscription
                            // Occurs whenever a subscription changes (e.g., switching from one plan to another, or changing the status from trial to active).
                            
                            break;
                        default:
                            // Unexpected event type
                            echo 'Received unknown event type';
                    }    

                } catch(\UnexpectedValueException $e) {
                // Invalid payload
                echo '⚠️  Webhook error while parsing basic request.';
                http_response_code(400);
                exit();
                }
            }    
        }    
    }
}

I don't even know how to debug this code. I tried to inspect the value of some variables writing them in a file, but the file is always empty. It's as if nothing inside the Case 'customer.subscription.crated' gets executed.

I used this code to write in an errors.txt file:

$text = "PRICE ID: " . $priceId;
$file = plugin_dir_path( __FILE__ ) . '/errors.txt'; 
$open = fopen( $file, "a" ); 
$write = fputs( $open, $text); 
fclose( $open );

I hope someone can figure out why my code isn't working. Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文