| 1 |
<?php |
| 2 |
/** |
| 3 |
* Notification Service |
| 4 |
* |
| 5 |
* Admin / customer emails for booking lifecycle events. Enable/disable and copy are controlled |
| 6 |
* under Yatra → Email → Templates (settings-backed flags), not Settings → Notifications. |
| 7 |
* |
| 8 |
* @package Yatra\Services |
| 9 |
* @since 3.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Yatra\Services; |
| 15 |
|
| 16 |
use Yatra\Repositories\BookingRepository; |
| 17 |
|
| 18 |
class NotificationService |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Send booking created notification (admin: {@see TransactionalEmailTemplateService::TYPE_ADMIN_NEW_BOOKING}). |
| 22 |
*/ |
| 23 |
public static function sendBookingCreatedNotification(int $bookingId, $bookingData): void |
| 24 |
{ |
| 25 |
self::notifyAdminNewBooking($bookingId, $bookingData); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Send payment completed notification (admin + customer templates). |
| 30 |
*/ |
| 31 |
public static function sendPaymentCompletedNotification(array $paymentData): void |
| 32 |
{ |
| 33 |
self::notifyAdminPaymentReceived($paymentData); |
| 34 |
self::notifyCustomerPaymentReceived($paymentData); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Send booking cancellation notification (admin template; customer email uses transactional templates from BookingService). |
| 39 |
*/ |
| 40 |
public static function sendCancellationNotification(int $bookingId, $bookingData): void |
| 41 |
{ |
| 42 |
self::notifyAdminCancellation($bookingId, $bookingData); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Notify admin of new booking |
| 47 |
*/ |
| 48 |
private static function notifyAdminNewBooking(int $bookingId, $bookingData): void |
| 49 |
{ |
| 50 |
if (!apply_filters('yatra_notify_admin_new_booking', true, $bookingId, $bookingData)) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 55 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$repository = new BookingRepository(); |
| 60 |
$booking = $repository->findWithTrip($bookingId); |
| 61 |
if (!$booking) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$adminNotifyContext = [ |
| 66 |
'booking_id' => $bookingId, |
| 67 |
'reference' => $booking->reference ?? '', |
| 68 |
'source' => 'yatra_booking_created', |
| 69 |
]; |
| 70 |
if (!apply_filters('yatra_send_checkout_admin_notification', true, $adminNotifyContext)) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$variables = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 75 |
|
| 76 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 77 |
TransactionalEmailTemplateService::TYPE_ADMIN_NEW_BOOKING, |
| 78 |
$admin_email, |
| 79 |
$variables |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Notify admin of payment received (template: admin_payment_received). |
| 85 |
*/ |
| 86 |
private static function notifyAdminPaymentReceived(array $paymentData): void |
| 87 |
{ |
| 88 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 89 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$bookingRepository = new BookingRepository(); |
| 94 |
$booking = $bookingRepository->findWithTrip((int) ($paymentData['booking_id'] ?? 0)); |
| 95 |
if (!$booking) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$vars = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 100 |
$vars['payment_amount_formatted'] = yatra_format_price((float) ($paymentData['amount'] ?? 0)); |
| 101 |
$vars['payment_method'] = (string) ($paymentData['payment_method'] ?? __('Online payment', 'yatra')); |
| 102 |
$vars['transaction_id'] = (string) ($paymentData['transaction_id'] ?? ''); |
| 103 |
|
| 104 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 105 |
TransactionalEmailTemplateService::TYPE_ADMIN_PAYMENT_RECEIVED, |
| 106 |
$admin_email, |
| 107 |
$vars |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Notify customer of payment received |
| 113 |
*/ |
| 114 |
private static function notifyCustomerPaymentReceived(array $paymentData): void |
| 115 |
{ |
| 116 |
$bookingRepository = new BookingRepository(); |
| 117 |
$booking = $bookingRepository->findWithTrip((int) ($paymentData['booking_id'] ?? 0)); |
| 118 |
|
| 119 |
if (!$booking || empty($booking->contact_email)) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$vars = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 124 |
$vars['payment_amount_formatted'] = yatra_format_price((float) ($paymentData['amount'] ?? 0)); |
| 125 |
$vars['payment_method'] = (string) ($paymentData['payment_method'] ?? __('Online payment', 'yatra')); |
| 126 |
$vars['transaction_id'] = (string) ($paymentData['transaction_id'] ?? ''); |
| 127 |
|
| 128 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 129 |
TransactionalEmailTemplateService::TYPE_PAYMENT_CONFIRMATION, |
| 130 |
$booking->contact_email, |
| 131 |
$vars |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Notify admin of cancellation |
| 137 |
*/ |
| 138 |
private static function notifyAdminCancellation(int $bookingId, $bookingData): void |
| 139 |
{ |
| 140 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 141 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$repository = new BookingRepository(); |
| 146 |
$booking = $repository->findWithTrip($bookingId); |
| 147 |
if (!$booking) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$variables = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 152 |
|
| 153 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 154 |
TransactionalEmailTemplateService::TYPE_ADMIN_BOOKING_CANCELLED, |
| 155 |
$admin_email, |
| 156 |
$variables |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
|