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 / Subscriptions / Http / Controllers / SubscriptionController.php

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

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