PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.0
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Modules / PaymentMethods / Cod / CodHandler.php

CodHandler.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.0, at app/Modules/PaymentMethods/Cod/CodHandler.php

112 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Services\Payments\PaymentHelper;
13 use FluentCart\App\Models\Subscription;
14
15 class CodHandler {
16
17 protected $cod;
18
19 public function __construct(Cod $cod)
20 {
21 $this->cod = $cod;
22 }
23 public function handlePayment($paymentInstance)
24 {
25 $settings = $this->cod->settings->get();
26 $order = $paymentInstance->order;
27 if ($order->total_amount == 0) {
28 return $this->handleZeroTotalPayment($paymentInstance);
29 }
30
31 if (($settings['is_active'] ?? 'no') !== 'yes') {
32 throw new \Exception(esc_html__('Offline payment is not activated', 'fluent-cart'));
33 }
34
35
36 $order->payment_method_title = $this->cod->getMeta('title');
37 $order->save();
38
39
40 if (!$order->id) {
41 throw new \Exception(esc_html__('Order not found!', 'fluent-cart'));
42 }
43
44 if ($paymentInstance->order) {
45 if (!$paymentInstance->order->customer) {
46 $paymentInstance->order->customer = $paymentInstance->order->customer()->first();
47 }
48
49 $paymentInstance->order->load(['customer', 'shipping_address', 'billing_address']);
50
51 $data = [
52 'order' => $paymentInstance->order,
53 'customer' => $paymentInstance->order->customer ?? [],
54 'transaction' => $paymentInstance->transaction ?? []
55 ];
56
57 // Renewal invoices are not new orders — skip placement emails.
58 // SubscriptionRenewed fires separately once the invoice is paid.
59 if ($paymentInstance->order->type !== Status::ORDER_TYPE_RENEWAL) {
60 do_action('fluent_cart/order_placed_offline', $data);
61 }
62 }
63
64 $paymentHelper = new PaymentHelper('offline_payment');
65
66 $relatedCart = Cart::query()->where('order_id', $order->id)
67 ->where('stage', '!=', 'completed')
68 ->first();
69
70 if ($relatedCart) {
71 $relatedCart->stage = 'completed';
72 $relatedCart->completed_at = DateTime::now()->format('Y-m-d H:i:s');
73 $relatedCart->save();
74 }
75
76 return $paymentHelper->successUrl($paymentInstance->transaction->uuid);
77 }
78
79 public function handleZeroTotalPayment($paymentInstance)
80 {
81 $order = $paymentInstance->order;
82
83 $transaction = $paymentInstance->transaction;
84 $transaction->status = Status::TRANSACTION_SUCCEEDED;
85 $transaction->save();
86
87 (new StatusHelper($order))->syncOrderStatuses($transaction);
88
89 // Check if this order has subscriptions with zero or negative recurring_total
90 if ($order->payment_method === 'offline_payment') {
91 $subscription = Subscription::query()
92 ->where('parent_order_id', $order->id)
93 ->where('recurring_total', '<=', 0)
94 ->first();
95
96 if ($subscription) {
97 $oldStatus = $subscription->status;
98 $subscription = SubscriptionService::syncSubscriptionStates($subscription, [
99 'status' => Status::SUBSCRIPTION_ACTIVE,
100 'next_billing_date' => null,
101 ]);
102 if ($oldStatus !== Status::SUBSCRIPTION_ACTIVE && $subscription->status === Status::SUBSCRIPTION_ACTIVE) {
103 (new SubscriptionActivated($subscription, $order, $order->customer))->dispatch();
104 }
105 }
106 }
107
108 $paymentHelper = new PaymentHelper('offline_payment');
109 return $paymentHelper->successUrl($transaction->uuid);
110 }
111 }
112