We have set up a Jet Engine form, which calls our web hook when the user submits the form. The endpoint tries to make a booking, and tries to redirect the user to a success or failure page. I've tried using wp_redirect and returning a WP_REST_Response, but in both cases the form just closes (the endpoint is being called and does it's thing successfully, apart from redirecting). How do I redirect the user to another page in an endpoint that was called from a Jet Engine form?
Reason why we can't use the redirect action of Jet Engine Forms is because we need to use the result of the web hook to know where to redirect the user to, and Jet Engine doesn't allow to store the result of a web hook call as far as we can see.
From my limited understanding, this is because returning a redirect from an Ajax context returns the redirect to the JavaScript client that made the request, not directly to the browser of the user.
#[NoReturn] function endpoint_register_reservation( WP_REST_Request $request ): WP_REST_Response {
$event_id = $request->get_param( 'event_id' );
$child_id = $request->get_param( 'child_id' );
$bookings = new reservations_manager();
$error = $bookings->create_reservation( $child_id, $event_id, false, false );
$booking_request_success = 'https://[redacted]/buchungswunsch-erhalten/';
$booking_request_failed = 'https://[redacted]/buchungswunsch-nicht-akzeptiert/';
if ( is_wp_error( $error ) ) {
$booking_request_failed = add_query_arg( 'error_code', $error->get_error_code(), $booking_request_failed );
return new WP_REST_Response( array( 'redirect' => $booking_request_failed ), 200 );
}
return new WP_REST_Response( array( 'redirect' => $booking_request_success ), 200 );
}