| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\StoreManagedRenewal; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Helpers\Status; |
| 7 |
use FluentCart\App\Modules\StoreManagedRenewal\Services\RenewalService; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class RenewalModule |
| 11 |
{ |
| 12 |
public static function register() |
| 13 |
{ |
| 14 |
$self = new static(); |
| 15 |
App::getInstance()->addAction('fluentcart_loaded', [$self, 'init']); |
| 16 |
} |
| 17 |
|
| 18 |
public function init($app) |
| 19 |
{ |
| 20 |
// Register the scheduler for processing manual subscription renewals |
| 21 |
(new Services\RenewalScheduler())->register(); |
| 22 |
|
| 23 |
// Handle renewal invoice paid — fired from StatusHelper::syncOrderStatuses() whenever |
| 24 |
// a renewal order transitions to paid. Covers all payment paths: admin mark-as-paid, |
| 25 |
// offline gateway, Stripe/Paystack/MercadoPago webhooks — they all go through syncOrderStatuses. |
| 26 |
add_action('fluent_cart/renewal_paid', function ($data) { |
| 27 |
RenewalService::handleRenewalPaid(['order' => $data['order'] ?? null]); |
| 28 |
}, 10, 1); |
| 29 |
|
| 30 |
// Fired by gateways when a renewal invoice's charge is deferred (e.g. Stripe setup intent). |
| 31 |
// Marks the invoice as payment_scheduled so the customer and admin know payment is coming. |
| 32 |
$markRenewalInvoicePaymentScheduled = function ($payload, $legacySubscription = null) { |
| 33 |
$order = is_array($payload) ? Arr::get($payload, 'order') : $payload; |
| 34 |
|
| 35 |
if (!$order) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if ($order->payment_status === Status::PAYMENT_PENDING) { |
| 40 |
$order->payment_status = Status::PAYMENT_SCHEDULED; |
| 41 |
$order->save(); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
add_action('fluent_cart/renewal/payment_scheduled', $markRenewalInvoicePaymentScheduled, 10, 1); |
| 46 |
} |
| 47 |
} |
| 48 |
|