PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.26
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.26
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 / Subscriptions / Http / Controllers / SubscriptionController.php

SubscriptionController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.26, at app/Modules/Subscriptions/Http/Controllers/SubscriptionController.php

207 lines 7.1 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\Subscriptions\Http\Controllers;
4
5 use FluentCart\App\Helpers\Status;
6 use FluentCart\App\Http\Controllers\Controller;
7 use FluentCart\App\Models\Order;
8 use FluentCart\App\Models\Subscription;
9 use FluentCart\App\Modules\Subscriptions\Services\EarlyPaymentFeature;
10 use FluentCart\App\Services\Reminders\ReminderService;
11 use FluentCart\Framework\Http\Request\Request;
12 use FluentCart\App\Modules\Subscriptions\Services\Filter\SubscriptionFilter;
13
14 class SubscriptionController extends Controller
15 {
16 public function index(Request $request): array
17 {
18
19
20 return [
21 'data' => SubscriptionFilter::fromRequest($request)->paginate(),
22 ];
23 }
24
25 public function getSubscriptionOrderDetails($subscriptionOrderId)
26 {
27
28 $subscription = Subscription::with([
29 'labels',
30 'activities.user',
31 'customer.shipping_address' => function ($query) {
32 $query->where('is_primary', 1);
33 },
34 'customer.billing_address' => function ($query) {
35 $query->where('is_primary', 1);
36 },
37 'order.billing_address',
38 'order.shipping_address',
39 ])
40 ->find($subscriptionOrderId);
41
42 if (is_wp_error($subscription) || empty($subscription)) {
43 return $this->entityNotFoundError(
44 __('Subscription not found', 'fluent-cart'),
45 __('Back to Subscription list', 'fluent-cart'),
46 '/subscriptions'
47 );
48 }
49
50 // Attach parent order's addresses directly to subscription for consistent access
51 $subscription->billing_address = $subscription->order->billing_address ?? null;
52 $subscription->shipping_address = $subscription->order->shipping_address ?? null;
53
54 $subscription->related_orders = Order::query()
55 ->with(['order_items' => function ($query) {
56 $query->select('id', 'order_id', 'post_title', 'title', 'quantity', 'payment_type', 'line_meta');
57 }])
58 ->where('id', $subscription->parent_order_id)
59 ->orWhere('parent_id', $subscription->parent_order_id)
60 ->orderBy('id', 'DESC')
61 ->get();
62
63
64 $subscription = apply_filters('fluent_cart/subscription/view', $subscription, []);
65
66 $reminderPermissions = (new ReminderService())->getSubscriptionReminderPermissions($subscription);
67
68 return $this->sendSuccess([
69 'subscription' => $subscription,
70 'selected_labels' => $subscription->labels->pluck('label_id'),
71 'reminder_permissions' => $reminderPermissions,
72 ]);
73
74 }
75
76 public function validateSubscription($subscription)
77 {
78 if (!$subscription) {
79 $this->sendError(['message' => __('Subscription not found!', 'fluent-cart')], 404);
80 }
81 }
82
83 public function cancelSubscription(Request $request, Order $order, Subscription $subscription)
84 {
85 $this->validateSubscription($subscription);
86
87 if (empty($request->getSafe('cancel_reason', 'sanitize_text_field'))) {
88 return $this->sendError([
89 'message' => __('Please select cancel reason!', 'fluent-cart')
90 ]);
91 }
92
93 $result = $subscription->cancelRemoteSubscription([
94 'reason' => $request->getSafe('cancel_reason', 'sanitize_text_field')
95 ]);
96
97 if (is_wp_error($result)) {
98 return $this->sendError([
99 'message' => $result->get_error_message()
100 ]);
101 }
102
103 $vendorCancelled = $result['vendor_result'];
104
105 if (is_wp_error($vendorCancelled)) {
106 return $this->sendError([
107 'message' => 'Subscription cancelled locally. Vendor Response: ' . $vendorCancelled->get_error_message()
108 ]);
109 }
110
111 return $this->sendSuccess([
112 'message' => __('Subscription has been cancelled successfully!', 'fluent-cart'),
113 'subscription' => Subscription::query()->find($subscription->id)
114 ]);
115 }
116
117 public function reactivateSubscription(Request $request, Order $order, Subscription $subscription)
118 {
119 return $this->sendError([
120 'message' => __('Not available yet', 'fluent-cart')
121 ]);
122 }
123
124 public function fetchSubscription(Request $request, Order $order, Subscription $subscription)
125 {
126 $result = $subscription->reSyncFromRemote();
127
128 if (is_wp_error($result)) {
129 return $this->sendError([
130 'message' => $result->get_error_message()
131 ]);
132 }
133
134 return $this->sendSuccess([
135 'message' => __('Subscription fetched successfully from remote payment gateway!', 'fluent-cart'),
136 'subscription' => $result
137 ]);
138 }
139
140 public function pauseSubscription(Request $request, Order $order, Subscription $subscription)
141 {
142 return $this->sendError([
143 'message' => __('Not available yet', 'fluent-cart')
144 ]);
145
146 }
147
148 public function resumeSubscription(Request $request, Order $order, Subscription $subscription)
149 {
150 return $this->sendError([
151 'message' => __('Not available yet', 'fluent-cart')
152 ]);
153 }
154
155 public function generateEarlyPaymentLink(Request $request, Order $order, Subscription $subscription)
156 {
157 $this->validateSubscription($subscription);
158
159 if (!EarlyPaymentFeature::isEnabled()) {
160 return $this->sendError([
161 'message' => __('Early payment is not enabled for this site.', 'fluent-cart')
162 ]);
163 }
164
165 $subscriptionOrderId = null;
166 if (property_exists($subscription, 'parent_order_id') && $subscription->parent_order_id) {
167 $subscriptionOrderId = (int) $subscription->parent_order_id;
168 }
169
170 if ($subscriptionOrderId === null || $subscriptionOrderId !== (int) $order->id) {
171 return $this->sendError([
172 'message' => __('Invalid subscription for the specified order.', 'fluent-cart')
173 ]);
174 }
175
176 if ($subscription->bill_times <= 0) {
177 return $this->sendError([
178 'message' => __('Early payment is only available for installment subscriptions.', 'fluent-cart')
179 ]);
180 }
181
182 if (!in_array($subscription->status, [Status::SUBSCRIPTION_ACTIVE, Status::SUBSCRIPTION_TRIALING])) {
183 return $this->sendError([
184 'message' => __('Subscription must be active to make early payments.', 'fluent-cart')
185 ]);
186 }
187
188 $remaining = $subscription->bill_times - $subscription->bill_count;
189
190 if ($remaining <= 0) {
191 return $this->sendError([
192 'message' => __('All installments have already been paid.', 'fluent-cart')
193 ]);
194 }
195
196 $url = add_query_arg([
197 'fluent-cart' => 'early-installment-payment',
198 'subscription_hash' => $subscription->uuid,
199 ], home_url('/'));
200
201 return $this->sendSuccess([
202 'message' => __('Early payment link generated.', 'fluent-cart'),
203 'payment_url' => $url,
204 ]);
205 }
206 }
207