| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\StoreManagedRenewal\Services; |
| 4 |
|
| 5 |
class RenewalScheduler |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Register the scheduler hook for hourly processing |
| 9 |
*/ |
| 10 |
public function register() |
| 11 |
{ |
| 12 |
add_action('fluent_cart/scheduler/hourly_tasks', [$this, 'handle'], 10); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Handle the hourly task to process due subscriptions, overdue invoices, and reminders |
| 17 |
*/ |
| 18 |
public function handle() |
| 19 |
{ |
| 20 |
// 1. Create renewal invoices for due subscriptions |
| 21 |
$results = RenewalService::processDueSubscriptions(20); |
| 22 |
|
| 23 |
if ($results['processed'] > 0 || $results['failed'] > 0) { |
| 24 |
fluent_cart_error_log( |
| 25 |
'Renewal Scheduler Results', |
| 26 |
sprintf( |
| 27 |
'Processed: %d, Failed: %d', |
| 28 |
$results['processed'], |
| 29 |
$results['failed'] |
| 30 |
) |
| 31 |
); |
| 32 |
|
| 33 |
if (!empty($results['errors'])) { |
| 34 |
foreach ($results['errors'] as $error) { |
| 35 |
fluent_cart_error_log( |
| 36 |
'Renewal Scheduler Error', |
| 37 |
sprintf( |
| 38 |
'Subscription ID: %d, Error: %s', |
| 39 |
$error['subscription_id'], |
| 40 |
$error['error'] |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// 2. Process overdue invoices — transition subscriptions past_due → expired |
| 48 |
$overdueResults = RenewalService::processOverdueRenewals(20); |
| 49 |
|
| 50 |
if ($overdueResults['past_due'] > 0 || $overdueResults['expired'] > 0) { |
| 51 |
fluent_cart_error_log( |
| 52 |
'Overdue Renewal Processing Results', |
| 53 |
sprintf( |
| 54 |
'Marked %d subscription(s) as past due, %d as expired', |
| 55 |
$overdueResults['past_due'], |
| 56 |
$overdueResults['expired'] |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
} |
| 62 |
} |
| 63 |
|