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);
}
}
@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);
}
}