| @@ -45,8 +45,14 @@ | ||
| 45 | 45 | $result = $subscriptionService->queueActions($startedAt, $maxRuntime); |
| 46 | 46 | $stats['renewal_queued'] = $result['renewal']; |
| 47 | 47 | $stats['trial_queued'] = $result['trial']; |
| 48 | 48 | } |
| 49 | + | |
| 50 | + $renewalReminderService = new RenewalReminderService(); | |
| 51 | + | |
| 52 | + if ($renewalReminderService->isEnabled()) { | |
| 53 | + $stats['renewal_order_queued'] = $renewalReminderService->queueActions($startedAt, $maxRuntime); | |
| 54 | + } | |
| 49 | 55 | } catch (\Throwable $e) { |
| 50 | 56 | fluent_cart_error_log('Reminder hourly scan error', $e->getMessage()); |
| 51 | 57 | } |
| 52 | 58 | |
| @@ -98,15 +104,9 @@ | ||
| 98 | 104 | * Check if a payment reminder can be sent for an order (used by detail API). |
| 99 | 105 | */ |
| 100 | 106 | public function canSendPaymentReminder(array $orderData): bool |
| 101 | 107 | { |
| 102 | - $eligibleStatuses = [ | |
| 103 | - Status::PAYMENT_PENDING, | |
| 104 | - Status::PAYMENT_PARTIALLY_PAID, | |
| 105 | - Status::PAYMENT_FAILED | |
| 106 | - ]; | |
| 107 | - | |
| 108 | - if (!in_array(Arr::get($orderData, 'payment_status'), $eligibleStatuses, true)) { | |
| 108 | + if (!in_array(Arr::get($orderData, 'payment_status'), RenewalReminderService::getReminderPaymentStatuses(), true)) { | |
| 109 | 109 | return false; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | return max((int)Arr::get($orderData, 'total_amount', 0) - (int)Arr::get($orderData, 'total_paid', 0), 0) > 0; |
| @@ -128,18 +128,35 @@ | ||
| 128 | 128 | |
| 129 | 129 | /** |
| 130 | 130 | * Send a manual reminder for a specific order or subscription. |
| 131 | 131 | * |
| 132 | - * @param string $event The reminder event hook (invoice_reminder_overdue, subscription_renewal_reminder, subscription_trial_end_reminder) | |
| 132 | + * @param string $event The reminder event hook (renewal_reminder_overdue, subscription_renewal_reminder, subscription_trial_end_reminder) | |
| 133 | 133 | * @param int $entityId The order ID or subscription ID |
| 134 | 134 | * @return array{success: bool, message: string} |
| 135 | 135 | */ |
| 136 | 136 | public function sendManualReminder(string $event, int $entityId): array |
| 137 | 137 | { |
| 138 | + if (!$this->isRemindersEnabled()) { | |
| 139 | + return [ | |
| 140 | + 'success' => false, | |
| 141 | + 'message' => __('Reminders are disabled for this store.', 'fluent-cart'), | |
| 142 | + ]; | |
| 143 | + } | |
| 144 | + | |
| 145 | + // renewal_reminder_overdue has no active/inactive toggle of its own | |
| 146 | + // (manage_toggle: 'no', on-demand) — only the scheduled renewal/trial | |
| 147 | + // reminders have a real per-event switch worth checking. | |
| 148 | + if ($event !== 'renewal_reminder_overdue' && !$this->isNotificationEnabled($event)) { | |
| 149 | + return [ | |
| 150 | + 'success' => false, | |
| 151 | + 'message' => __('This reminder notification is currently disabled.', 'fluent-cart'), | |
| 152 | + ]; | |
| 153 | + } | |
| 154 | + | |
| 138 | 155 | try { |
| 139 | 156 | switch ($event) { |
| 140 | - case 'invoice_reminder_overdue': | |
| 141 | - return $this->sendManualInvoiceReminder($entityId); | |
| 157 | + case 'renewal_reminder_overdue': | |
| 158 | + return $this->sendRenewalOrderReminder($entityId); | |
| 142 | 159 | case 'subscription_renewal_reminder': |
| 143 | 160 | return $this->sendManualRenewalReminder($entityId); |
| 144 | 161 | case 'subscription_trial_end_reminder': |
| 145 | 162 | return $this->sendManualTrialReminder($entityId); |
| @@ -157,9 +174,9 @@ | ||
| 157 | 174 | ]; |
| 158 | 175 | } |
| 159 | 176 | } |
| 160 | 177 | |
| 161 | - protected function sendManualInvoiceReminder(int $orderId): array | |
| 178 | + protected function sendRenewalOrderReminder(int $orderId): array | |
| 162 | 179 | { |
| 163 | 180 | $order = Order::query()->with(['customer'])->find($orderId); |
| 164 | 181 | |
| 165 | 182 | if (!$order || !$order->customer) { |
| @@ -165,16 +182,9 @@ | ||
| 165 | 182 | if (!$order || !$order->customer) { |
| 166 | 183 | return ['success' => false, 'message' => __('Order or customer not found', 'fluent-cart')]; |
| 167 | 184 | } |
| 168 | 185 | |
| 169 | - $eligibleStatuses = [ | |
| 170 | - Status::PAYMENT_PENDING, | |
| 171 | - Status::PAYMENT_PARTIALLY_PAID, | |
| 172 | - Status::PAYMENT_FAILED, | |
| 173 | - Status::PAYMENT_AUTHORIZED, | |
| 174 | - ]; | |
| 175 | - | |
| 176 | - if (!in_array($order->payment_status, $eligibleStatuses, true)) { | |
| 186 | + if (!in_array($order->payment_status, RenewalReminderService::getReminderPaymentStatuses(), true)) { | |
| 177 | 187 | return ['success' => false, 'message' => __('Order is not eligible for payment reminder', 'fluent-cart')]; |
| 178 | 188 | } |
| 179 | 189 | |
| 180 | 190 | $outstanding = max((int)$order->total_amount - (int)$order->total_paid, 0); |
| @@ -187,9 +197,9 @@ | ||
| 187 | 197 | if (!$base || $base <= 0) { |
| 188 | 198 | return ['success' => false, 'message' => __('Invalid order date', 'fluent-cart')]; |
| 189 | 199 | } |
| 190 | 200 | |
| 191 | - $dueDays = (int)$this->storeSettings->get('invoice_reminder_due_days', 0); | |
| 201 | + $dueDays = (int)$this->storeSettings->get('renewal_reminder_due_days', 0); | |
| 192 | 202 | $dueAt = $base + (max($dueDays, 0) * DAY_IN_SECONDS); |
| 193 | 203 | |
| 194 | 204 | $orderRef = !empty($order->invoice_no) ? (string)$order->invoice_no : '#' . (string)$order->id; |
| 195 | 205 | |
| @@ -205,11 +215,11 @@ | ||
| 205 | 215 | 'payment_link' => PaymentHelper::getCustomPaymentLink($order->uuid), |
| 206 | 216 | ] |
| 207 | 217 | ]; |
| 208 | 218 | |
| 209 | - do_action('fluent_cart/invoice_reminder_overdue', $data); | |
| 219 | + do_action('fluent_cart/renewal_reminder_overdue', $data); | |
| 210 | 220 | |
| 211 | - $state = $this->normalizeReminderState($order->getMeta(InvoiceReminderService::META_KEY, [])); | |
| 221 | + $state = $this->normalizeReminderState($order->getMeta(RenewalReminderService::META_KEY, [])); | |
| 212 | 222 | $cycleKey = md5(implode('|', ['order', $order->id, $dueAt, (int)$order->total_amount, (int)$order->total_paid])); |
| 213 | 223 | $cycleState = $this->getCycleState($state, $cycleKey); |
| 214 | 224 | |
| 215 | 225 | foreach (array_keys($cycleState['queue']) as $stage) { |
| @@ -219,9 +229,9 @@ | ||
| 219 | 229 | $cycleState['sent']['manual_' . time()] = gmdate('Y-m-d H:i:s'); |
| 220 | 230 | |
| 221 | 231 | $state = $this->setCycleState($state, $cycleKey, $cycleState); |
| 222 | 232 | $state['updated_at'] = gmdate('Y-m-d H:i:s'); |
| 223 | - $order->updateMeta(InvoiceReminderService::META_KEY, $state); | |
| 233 | + $order->updateMeta(RenewalReminderService::META_KEY, $state); | |
| 224 | 234 | |
| 225 | 235 | return ['success' => true, 'message' => __('Payment reminder sent successfully', 'fluent-cart')]; |
| 226 | 236 | } |
| 227 | 237 | |
| @@ -364,8 +374,10 @@ | ||
| 364 | 374 | $values = explode(',', $values); |
| 365 | 375 | } elseif (!is_array($values)) { |
| 366 | 376 | $values = $defaults; |
| 367 | 377 | } |
| 378 | + | |
| 379 | + $values = array_slice($values, 0, 10); | |
| 368 | 380 | |
| 369 | 381 | $days = []; |
| 370 | 382 | foreach ($values as $value) { |
| 371 | 383 | $day = (int)trim((string)$value); |