| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Core\Handlers; |
| 6 |
|
| 7 |
use Yatra\Repositories\BookingRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Booking Confirmation Page Handler |
| 11 |
* |
| 12 |
* Handles booking confirmation page requests |
| 13 |
*/ |
| 14 |
class BookingConfirmationPageHandler extends BasePageHandler |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Handle booking confirmation page request |
| 18 |
* |
| 19 |
* @param array $route_data Route data from RouteMatcher |
| 20 |
* @return bool True if handled successfully |
| 21 |
*/ |
| 22 |
public function handle(array $route_data): bool |
| 23 |
{ |
| 24 |
$confirmation_id = (string) ($route_data['confirmation_id'] ?? ''); |
| 25 |
|
| 26 |
$bookingRepo = new BookingRepository(); |
| 27 |
$booking = $bookingRepo->findByConfirmationSegment($confirmation_id); |
| 28 |
|
| 29 |
if (!$booking) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
// Configure $wp_query + virtual WP_Post so FSE block themes don't fall back to 404.html. |
| 34 |
$this->setupPageEnvironment('singular', [ |
| 35 |
'title' => __('Booking Confirmation', 'yatra'), |
| 36 |
// Keep the virtual post ID at 0 (like the account/login/booking |
| 37 |
// handlers). Using the booking row id made get_queried_object_id() |
| 38 |
// collide with a real wp_posts row of the same id, so SEO plugins / |
| 39 |
// WordPress emitted THAT page's title, description and OG tags on the |
| 40 |
// confirmation page. The booking is read from the `yatra_booking` |
| 41 |
// global, so no queried-object id is needed here. |
| 42 |
'object_id' => 0, |
| 43 |
'post_type' => 'page', |
| 44 |
'post_name' => $confirmation_id, |
| 45 |
]); |
| 46 |
|
| 47 |
// Set up global booking object |
| 48 |
$this->setGlobal('yatra_booking', $booking); |
| 49 |
|
| 50 |
// Set up query vars for backward compatibility |
| 51 |
$this->setQueryVars([ |
| 52 |
'yatra_booking_confirmation' => $confirmation_id, |
| 53 |
'yatra_booking' => $booking, |
| 54 |
]); |
| 55 |
|
| 56 |
// Ensure the payment gateways are registered before the template renders. |
| 57 |
// Gateways attach their confirmation-page hooks (e.g. Bank Transfer's |
| 58 |
// `yatra_booking_confirmation_after_details` renderer) in their |
| 59 |
// constructors, which only run once the registry is built. Without this, |
| 60 |
// the confirmation page fires the hook with no gateway listening, so the |
| 61 |
// bank-transfer account details never appear. |
| 62 |
if (class_exists('\\Yatra\\PaymentGateways\\PaymentGatewayRegistry')) { |
| 63 |
\Yatra\PaymentGateways\PaymentGatewayRegistry::getInstance(); |
| 64 |
} |
| 65 |
|
| 66 |
// Process a PayPal return before the template renders. PayPal redirects |
| 67 |
// the buyer back here with `?paypal=success` (a param only PayPal sets), |
| 68 |
// so this runs only on a genuine PayPal return and affects nothing else. |
| 69 |
// For Advanced mode it captures the approved order and confirms the |
| 70 |
// booking; for Simple mode it is a no-op (the IPN webhook confirms). |
| 71 |
// Idempotency is guaranteed by the gateway (paid-guard + transaction-id). |
| 72 |
if (isset($_GET['paypal']) && sanitize_key((string) $_GET['paypal']) === 'success' |
| 73 |
&& class_exists('\\Yatra\\PaymentGateways\\PaymentGatewayRegistry')) { |
| 74 |
$paypal = \Yatra\PaymentGateways\PaymentGatewayRegistry::getInstance()->get('paypal'); |
| 75 |
if ($paypal && method_exists($paypal, 'handlePaymentReturn')) { |
| 76 |
try { |
| 77 |
$paypal->handlePaymentReturn($booking, $bookingRepo); |
| 78 |
$reloaded = $bookingRepo->findByConfirmationSegment($confirmation_id); |
| 79 |
if ($reloaded) { |
| 80 |
$booking = $reloaded; |
| 81 |
$this->setGlobal('yatra_booking', $booking); |
| 82 |
$this->setQueryVars([ |
| 83 |
'yatra_booking_confirmation' => $confirmation_id, |
| 84 |
'yatra_booking' => $booking, |
| 85 |
]); |
| 86 |
} |
| 87 |
} catch (\Throwable $e) { |
| 88 |
// Best-effort: the page still renders; the webhook can reconcile. |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $this->selectTemplate('booking-confirmation', null, 'booking-confirmation'); |
| 94 |
} |
| 95 |
} |
| 96 |
|