[英]Laravel auth user on webhook
I am having trouble with authenticated user in my Laravel/Vue app.我在我的 Laravel/Vue 应用程序中遇到了经过身份验证的用户的问题。 Once you log in, you can choose to make a purchase via Stripe which leads you off the page, and returns back upon payment.
登录后,您可以选择通过 Stripe 进行购买,这会引导您离开页面,并在付款后返回。
Just to make sure, I've made an endpoint:为了确保,我做了一个端点:
Route::get('test', function(){
return Auth::user();
});
And before and after Stripe, when I hit it, I do get back the user.在 Stripe 之前和之后,当我点击它时,我确实找回了用户。 So authentication is in order.
因此,身份验证是有序的。
What happens though is that Stripe upon payment event makes a webhook callback to my route:但发生的情况是,支付事件时的 Stripe 会对我的路由进行 webhook 回调:
Route::post('api/stripe/checkout-session-completed', 'StripeController@checkoutSessionCompleted');
Inside a hook, event is fired which should propagate number of credits purchased to the user who made the purchase, however I am always getting that Auth::user()
is not defined.在钩子内部,触发了事件,该事件应该将购买的积分数传播给进行购买的用户,但是我总是得到未定义的
Auth::user()
。
use Illuminate\Support\Facades\Auth;
...
public function checkoutSessionCompleted()
{
...
$this->handleCheckout($session); // this is Stripe session object
...
}
private function handleCheckout($session)
{
...
event(new PaymentSuccessful($payment, Auth::user()));
...
}
Was this supposed to happen?这应该发生吗? How can I get the currently auth user if not like this?
如果不是这样,我怎样才能获得当前的身份验证用户?
Looks like sessions aren't shared when external source makes a POST request to your route.当外部来源向您的路线发出 POST 请求时,似乎不会共享会话。 I made a workaround to include user ID within Stripe session metadata, so I can find user by that same ID when request returns via webhook.
我做了一个解决方法,在 Stripe session 元数据中包含用户 ID,因此当请求通过 webhook 返回时,我可以通过相同的 ID 找到用户。
$stripeSession = Session::create([
'success_url' => route('stripe.success') . '/?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => route('stripe.cancel'),
'payment_method_types' => ['card'],
'mode' => 'payment',
'line_items' => [
[
'price_data' => [
'currency' => 'eur',
'product' => env('STRIPE_PRODUCT_ID'),
'unit_amount' => $price->stripe_price * 100,
],
'description' => "Credits to receive: $price->quantity",
'quantity' => 1,
],
],
'metadata' => [
'quantity' => $price->quantity,
'user_id' => Auth::user()->id,
],
'customer_email' => optional(Auth::user())->email ?? null,
'client_reference_id' => optional(Auth::user())->id ?? null,
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.