false, 'error' => 'staff_id must be the staff id of the chosen time from timetics-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 Timetics_Available_Slots_Tool( $this->settings, $this->gateway ) )->find_slots( [ 'meeting_id' => (int) $meeting_id, 'date_from' => (string) $date, 'staff_id' => (int) $staff_id, 'location' => $location, ], 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) $staff_id ) { $slot = $candidate; break; } } if ( null === $slot ) { return [ 'ok' => false, 'error' => 'That time is no longer free. Call timetics-available-slots again and offer other times.', ]; } $meeting = $found['meeting']; $timezone = $this->gateway->visitor_timezone(); $button = __( 'Continue booking', 'aisentic' ); $place = ''; foreach ( $meeting['locations'] as $row ) { if ( $row['index'] === $found['location'] ) { $place = $row['label']; } } $this->set_card( 'booking_link', [ 'url' => $this->url( $meeting['id'], (int) $staff_id, (string) $date, $start_time, $timezone, $found['location'] ), 'button_label' => $button, 'service' => $meeting['name'], 'staff' => $slot['staff_name'], 'location' => $place, 'date_label' => $this->date_label( (string) $date ), 'time_label' => $this->time_label( $start_time ) . ' (' . self::timezone_label( $timezone ) . ')', 'lines' => $this->lines( $meeting ), '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 timetics-booking-link again for this time. Tell the visitor to tap the button.', ]; } catch ( \Throwable $e ) { Log::error( 'Timetics 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.', ]; } } /** * Price lines: the meeting's one price, or each ticket a seat can be. * * @param array $meeting Shaped meeting. * @return array List of [ label, price, price_text ]. */ private function lines( array $meeting ): array { if ( count( $meeting['tickets'] ?? [] ) < 2 ) { // A meeting cached before tickets were read has none. return [ [ 'label' => $meeting['duration'], 'price' => $meeting['price'], 'price_text' => $this->gateway->price_text( (float) $meeting['price'] ), ], ]; } return array_map( fn( array $ticket ): array => [ /* translators: %s: ticket name, e.g. "Vip" */ 'label' => sprintf( __( '%s ticket', 'aisentic' ), $ticket['name'] ), 'price' => $ticket['price'], 'price_text' => $this->gateway->price_text( $ticket['price'] ), ], $meeting['tickets'] ); } /** * The pre-filled Timetics form link, or the plain meeting page on old Timetics. * * Never `id`, `meeting_id`, `action` or `booking_action`: Timetics reads * those as reschedule and cancel links. * * @param int $meeting_id Meeting id. * @param int $staff_id Staff id. * @param string $date Day. * @param string $time HH:MM in $timezone. * @param string $timezone Visitor timezone. * @param int|null $location Location index. * @return string */ private function url( int $meeting_id, int $staff_id, string $date, string $time, string $timezone, ?int $location ): string { $base = $this->gateway->meeting_url( $meeting_id ); if ( ! $this->gateway->supports_manage_links() ) { return $base; } $args = [ 'tt_prefill' => 1, 'tt_meeting' => $meeting_id, 'tt_staff' => $staff_id, 'tt_date' => $date, 'tt_time' => $time, 'tt_tz' => $timezone, ]; if ( null !== $location ) { $args['tt_location'] = $location; } // add_query_arg() does not encode values: "Europe/London" must stay one value. return esc_url_raw( add_query_arg( array_map( 'rawurlencode', array_map( 'strval', $args ) ), $base ) ); } }