PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.4
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.4, at app/Modules/Subscriptions/Http/Controllers/SubscriptionController.php

213 lines 7.5 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' => sprintf(
110 /* translators: %1$s: error message returned by the payment vendor */
111 __('Subscription cancelled locally. Vendor Response: %1$s', 'fluent-cart'),
112 $vendorCancelled->get_error_message()
113 )
114 ]);
115 }
116
117 return $this->sendSuccess([
118 'message' => __('Subscription has been cancelled successfully!', 'fluent-cart'),
119 'subscription' => Subscription::query()->find($subscription->id)
120 ]);
121 }
122
123 public function reactivateSubscription(Request $request, Order $order, Subscription $subscription)
124 {
125 return $this->sendError([
126 'message' => __('Not available yet', 'fluent-cart')
127 ]);
128 }
129
130 public function fetchSubscription(Request $request, Order $order, Subscription $subscription)
131 {
132 $result = $subscription->reSyncFromRemote();
133
134 if (is_wp_error($result)) {
135 return $this->sendError([
136 'message' => $result->get_error_message()
137 ]);
138 }
139
140 return $this->sendSuccess([
141 'message' => __('Subscription fetched successfully from remote payment gateway!', 'fluent-cart'),
142 'subscription' => $result
143 ]);
144 }
145
146 public function pauseSubscription(Request $request, Order $order, Subscription $subscription)
147 {
148 return $this->sendError([
149 'message' => __('Not available yet', 'fluent-cart')
150 ]);
151
152 }
153
154 public function resumeSubscription(Request $request, Order $order, Subscription $subscription)
155 {
156 return $this->sendError([
157 'message' => __('Not available yet', 'fluent-cart')
158 ]);
159 }
160
161 public function generateEarlyPaymentLink(Request $request, Order $order, Subscription $subscription)
162 {
163 $this->validateSubscription($subscription);
164
165 if (!EarlyPaymentFeature::isEnabled()) {
166 return $this->sendError([
167 'message' => __('Early payment is not enabled for this site.', 'fluent-cart')
168 ]);
169 }
170
171 $subscriptionOrderId = null;
172 if (property_exists($subscription, 'parent_order_id') && $subscription->parent_order_id) {
173 $subscriptionOrderId = (int) $subscription->parent_order_id;
174 }
175
176 if ($subscriptionOrderId === null || $subscriptionOrderId !== (int) $order->id) {
177 return $this->sendError([
178 'message' => __('Invalid subscription for the specified order.', 'fluent-cart')
179 ]);
180 }
181
182 if ($subscription->bill_times <= 0) {
183 return $this->sendError([
184 'message' => __('Early payment is only available for installment subscriptions.', 'fluent-cart')
185 ]);
186 }
187
188 if (!in_array($subscription->status, [Status::SUBSCRIPTION_ACTIVE, Status::SUBSCRIPTION_TRIALING])) {
189 return $this->sendError([
190 'message' => __('Subscription must be active to make early payments.', 'fluent-cart')
191 ]);
192 }
193
194 $remaining = $subscription->bill_times - $subscription->bill_count;
195
196 if ($remaining <= 0) {
197 return $this->sendError([
198 'message' => __('All installments have already been paid.', 'fluent-cart')
199 ]);
200 }
201
202 $url = add_query_arg([
203 'fluent-cart' => 'early-installment-payment',
204 'subscription_hash' => $subscription->uuid,
205 ], home_url('/'));
206
207 return $this->sendSuccess([
208 'message' => __('Early payment link generated.', 'fluent-cart'),
209 'payment_url' => $url,
210 ]);
211 }
212 }
213