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