| @@ -2,13 +2,14 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Modules\PaymentMethods\Cod; |
| 4 | 4 | |
| 5 | 5 | |
| 6 | +use FluentCart\App\Events\Subscription\SubscriptionActivated; | |
| 6 | 7 | use FluentCart\App\Helpers\Status; |
| 7 | 8 | use FluentCart\App\Helpers\StatusHelper; |
| 8 | 9 | use FluentCart\App\Models\Cart; |
| 10 | +use FluentCart\App\Modules\Subscriptions\Services\SubscriptionService; | |
| 9 | 11 | use FluentCart\App\Services\DateTime\DateTime; |
| 10 | -use FluentCart\App\Services\Payments\PaymentHelper; | |
| 11 | 12 | use FluentCart\App\Models\Subscription; |
| 12 | 13 | |
| 13 | 14 | class CodHandler { |
| 14 | 15 | |
| @@ -25,9 +26,9 @@ | ||
| 25 | 26 | if ($order->total_amount == 0) { |
| 26 | 27 | return $this->handleZeroTotalPayment($paymentInstance); |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | - if (!$settings['is_active'] === 'yes') { | |
| 30 | + if (($settings['is_active'] ?? 'no') !== 'yes') { | |
| 30 | 31 | throw new \Exception(esc_html__('Offline payment is not activated', 'fluent-cart')); |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | 34 | |
| @@ -51,13 +52,15 @@ | ||
| 51 | 52 | 'customer' => $paymentInstance->order->customer ?? [], |
| 52 | 53 | 'transaction' => $paymentInstance->transaction ?? [] |
| 53 | 54 | ]; |
| 54 | 55 | |
| 55 | - do_action('fluent_cart/order_placed_offline', $data); | |
| 56 | + // Renewal invoices are not new orders — skip placement emails. | |
| 57 | + // SubscriptionRenewed fires separately once the invoice is paid. | |
| 58 | + if ($paymentInstance->order->type !== Status::ORDER_TYPE_RENEWAL) { | |
| 59 | + do_action('fluent_cart/order_placed_offline', $data); | |
| 60 | + } | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | - $paymentHelper = new PaymentHelper('offline_payment'); | |
| 59 | - | |
| 60 | 63 | $relatedCart = Cart::query()->where('order_id', $order->id) |
| 61 | 64 | ->where('stage', '!=', 'completed') |
| 62 | 65 | ->first(); |
| 63 | 66 | |
| @@ -66,9 +69,9 @@ | ||
| 66 | 69 | $relatedCart->completed_at = DateTime::now()->format('Y-m-d H:i:s'); |
| 67 | 70 | $relatedCart->save(); |
| 68 | 71 | } |
| 69 | 72 | |
| 70 | - return $paymentHelper->successUrl($paymentInstance->transaction->uuid); | |
| 73 | + return $paymentInstance->transaction->getSuccessUrl(); | |
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | public function handleZeroTotalPayment($paymentInstance) |
| 74 | 77 | { |
| @@ -87,14 +90,18 @@ | ||
| 87 | 90 | ->where('recurring_total', '<=', 0) |
| 88 | 91 | ->first(); |
| 89 | 92 | |
| 90 | 93 | if ($subscription) { |
| 91 | - $subscription->status = 'active'; | |
| 92 | - $subscription->next_billing_date = null; // No future billing needed | |
| 93 | - $subscription->save(); | |
| 94 | + $oldStatus = $subscription->status; | |
| 95 | + $subscription = SubscriptionService::syncSubscriptionStates($subscription, [ | |
| 96 | + 'status' => Status::SUBSCRIPTION_ACTIVE, | |
| 97 | + 'next_billing_date' => null, | |
| 98 | + ]); | |
| 99 | + if ($oldStatus !== Status::SUBSCRIPTION_ACTIVE && $subscription->status === Status::SUBSCRIPTION_ACTIVE) { | |
| 100 | + (new SubscriptionActivated($subscription, $order, $order->customer))->dispatch(); | |
| 101 | + } | |
| 94 | 102 | } |
| 95 | 103 | } |
| 96 | 104 | |
| 97 | - $paymentHelper = new PaymentHelper('offline_payment'); | |
| 98 | - return $paymentHelper->successUrl($transaction->uuid); | |
| 105 | + return $transaction->getSuccessUrl(); | |
| 99 | 106 | } |
| 100 | 107 | } |