Implementing booking synchronization with Google Calendar
Synchronizing bookings with Google Calendar allows masters and administrators to see schedules in a familiar tool, receive Google notifications, and manage time through Calendar interface. New bookings from the site automatically appear in the calendar.
Google Calendar API
Uses OAuth2 for authorization and Google Calendar API v3:
use Google\Client;
use Google\Service\Calendar;
class GoogleCalendarService
{
private Calendar $calendar;
public function __construct(string $accessToken)
{
$client = new Client();
$client->setAccessToken($accessToken);
$this->calendar = new Calendar($client);
}
public function createEvent(Booking $booking): string
{
$event = new Calendar\Event([
'summary' => "📅 {$booking->service->name} — {$booking->customer_name}",
'description' => $this->buildDescription($booking),
'start' => [
'dateTime' => $booking->starts_at->toRfc3339String(),
'timeZone' => 'Europe/Moscow',
],
'end' => [
'dateTime' => $booking->ends_at->toRfc3339String(),
'timeZone' => 'Europe/Moscow',
],
'attendees' => [
['email' => $booking->customer_email],
],
'reminders' => [
'useDefault' => false,
'overrides' => [
['method' => 'email', 'minutes' => 1440],
['method' => 'popup', 'minutes' => 60],
],
],
'extendedProperties' => [
'private' => ['booking_id' => (string) $booking->id],
],
]);
$created = $this->calendar->events->insert('primary', $event);
return $created->getId();
}
public function updateEvent(Booking $booking): void
{
$event = $this->calendar->events->get('primary', $booking->google_event_id);
$event->setStart(new Calendar\EventDateTime([
'dateTime' => $booking->starts_at->toRfc3339String(),
]));
$event->setEnd(new Calendar\EventDateTime([
'dateTime' => $booking->ends_at->toRfc3339String(),
]));
$this->calendar->events->update('primary', $booking->google_event_id, $event);
}
public function deleteEvent(string $eventId): void
{
$this->calendar->events->delete('primary', $eventId);
}
}
OAuth2 authorization for masters
Each master authenticates via Google and grants the app https://www.googleapis.com/auth/calendar.events rights:
Route::get('/integrations/google-calendar/connect', function () {
$client = new Google\Client();
$client->setScopes([\Google\Service\Calendar::CALENDAR_EVENTS]);
$client->setState(auth()->id());
return redirect($client->createAuthUrl());
});
Route::get('/integrations/google-calendar/callback', function (Request $request) {
$client = new Google\Client();
$tokens = $client->fetchAccessTokenWithAuthCode($request->code);
User::find($request->state)->update([
'google_calendar_token' => encrypt(json_encode($tokens)),
]);
return redirect('/settings/integrations')->with('success', 'Google Calendar connected');
});
Two-way synchronization via Webhook
Google Calendar Push Notifications allow receiving notifications of calendar changes:
// Subscribe to notifications
$this->calendar->events->watch('primary', new Calendar\Channel([
'id' => Str::uuid(),
'type' => 'web_hook',
'address' => 'https://example.com/webhooks/google-calendar',
]));
// Notification handler
Route::post('/webhooks/google-calendar', function (Request $request) {
$channelId = $request->header('X-Goog-Channel-ID');
SyncGoogleCalendarChanges::dispatch($channelId);
return response('ok');
});
Timeline
One-way synchronization (site → Google Calendar): 2–3 days. Two-way with webhook: 4–6 days.







