Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brycematheson's avatar

Auth::user returns null inside SparkWebhook

Hello,

I apologize for the newbie question. This is my first time using Spark/Stripe Webhooks, along with event listeners.

After a successful payment in Spark, I'm wanting to write a few values to my database. The problem I'm having now is that Auth::user() returns null. I'm wanting to get the current logged on user, and perform some logic based off that.

Everything else appears working. My webhooks report successful in Stripe and there are no errors there. I'm simply unable to access Auth::user(). Thank you!

Here's my code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Mail\newAffiliateCommission;
use App\Models\AffiliateReferral;
use Closure;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

use Spark\Http\Controllers\WebhookController as SparkWebhookController;

class WebhookController extends SparkWebhookController
{
    /**
     * Handle invoice payment succeeded.
     *
     * @param  array  $payload
     * @return \Symfony\Component\HttpFoundation\Response
     */
    
    public function handleInvoicePaymentSucceeded(array $payload)
    {
        // no matter what I try, these return null 
        $user = \Auth::user();
        echo Auth::user()->id;
        Log::info("User ID: ".$user->id);
        // 

        return parent::handleInvoicePaymentSucceeded($payload);
    }
}
0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

The response from stripe comes in via a webhook direct from their server so is not providing the user session cookie.

You need something on the payment data to identify the user. Ive not used spark but surely there is a transaction record that identifies the user?

1 like
brycematheson's avatar

@snapey Thank you very much! Using your suggestion, I was able to figure it out. I've posted the code below to help out anyone in the future who has this issue.

I was able to find the customers' email using the $payload array, and then could do my logic from there. In my case, I'm getting the user's email, and then notifying the user who referred them to my site that they've made a new commission, and entering a few details into the DB.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Mail\newAffiliateCommission;
use App\Models\AffiliateReferral;
use Closure;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

use Spark\Http\Controllers\WebhookController as SparkWebhookController;

class WebhookController extends SparkWebhookController
{
    /**
     * Handle invoice payment succeeded.
     *
     * @param  array  $payload
     * @return \Symfony\Component\HttpFoundation\Response
     */
    
    public function handleInvoicePaymentSucceeded(array $payload)
    {
        $user = User::where('email',$payload['data']['object']['customer_email'])->first();
        
        //notify user if there's an affiliate commission
        if($user->referrer_id != NULL) {
            $referral = new AffiliateReferral();
            $referral->referrer_id = $user->referrer_id;
            $referral->referree_id = $user->id;
            $referral->payout_pending = 1;
            $referral->payout_complete = 0;
            $referral->save();

            $email = User::select('email')->where('id', $user->referrer_id)->first();
            \Mail::to($email)->send(new newAffiliateCommission());

            Log::info('-------------------------');
            Log::info('New affiliate commission email sent.');
            Log::info('-------------------------');
        }

        return parent::handleInvoicePaymentSucceeded($payload);
    }
}

Please or to participate in this conversation.