getContent(); $signature = $request->header('Stripe-Signature'); $endpointSecret = env('STRIPE_WEBHOOK_SECRET'); try { $event = Webhook::constructEvent($payload, $signature, $endpointSecret); } catch (SignatureVerificationException $e) { Log::error('Stripe webhook signature verification failed: ' . $e->getMessage()); return response()->json(['error' => 'Invalid signature'], 400); } try { // Handle the event switch ($event->type) { case 'checkout.session.completed': $session = $event->data->object; $drawCount = $session->metadata->draw_count; $clientSessionId = $session->metadata->client_session_id; $payment = Payment::where('client_session_id', $clientSessionId)->first(); if ($payment) { $payment->update([ 'status' => 'succeeded', 'draw_count' => $drawCount, ]); } else { // Log if no matching payment record is found Log::warning('No pending payment record found for client_session_id: ' . $clientSessionId); } break; default: Log::info('Received a non-checkout.session.completed webhook event: ' . $event->type); break; } } catch (\Exception $e) { // Log any other unexpected errors Log::error('Stripe webhook processing error: ' . $e->getMessage(), ['exception' => $e]); return response()->json(['error' => 'Server error'], 500); } return response()->json(['status' => 'success'], 200); } }