| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notification Hooks |
| 4 |
* |
| 5 |
* Registers hooks for notification system |
| 6 |
* |
| 7 |
* @package Yatra\Hooks |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace Yatra\Hooks; |
| 14 |
|
| 15 |
use Yatra\Services\NotificationService; |
| 16 |
|
| 17 |
class NotificationHooks |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Initialize notification hooks |
| 21 |
*/ |
| 22 |
public static function init(): void |
| 23 |
{ |
| 24 |
// Booking notifications |
| 25 |
add_action('yatra_booking_created', [self::class, 'onBookingCreated'], 10, 2); |
| 26 |
add_action('yatra_payment_completed', [self::class, 'onPaymentCompleted'], 10, 4); |
| 27 |
add_action('yatra_booking_status_changed', [self::class, 'onBookingStatusChanged'], 10, 3); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Handle booking created event |
| 32 |
*/ |
| 33 |
public static function onBookingCreated(int $bookingId, $bookingData): void |
| 34 |
{ |
| 35 |
NotificationService::sendBookingCreatedNotification($bookingId, $bookingData); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Handle payment completed event |
| 40 |
*/ |
| 41 |
public static function onPaymentCompleted(...$args): void |
| 42 |
{ |
| 43 |
$paymentData = []; |
| 44 |
|
| 45 |
if (!empty($args)) { |
| 46 |
// Format A: array payload |
| 47 |
if (is_array($args[0])) { |
| 48 |
$paymentData = $args[0]; |
| 49 |
} |
| 50 |
// Format B: multiple args with array at end |
| 51 |
elseif (isset($args[3]) && is_array($args[3])) { |
| 52 |
$paymentData = $args[3]; |
| 53 |
if (!empty($args[0]) && is_numeric($args[0])) { |
| 54 |
$paymentData['booking_id'] = $paymentData['booking_id'] ?? (int) $args[0]; |
| 55 |
} |
| 56 |
if (!empty($args[1]) && is_string($args[1])) { |
| 57 |
$paymentData['payment_method'] = $paymentData['payment_method'] ?? $args[1]; |
| 58 |
} |
| 59 |
if (!empty($args[2])) { |
| 60 |
$paymentData['transaction_id'] = $paymentData['transaction_id'] ?? $args[2]; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// A manually recorded payment (PaymentService) fires this same action |
| 66 |
// with `send_emails` => false when an operator opted out via the |
| 67 |
// `yatra_send_manual_payment_emails` filter; the event still reaches |
| 68 |
// every other listener (automations, webhooks, …). |
| 69 |
if (isset($paymentData['send_emails']) && !$paymentData['send_emails']) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (!empty($paymentData)) { |
| 74 |
NotificationService::sendPaymentCompletedNotification($paymentData); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Handle booking status changed event |
| 80 |
*/ |
| 81 |
public static function onBookingStatusChanged(int $bookingId, string $oldStatus, string $newStatus): void |
| 82 |
{ |
| 83 |
// Send cancellation notification if status changed to cancelled |
| 84 |
if ($newStatus === 'cancelled') { |
| 85 |
$bookingRepository = new \Yatra\Repositories\BookingRepository(); |
| 86 |
$booking = $bookingRepository->find($bookingId); |
| 87 |
|
| 88 |
if ($booking) { |
| 89 |
NotificationService::sendCancellationNotification($bookingId, (array) $booking); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|