| 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 |
* Resend just the customer payment-received email (part- or full-payment |
| 47 |
* template, exactly as the automated flow chooses it). Public entry for the |
| 48 |
* admin "resend email" action. |
| 49 |
*/ |
| 50 |
public static function resendCustomerPaymentEmail(array $paymentData): void |
| 51 |
{ |
| 52 |
self::notifyCustomerPaymentReceived($paymentData); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Resend just the admin payment-received notification. Public entry for the |
| 57 |
* admin "resend email" action. |
| 58 |
*/ |
| 59 |
public static function resendAdminPaymentEmail(array $paymentData): void |
| 60 |
{ |
| 61 |
self::notifyAdminPaymentReceived($paymentData); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Notify admin of new booking |
| 66 |
*/ |
| 67 |
private static function notifyAdminNewBooking(int $bookingId, $bookingData): void |
| 68 |
{ |
| 69 |
if (!apply_filters('yatra_notify_admin_new_booking', true, $bookingId, $bookingData)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 74 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$repository = new BookingRepository(); |
| 79 |
$booking = $repository->findWithTrip($bookingId); |
| 80 |
if (!$booking) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$adminNotifyContext = [ |
| 85 |
'booking_id' => $bookingId, |
| 86 |
'reference' => $booking->reference ?? '', |
| 87 |
'source' => 'yatra_booking_created', |
| 88 |
]; |
| 89 |
if (!apply_filters('yatra_send_checkout_admin_notification', true, $adminNotifyContext)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$variables = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 94 |
|
| 95 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 96 |
TransactionalEmailTemplateService::TYPE_ADMIN_NEW_BOOKING, |
| 97 |
$admin_email, |
| 98 |
$variables |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Notify admin of payment received (template: admin_payment_received). |
| 104 |
*/ |
| 105 |
private static function notifyAdminPaymentReceived(array $paymentData): void |
| 106 |
{ |
| 107 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 108 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$bookingRepository = new BookingRepository(); |
| 113 |
$booking = $bookingRepository->findWithTrip((int) ($paymentData['booking_id'] ?? 0)); |
| 114 |
if (!$booking) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$vars = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 119 |
$vars['payment_amount_formatted'] = yatra_format_price((float) ($paymentData['amount'] ?? 0)); |
| 120 |
$vars['payment_method'] = (string) ($paymentData['payment_method'] ?? __('Online payment', 'yatra')); |
| 121 |
$vars['transaction_id'] = (string) ($paymentData['transaction_id'] ?? ''); |
| 122 |
|
| 123 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 124 |
TransactionalEmailTemplateService::TYPE_ADMIN_PAYMENT_RECEIVED, |
| 125 |
$admin_email, |
| 126 |
$vars |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Which payment email to send: the part-payment template or the full one. |
| 132 |
* |
| 133 |
* Defaults to the long-standing single template. The part-payment template |
| 134 |
* is used only when ALL of these hold, so an existing site sees no change |
| 135 |
* until its operator opts in: |
| 136 |
* |
| 137 |
* 1. a balance is genuinely still outstanding on the booking, |
| 138 |
* 2. deposits / partial payments are switched on at all, and |
| 139 |
* 3. the operator has enabled the separate part-payment template. |
| 140 |
*/ |
| 141 |
private static function paymentTemplateTypeFor(object $booking): string |
| 142 |
{ |
| 143 |
$full = TransactionalEmailTemplateService::TYPE_PAYMENT_CONFIRMATION; |
| 144 |
|
| 145 |
// Nothing left to pay → this is the full-payment confirmation. |
| 146 |
if ((float) ($booking->amount_due ?? 0) <= 0) { |
| 147 |
return $full; |
| 148 |
} |
| 149 |
|
| 150 |
if (!yatra_partial_payments_enabled()) { |
| 151 |
return $full; |
| 152 |
} |
| 153 |
|
| 154 |
if (!SettingsService::isEnabled('email_template_partial_payment')) { |
| 155 |
return $full; |
| 156 |
} |
| 157 |
|
| 158 |
return TransactionalEmailTemplateService::TYPE_PARTIAL_PAYMENT_RECEIVED; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Notify customer of payment received |
| 163 |
*/ |
| 164 |
private static function notifyCustomerPaymentReceived(array $paymentData): void |
| 165 |
{ |
| 166 |
$bookingRepository = new BookingRepository(); |
| 167 |
$booking = $bookingRepository->findWithTrip((int) ($paymentData['booking_id'] ?? 0)); |
| 168 |
|
| 169 |
if (!$booking || empty($booking->contact_email)) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$vars = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 174 |
$vars['payment_amount_formatted'] = yatra_format_price((float) ($paymentData['amount'] ?? 0)); |
| 175 |
$vars['payment_method'] = (string) ($paymentData['payment_method'] ?? __('Online payment', 'yatra')); |
| 176 |
$vars['transaction_id'] = (string) ($paymentData['transaction_id'] ?? ''); |
| 177 |
|
| 178 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 179 |
self::paymentTemplateTypeFor($booking), |
| 180 |
$booking->contact_email, |
| 181 |
$vars |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Notify admin of cancellation |
| 187 |
*/ |
| 188 |
private static function notifyAdminCancellation(int $bookingId, $bookingData): void |
| 189 |
{ |
| 190 |
$admin_email = SettingsService::getString('admin_email', (string) get_option('admin_email', '')); |
| 191 |
if ($admin_email === '' || !is_email($admin_email)) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
$repository = new BookingRepository(); |
| 196 |
$booking = $repository->findWithTrip($bookingId); |
| 197 |
if (!$booking) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$variables = TransactionalEmailTemplateService::variablesFromBooking($booking); |
| 202 |
|
| 203 |
TransactionalEmailTemplateService::sendIfEnabled( |
| 204 |
TransactionalEmailTemplateService::TYPE_ADMIN_BOOKING_CANCELLED, |
| 205 |
$admin_email, |
| 206 |
$variables |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
|