false, 'error' => 'team_member_id must be the staff id of the chosen time from booktics-available-slots.', ]; } if ( 1 !== preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $start_time ) ) { return [ 'ok' => false, 'error' => 'start_time must be HH:MM in 24 hour time, for example 15:00.', ]; } try { $found = ( new Booktics_Available_Slots_Tool( $this->settings, $this->gateway ) )->find_slots( [ 'service_id' => (int) $service_id, 'date_from' => (string) $date, 'duration_option_id' => (string) $duration_option_id, 'team_member_id' => (int) $team_member_id, 'extras' => (string) $extras, 'attendees' => (int) $attendees, 'location_id' => (int) $location_id, ], true ); if ( empty( $found['ok'] ) ) { return $found; } $slot = null; foreach ( $found['days'][ (string) $date ] ?? [] as $candidate ) { if ( $candidate['start'] === $start_time && $candidate['staff_id'] === (int) $team_member_id ) { $slot = $candidate; break; } } if ( null === $slot ) { return [ 'ok' => false, 'error' => 'That time is no longer free. Call booktics-available-slots again and offer other times.', ]; } $service = $found['service']; $extra_pairs = Booktics_Available_Slots_Tool::parse_extras( (string) $extras ); $button = __( 'Continue booking', 'aisentic' ); $this->set_card( 'booking_link', [ 'url' => $this->url( $service['id'], (int) $team_member_id, (string) $date, $start_time, (string) $duration_option_id, $extra_pairs, (int) $attendees, (int) $location_id ), 'button_label' => $button, 'service' => $service['title'], 'staff' => $slot['staff_name'], 'location' => $this->location_name( (int) $location_id ), 'date_label' => $this->date_label( (string) $date ), 'time_label' => $this->time_label( $start_time ) . ' (' . $this->timezone_label() . ')', 'lines' => $this->lines( $service, (string) $duration_option_id, $extra_pairs ), 'currency_symbol' => $this->gateway->currency_symbol(), 'total_note' => __( 'Final total at checkout', 'aisentic' ), ] ); return [ 'ok' => true, 'button_label' => $button, // Without this the model kept calling the tool after a success, // then told the visitor something went wrong. 'shown' => 'The booking card with the button is on screen now. Do not call booktics-booking-link again for this time. Tell the visitor to tap the button.', ]; } catch ( \Throwable $e ) { Log::error( 'Booktics booking link failed: ' . $e->getMessage() ); return [ 'ok' => false, 'error' => 'The booking check failed. Tell the visitor to open the booking page and pick the time there.', ]; } } /** * The pre-filled Booktics form link, or the plain service page on old Booktics. * * @param int $service_id Service id. * @param int $team_id Staff id. * @param string $date Day. * @param string $time HH:MM. * @param string $duration Duration id. * @param array $extras [ id => qty ]. * @param int $attendees People. * @param int $location Branch id. * @return string */ private function url( int $service_id, int $team_id, string $date, string $time, string $duration, array $extras, int $attendees, int $location ): string { $base = $this->gateway->service_url( $service_id ); if ( ! $this->gateway->supports_manage_links() ) { return $base; } $pairs = []; foreach ( $extras as $id => $qty ) { $pairs[] = $id . ':' . $qty; } $args = [ 'bt_prefill' => 1, 'bt_service' => $service_id, 'bt_team' => $team_id, 'bt_date' => $date, 'bt_time' => $time, ]; $optional = [ 'bt_duration' => $duration, 'bt_extras' => implode( ',', $pairs ), 'bt_attendees' => $attendees > 1 ? $attendees : '', 'bt_location' => $location > 0 ? $location : '', ]; // add_query_arg() does not encode values: a duration id holding "&" // would split the link. $query = array_map( 'rawurlencode', array_map( 'strval', array_merge( $args, array_filter( $optional, static fn( $v ): bool => '' !== $v ) ) ) ); return esc_url_raw( add_query_arg( $query, $base ) ); } /** * Price lines: the duration, then each extra times its quantity. * * @param array $service Service. * @param string $duration Duration id ('' for the first one). * @param array $extras [ id => qty ]. * @return array [ [ label, price ] ] */ private function lines( array $service, string $duration, array $extras ): array { $chosen = $service['durations'][0] ?? null; foreach ( $service['durations'] as $row ) { if ( $row['id'] === $duration ) { $chosen = $row; } } $lines = []; if ( null !== $chosen ) { $lines[] = [ 'label' => '' !== $chosen['name'] ? $chosen['name'] : $chosen['minutes'] . ' min', 'price' => $chosen['price'], 'price_text' => $this->gateway->price_text( (float) $chosen['price'] ), ]; } foreach ( $this->gateway->extras( $service['id'] ) as $extra ) { $qty = $extras[ $extra['id'] ] ?? 0; if ( $qty > 0 ) { $lines[] = [ 'label' => $extra['name'] . ' x ' . $qty, 'price' => $extra['price'] * $qty, 'price_text' => $this->gateway->price_text( (float) ( $extra['price'] * $qty ) ), ]; } } return $lines; } /** * Branch name, or '' when none was chosen. * * @param int $location_id Branch id. * @return string */ private function location_name( int $location_id ): string { foreach ( $location_id > 0 ? $this->gateway->locations() : [] as $location ) { if ( $location['id'] === $location_id ) { return $location['name']; } } return ''; } }