Реалізація синхронізації бронювань з Google Calendar
Синхронізація бронювань з Google Calendar дозволяє майстрам та адміністраторам бачити розклад у звичному інструменті, отримувати сповіщення від Google та керувати часом через інтерфейс Calendar. Нові бронювання з сайту автоматично з'являються в календарі.
Google Calendar API
Використовується OAuth2 для авторизації та 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 авторизація для майстрів
Кожен майстер авторизується через Google та дає додатку права https://www.googleapis.com/auth/calendar.events:
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 підключено');
});
Двостороння синхронізація через Webhook
Google Calendar Push Notifications дозволяють отримувати сповіщення про зміни в календарі:
// Підписка на сповіщення
$this->calendar->events->watch('primary', new Calendar\Channel([
'id' => Str::uuid(),
'type' => 'web_hook',
'address' => 'https://example.com/webhooks/google-calendar',
]));
// Обробник сповіщень
Route::post('/webhooks/google-calendar', function (Request $request) {
$channelId = $request->header('X-Goog-Channel-ID');
SyncGoogleCalendarChanges::dispatch($channelId);
return response('ok');
});
Терміни
Одностороння синхронізація (сайт → Google Calendar): 2–3 дні. Двостороння з webhook: 4–6 днів.







