PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.0
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 trunk All 48 releases
fluent-cart / app / Modules / PaymentMethods / Cod / CodHandler.php

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

108 lines 3.7 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 do_action('fluent_cart/order_placed_offline', $data);
58 }
59
60 $paymentHelper = new PaymentHelper('offline_payment');
61
62 $relatedCart = Cart::query()->where('order_id', $order->id)
63 ->where('stage', '!=', 'completed')
64 ->first();
65
66 if ($relatedCart) {
67 $relatedCart->stage = 'completed';
68 $relatedCart->completed_at = DateTime::now()->format('Y-m-d H:i:s');
69 $relatedCart->save();
70 }
71
72 return $paymentHelper->successUrl($paymentInstance->transaction->uuid);
73 }
74
75 public function handleZeroTotalPayment($paymentInstance)
76 {
77 $order = $paymentInstance->order;
78
79 $transaction = $paymentInstance->transaction;
80 $transaction->status = Status::TRANSACTION_SUCCEEDED;
81 $transaction->save();
82
83 (new StatusHelper($order))->syncOrderStatuses($transaction);
84
85 // Check if this order has subscriptions with zero or negative recurring_total
86 if ($order->payment_method === 'offline_payment') {
87 $subscription = Subscription::query()
88 ->where('parent_order_id', $order->id)
89 ->where('recurring_total', '<=', 0)
90 ->first();
91
92 if ($subscription) {
93 $oldStatus = $subscription->status;
94 $subscription = SubscriptionService::syncSubscriptionStates($subscription, [
95 'status' => Status::SUBSCRIPTION_ACTIVE,
96 'next_billing_date' => null,
97 ]);
98 if ($oldStatus !== Status::SUBSCRIPTION_ACTIVE && $subscription->status === Status::SUBSCRIPTION_ACTIVE) {
99 (new SubscriptionActivated($subscription, $order, $order->customer))->dispatch();
100 }
101 }
102 }
103
104 $paymentHelper = new PaymentHelper('offline_payment');
105 return $paymentHelper->successUrl($transaction->uuid);
106 }
107 }
108