| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\Cod; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\App\Events\Subscription\SubscriptionActivated; |
| 7 |
use FluentCart\App\Helpers\Status; |
| 8 |
use FluentCart\App\Helpers\StatusHelper; |
| 9 |
use FluentCart\App\Models\Cart; |
| 10 |
use FluentCart\App\Modules\Subscriptions\Services\SubscriptionService; |
| 11 |
use FluentCart\App\Services\DateTime\DateTime; |
| 12 |
use FluentCart\App\Models\Subscription; |
| 13 |
|
| 14 |
class CodHandler { |
| 15 |
|
| 16 |
protected $cod; |
| 17 |
|
| 18 |
public function __construct(Cod $cod) |
| 19 |
{ |
| 20 |
$this->cod = $cod; |
| 21 |
} |
| 22 |
public function handlePayment($paymentInstance) |
| 23 |
{ |
| 24 |
$settings = $this->cod->settings->get(); |
| 25 |
$order = $paymentInstance->order; |
| 26 |
if ($order->total_amount == 0) { |
| 27 |
return $this->handleZeroTotalPayment($paymentInstance); |
| 28 |
} |
| 29 |
|
| 30 |
if (($settings['is_active'] ?? 'no') !== 'yes') { |
| 31 |
throw new \Exception(esc_html__('Offline payment is not activated', 'fluent-cart')); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
$order->payment_method_title = $this->cod->getMeta('title'); |
| 36 |
$order->save(); |
| 37 |
|
| 38 |
|
| 39 |
if (!$order->id) { |
| 40 |
throw new \Exception(esc_html__('Order not found!', 'fluent-cart')); |
| 41 |
} |
| 42 |
|
| 43 |
if ($paymentInstance->order) { |
| 44 |
if (!$paymentInstance->order->customer) { |
| 45 |
$paymentInstance->order->customer = $paymentInstance->order->customer()->first(); |
| 46 |
} |
| 47 |
|
| 48 |
$paymentInstance->order->load(['customer', 'shipping_address', 'billing_address']); |
| 49 |
|
| 50 |
$data = [ |
| 51 |
'order' => $paymentInstance->order, |
| 52 |
'customer' => $paymentInstance->order->customer ?? [], |
| 53 |
'transaction' => $paymentInstance->transaction ?? [] |
| 54 |
]; |
| 55 |
|
| 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 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$relatedCart = Cart::query()->where('order_id', $order->id) |
| 64 |
->where('stage', '!=', 'completed') |
| 65 |
->first(); |
| 66 |
|
| 67 |
if ($relatedCart) { |
| 68 |
$relatedCart->stage = 'completed'; |
| 69 |
$relatedCart->completed_at = DateTime::now()->format('Y-m-d H:i:s'); |
| 70 |
$relatedCart->save(); |
| 71 |
} |
| 72 |
|
| 73 |
return $paymentInstance->transaction->getSuccessUrl(); |
| 74 |
} |
| 75 |
|
| 76 |
public function handleZeroTotalPayment($paymentInstance) |
| 77 |
{ |
| 78 |
$order = $paymentInstance->order; |
| 79 |
|
| 80 |
$transaction = $paymentInstance->transaction; |
| 81 |
$transaction->status = Status::TRANSACTION_SUCCEEDED; |
| 82 |
$transaction->save(); |
| 83 |
|
| 84 |
(new StatusHelper($order))->syncOrderStatuses($transaction); |
| 85 |
|
| 86 |
// Check if this order has subscriptions with zero or negative recurring_total |
| 87 |
if ($order->payment_method === 'offline_payment') { |
| 88 |
$subscription = Subscription::query() |
| 89 |
->where('parent_order_id', $order->id) |
| 90 |
->where('recurring_total', '<=', 0) |
| 91 |
->first(); |
| 92 |
|
| 93 |
if ($subscription) { |
| 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 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $transaction->getSuccessUrl(); |
| 106 |
} |
| 107 |
} |
| 108 |
|