WP_REST_Server::CREATABLE, 'callback' => [__CLASS__, 'handle'], // Booking customers are normally logged out, so this has to be public. // Authorisation is the per-booking token the provider verifies. 'permission_callback' => '__return_true', 'args' => [ 'bookingId' => [ 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', ], 'token' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], 'provider' => [ 'required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ], ], ] ); } public static function handle(WP_REST_Request $request) { $available = ATW_Bookly_Providers::available(); if (!$available) { return new WP_Error( 'atw_no_provider', __('No supported booking plugin is active.', 'addtowallet-for-bookly'), ['status' => 503] ); } $settings = ATW_Bookly_Settings::get(); if (empty($settings['api_key'])) { return new WP_Error('atw_not_configured', __('AddToWallet is not configured.', 'addtowallet-for-bookly'), ['status' => 503]); } $booking_id = absint($request->get_param('bookingId')); $token = (string) $request->get_param('token'); $wanted = (string) $request->get_param('provider'); // Prefer the provider the front-end names; fall back to trying each active one so // a stale cached script still works after another provider is added. $candidates = $available; if ($wanted && isset($available[$wanted])) { $candidates = [$wanted => $available[$wanted]]; } $booking = null; $provider = null; $refusal = null; foreach ($candidates as $candidate) { $result = $candidate->find_booking($booking_id, $token); if (is_wp_error($result)) { // Wrong token for a booking that does exist. Remember it, but let other // providers try in case ids collide across booking plugins. $refusal = $result; continue; } if (is_array($result)) { $booking = $result; $provider = $candidate; break; } } if (!$booking) { if ($refusal) { return $refusal; } return new WP_Error('atw_not_found', __('Booking not found.', 'addtowallet-for-bookly'), ['status' => 404]); } $key = $provider->id() . ':' . $booking['booking_id']; // Idempotent: the same booking always resolves to the same pass, so a refresh or // a duplicated interceptor cannot bill twice. $existing = ATW_Bookly_Pass_Store::get($key); if ($existing) { return new WP_REST_Response(['shareUrl' => $existing['share_url'], 'cached' => true], 200); } $allowed = (array) $settings['allowed_statuses']; if (!in_array($booking['status'], $allowed, true)) { // Not an error: the booking was accepted, it just is not in a state that earns // a pass. 200 with no URL keeps this out of the browser console. return new WP_REST_Response( ['shareUrl' => null, 'skipped' => true, 'status' => $booking['status']], 200 ); } $limited = self::check_rate_limit(); if (is_wp_error($limited)) { return $limited; } $payload = ATW_Bookly_Pass_Builder::build($booking, $settings); $result = ATW_Bookly_Api_Client::create_pass($payload, $settings); if (is_wp_error($result)) { // Kept for the Bookings & passes tab, so the admin can see why there is no pass. ATW_Bookly_Pass_Store::record_create_error($key, $result->get_error_message()); self::log('Pass creation failed for ' . $key . ': ' . $result->get_error_message()); return new WP_Error('atw_create_failed', $result->get_error_message(), ['status' => 502]); } ATW_Bookly_Pass_Store::save($key, $result, isset($payload['logoUrl']) ? $payload['logoUrl'] : '', ATW_Bookly_Pass_Store::fingerprint($payload)); /** * Fires once a pass has been created for a booking. * * @param string $share_url * @param array $booking * @param ATW_Bookly_Provider $provider */ do_action('atw_bookly_pass_created', $result['share_url'], $booking, $provider); return new WP_REST_Response(['shareUrl' => $result['share_url'], 'cached' => false], 200); } private static function check_rate_limit() { $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; if ($ip === '') { return true; } $key = 'atw_bookly_rl_' . md5($ip); $count = (int) get_transient($key); if ($count >= self::RATE_LIMIT) { return new WP_Error( 'atw_rate_limited', __('Too many pass requests. Try again later.', 'addtowallet-for-bookly'), ['status' => 429] ); } set_transient($key, $count + 1, self::RATE_WINDOW); return true; } private static function log($message) { if (defined('WP_DEBUG') && WP_DEBUG) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- only with WP_DEBUG on, for site owners diagnosing a failed pass. error_log('[addtowallet-for-bookly] ' . $message); } } }