From 27af3d58b9f2179f9f6620a34a4b2049557cc16c Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 24 Oct 2024 11:28:24 +0200 Subject: [PATCH] fix(Seating) do not clear sessions for non-complete orders This updates the logic used to confirm attendee reservations and clear the current seat selection session to make sure the session will not be cleared when the order is not complete (e.g. a pending one). If the session is incorrectly cleared, then the frontend timer will not find the session information and will expire sending the user a time-out message and a redirection dialog. This will happen if the user cannot complete the payment correctly after a failure (that would generate attendees and a pending order) in the grace time. Avoiding the session clear in those instances of failed payment will allow the user to try and complete the payment beyond the grace period, provided they still have time to do it. --- src/Tickets/Seating/Frontend/Session.php | 11 +++++-- src/Tickets/Seating/Orders/Controller.php | 39 ++++++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Tickets/Seating/Frontend/Session.php b/src/Tickets/Seating/Frontend/Session.php index 8bb54a1872..5f5a173d66 100644 --- a/src/Tickets/Seating/Frontend/Session.php +++ b/src/Tickets/Seating/Frontend/Session.php @@ -322,9 +322,11 @@ public function get_session_token_object_id(): ?array { * * @since TBD * + * @param bool $delete_token_session Whether to delete the token session after confirming the reservations. + * * @return bool Whether the reservations were confirmed or not. */ - public function confirm_all_reservations(): bool { + public function confirm_all_reservations(bool $delete_token_session = true): bool { $confirmed = true; foreach ( $this->get_entries() as $post_id => $token ) { @@ -334,8 +336,11 @@ public function confirm_all_reservations(): bool { continue; } - $confirmed &= $this->reservations->confirm( $post_id, $reservation_uuids ) - && $this->sessions->delete_token_session( $token ); + $confirmed = $this->reservations->confirm( $post_id, $reservation_uuids ); + + if ( $confirmed && $delete_token_session ) { + $confirmed &= $this->sessions->delete_token_session( $token ); + } } return $confirmed; diff --git a/src/Tickets/Seating/Orders/Controller.php b/src/Tickets/Seating/Orders/Controller.php index 1c28bec686..db73dc9a40 100644 --- a/src/Tickets/Seating/Orders/Controller.php +++ b/src/Tickets/Seating/Orders/Controller.php @@ -14,6 +14,7 @@ use TEC\Common\StellarWP\Assets\Asset; use TEC\Common\StellarWP\DB\DB; use TEC\Tickets\Admin\Attendees\Page as Attendee_Page; +use TEC\Tickets\Commerce\Order; use TEC\Tickets\Commerce\Shortcodes\Checkout_Shortcode; use TEC\Tickets\Seating\Admin\Ajax; use TEC\Tickets\Seating\Ajax_Methods; @@ -31,6 +32,7 @@ use Tribe__Tickets__Tickets as Tickets; use WP_Post; use WP_Query; +use TEC\Tickets\Commerce\Status\Status_Interface; /** * Class Controller @@ -166,7 +168,11 @@ protected function do_register(): void { // Attendee delete handler. add_filter( 'tec_tickets_commerce_attendee_to_delete', [ $this, 'handle_attendee_delete' ] ); - add_action( 'tec_tickets_commerce_flag_action_generated_attendees', [ $this, 'confirm_all_reservations' ] ); + add_action( 'tec_tickets_commerce_flag_action_generated_attendees', [ $this, 'confirm_all_reservations' ], 10, 4 ); + add_action( + 'tec_tickets_commerce_order_status_flag_complete', + [ $this, 'confirm_all_reservations_on_completion' ] + ); add_action( 'wp_ajax_' . Ajax::ACTION_FETCH_ATTENDEES, [ $this, 'fetch_attendees_by_post' ] ); add_action( 'wp_ajax_' . Ajax::ACTION_RESERVATION_CREATED, [ $this, 'update_reservation' ] ); add_action( 'wp_ajax_' . Ajax::ACTION_RESERVATION_UPDATED, [ $this, 'update_reservation' ] ); @@ -247,6 +253,10 @@ public function unregister(): void { remove_filter( 'post_row_actions', [ $this, 'add_seats_row_action' ] ); remove_action( 'tec_tickets_commerce_flag_action_generated_attendees', [ $this, 'confirm_all_reservations' ] ); + remove_action( + 'tec_tickets_commerce_order_status_flag_complete', + [ $this, 'confirm_all_reservations_on_completion' ] + ); remove_action( 'wp_ajax_' . Ajax::ACTION_FETCH_ATTENDEES, [ $this, 'fetch_attendees_by_post' ] ); remove_action( 'wp_ajax_' . Ajax::ACTION_RESERVATION_CREATED, [ $this, 'update_reservation' ] ); remove_action( 'wp_ajax_' . Ajax::ACTION_RESERVATION_UPDATED, [ $this, 'update_reservation' ] ); @@ -346,7 +356,7 @@ public function register_seat_tab( $tabbed_view, $post ) { if ( ! $post ) { return; } - + if ( ! tec_tickets_seating_enabled( $post->ID ) ) { return; } @@ -455,14 +465,22 @@ public function remove_move_row_action( $default_row_actions, $item ) { } /** - * Confirms all the reservations contained in the Session cookie. + * Confirms all the reservations contained in the Session cookie on generation of an Attendee. + * If the order status is complete, it will also delete the token session. * * @since TBD * + * @param array $attendees The generated attendees, unused. + * @param \Tribe__Tickets__Tickets $ticket The ticket the attendee is generated for, unused. + * @param \WP_Post $order The order the attendee is generated for, unused. + * @param Status_Interface $new_status New post status. + * * @return void */ - public function confirm_all_reservations(): void { - $this->session->confirm_all_reservations(); + public function confirm_all_reservations($attendees, $ticket, $order, $new_status): void { + $incomplete_flags = array_intersect( $new_status->get_flags(), [ 'incomplete', 'count_incomplete' ] ); + $delete_session = count( $incomplete_flags ) === 0; + $this->session->confirm_all_reservations( $delete_session ); } /** @@ -786,4 +804,15 @@ public function update_reservation(): void { public function add_seats_row_action( array $actions, $post ): array { return $this->seats_report->add_seats_row_action( $actions, $post ); } + + /** + * On completion of a TC Order, confirm all the reservations and clear the session. + * + * @since TBD + * + * @return void + */ + public function confirm_all_reservations_on_completion(): void { + $this->session->confirm_all_reservations(); + } }