← All changes
|
app/Http/Controllers/FrontendControllers/CustomerSubscriptionController.php
+128
-4
1.5.4
→
1.7.0
View file →
| @@ -51,12 +51,11 @@ | ||
| 51 | 51 | |
| 52 | 52 | // Query the Subscription model to retrieve subscriptions for the logged-in customer |
| 53 | 53 | // Include associated order and product data using 'with' for eager loading |
| 54 | 54 | // Sort the results by 'id' in descending order and fetch the data |
| 55 | - $subscriptions = Subscription::query() | |
| 55 | + $subscriptions = Subscription::visibleToCustomer() | |
| 56 | 56 | ->where('customer_id', $customer->id) |
| 57 | 57 | ->with(['product']) |
| 58 | - ->whereNotIn('status', [Status::SUBSCRIPTION_PENDING, Status::SUBSCRIPTION_INTENDED]) | |
| 59 | 58 | ->orderBy('id', 'desc') |
| 60 | 59 | ->paginate($perPage, ['*'], 'page', $page); |
| 61 | 60 | |
| 62 | 61 | |
| @@ -98,9 +97,9 @@ | ||
| 98 | 97 | ->with(['product', 'variation', 'billing_addresses']) |
| 99 | 98 | ->where('uuid', $subscription_uuid) |
| 100 | 99 | ->first(); |
| 101 | 100 | |
| 102 | - if (!$subscription || in_array($subscription->status, [Status::SUBSCRIPTION_PENDING, Status::SUBSCRIPTION_INTENDED])) { | |
| 101 | + if (!$subscription || !$subscription->isVisibleToCustomer()) { | |
| 103 | 102 | return $this->sendError([ |
| 104 | 103 | 'message' => __('Subscription not found', 'fluent-cart') |
| 105 | 104 | ]); |
| 106 | 105 | } |
| @@ -119,8 +118,9 @@ | ||
| 119 | 118 | 'bill_count' => $subscription->bill_count, |
| 120 | 119 | 'variation_id' => $subscription->variation_id, |
| 121 | 120 | 'product_id' => $subscription->product_id, |
| 122 | 121 | 'config' => $subscription->config, |
| 122 | + 'collection_method' => $subscription->collection_method, | |
| 123 | 123 | 'reactivate_url' => $subscription->getReactivateUrl(), |
| 124 | 124 | // item_name resolves to "<product> - <attributes>" (or the raw name), |
| 125 | 125 | // so it carries the labeled combination on a single line. |
| 126 | 126 | 'title' => $subscription->display_item_name, |
| @@ -128,16 +128,24 @@ | ||
| 128 | 128 | 'can_upgrade' => $subscription->canUpgrade(), |
| 129 | 129 | 'can_switch_payment_method' => $subscription->canSwitchPaymentMethod(), |
| 130 | 130 | 'switchable_payment_methods' => $subscription->switchablePaymentMethods(), |
| 131 | 131 | 'can_update_payment_method' => $subscription->canUpdatePaymentMethod(), |
| 132 | + 'can_pause' => $subscription->canPause(), | |
| 133 | + 'can_resume' => $subscription->canResume(), | |
| 132 | 134 | 'order' => [ |
| 133 | 135 | 'uuid' => $subscription->order ? $subscription->order->uuid : '' |
| 134 | 136 | ], |
| 135 | 137 | 'billing_addresses' => $subscription->billing_addresses, |
| 136 | 138 | 'recurring_amount' => $subscription->recurring_amount, |
| 139 | + 'currency' => $subscription->currency, | |
| 137 | 140 | 'can_early_pay' => EarlyPaymentFeature::canPay($subscription), |
| 138 | 141 | 'remaining_installments' => max(0, $subscription->bill_times - $subscription->bill_count), |
| 139 | 142 | 'card_update_url' => $subscription->url, |
| 143 | + // Auto-charged (system) subscriptions: the saved card is charged | |
| 144 | + // automatically on each renewal date — the portal says so, and surfaces | |
| 145 | + // the last failure so the customer knows to update the card. | |
| 146 | + 'is_auto_charged' => $subscription->isSystem(), | |
| 147 | + 'auto_charge_error' => Arr::get((array) $subscription->system_charge_state, 'last_error', ''), | |
| 140 | 148 | ]; |
| 141 | 149 | |
| 142 | 150 | |
| 143 | 151 | // Let's find all the transactions related to this order |
| @@ -154,9 +162,15 @@ | ||
| 154 | 162 | ->orderBy('id', 'DESC') |
| 155 | 163 | ->get(); |
| 156 | 164 | |
| 157 | 165 | $formattedData['transactions'] = $transactions->map(function ($transaction) { |
| 158 | - return OrderService::transformTransaction($transaction); | |
| 166 | + $transformedTransaction = OrderService::transformTransaction($transaction); | |
| 167 | + if ($transaction->status === Status::TRANSACTION_PENDING | |
| 168 | + && !empty($transformedTransaction['custom_checkout_url']) | |
| 169 | + ) { | |
| 170 | + $transformedTransaction['show_pay_now'] = true; | |
| 171 | + } | |
| 172 | + return $transformedTransaction; | |
| 159 | 173 | }); |
| 160 | 174 | |
| 161 | 175 | $formattedData = apply_filters('fluent_cart/customer_portal/subscription_data', $formattedData, [ |
| 162 | 176 | 'subscription' => $subscription, |
| @@ -219,8 +233,13 @@ | ||
| 219 | 233 | return $this->sendError([ |
| 220 | 234 | 'message' => $e->getMessage() |
| 221 | 235 | ]); |
| 222 | 236 | } |
| 237 | + | |
| 238 | + return $this->sendSuccess([ | |
| 239 | + 'status' => 'success', | |
| 240 | + 'message' => __('Payment method updated successfully', 'fluent-cart') | |
| 241 | + ]); | |
| 223 | 242 | } |
| 224 | 243 | |
| 225 | 244 | return $this->sendError([ |
| 226 | 245 | 'message' => __('Could not update payment method', 'fluent-cart') |
| @@ -266,8 +285,13 @@ | ||
| 266 | 285 | return $this->sendError([ |
| 267 | 286 | 'message' => $e->getMessage() |
| 268 | 287 | ]); |
| 269 | 288 | } |
| 289 | + | |
| 290 | + return $this->sendSuccess([ | |
| 291 | + 'status' => 'success', | |
| 292 | + 'message' => __('Plan created successfully', 'fluent-cart') | |
| 293 | + ]); | |
| 270 | 294 | } |
| 271 | 295 | |
| 272 | 296 | return $this->sendError([ |
| 273 | 297 | 'message' => __('Could not get or create plan', 'fluent-cart') |
| @@ -306,8 +330,14 @@ | ||
| 306 | 330 | 'message' => __('Subscription not found', 'fluent-cart') |
| 307 | 331 | ]); |
| 308 | 332 | } |
| 309 | 333 | |
| 334 | + if (!$subscription->canSwitchPaymentMethod()) { | |
| 335 | + return $this->sendError([ | |
| 336 | + 'message' => __('This subscription cannot be moved to another payment gateway. Update the payment method on file instead.', 'fluent-cart') | |
| 337 | + ]); | |
| 338 | + } | |
| 339 | + | |
| 310 | 340 | if (App::gateway()->has($newPaymentMethod)) { |
| 311 | 341 | try { |
| 312 | 342 | App::gateway($newPaymentMethod)->subscriptions->switchPaymentMethod($data, $subscription->id); |
| 313 | 343 | } catch (\Exception $e) { |
| @@ -314,8 +344,13 @@ | ||
| 314 | 344 | return $this->sendError([ |
| 315 | 345 | 'message' => $e->getMessage() |
| 316 | 346 | ]); |
| 317 | 347 | } |
| 348 | + | |
| 349 | + return $this->sendSuccess([ | |
| 350 | + 'status' => 'success', | |
| 351 | + 'message' => __('Payment method switched successfully', 'fluent-cart') | |
| 352 | + ]); | |
| 318 | 353 | } |
| 319 | 354 | |
| 320 | 355 | return $this->sendError([ |
| 321 | 356 | 'message' => __('Could not switch payment method', 'fluent-cart') |
| @@ -356,8 +391,14 @@ | ||
| 356 | 391 | 'message' => __('Subscription not found', 'fluent-cart') |
| 357 | 392 | ]); |
| 358 | 393 | } |
| 359 | 394 | |
| 395 | + if (!$subscription->canSwitchPaymentMethod()) { | |
| 396 | + return $this->sendError([ | |
| 397 | + 'message' => __('This subscription cannot be moved to another payment gateway. Update the payment method on file instead.', 'fluent-cart') | |
| 398 | + ]); | |
| 399 | + } | |
| 400 | + | |
| 360 | 401 | if (APP::gateway()->has($method)) { |
| 361 | 402 | try { |
| 362 | 403 | APP::gateway($method)->subscriptions->confirmSubscriptionSwitch($data, $subscription->id); |
| 363 | 404 | } catch (\Exception $e) { |
| @@ -364,8 +405,13 @@ | ||
| 364 | 405 | return $this->sendError([ |
| 365 | 406 | 'message' => $e->getMessage() |
| 366 | 407 | ]); |
| 367 | 408 | } |
| 409 | + | |
| 410 | + return $this->sendSuccess([ | |
| 411 | + 'status' => 'success', | |
| 412 | + 'message' => __('Subscription switch confirmed successfully', 'fluent-cart') | |
| 413 | + ]); | |
| 368 | 414 | } |
| 369 | 415 | |
| 370 | 416 | return $this->sendError([ |
| 371 | 417 | 'message' => __('Could not confirm subscription switch', 'fluent-cart') |
| @@ -462,8 +508,86 @@ | ||
| 462 | 508 | return App::isProActive() |
| 463 | 509 | && $subscription->bill_times > 0 |
| 464 | 510 | && $subscription->bill_count < $subscription->bill_times |
| 465 | 511 | && in_array($subscription->status, [Status::SUBSCRIPTION_ACTIVE, Status::SUBSCRIPTION_TRIALING]); |
| 512 | + } | |
| 513 | + | |
| 514 | + public function pauseSubscription(Request $request, $subscription_uuid) | |
| 515 | + { | |
| 516 | + $errorResponse = $this->checkUserLoggedIn(); | |
| 517 | + if ($errorResponse !== null) { | |
| 518 | + return $errorResponse; | |
| 519 | + } | |
| 520 | + | |
| 521 | + $customer = CustomerResource::getCurrentCustomer(); | |
| 522 | + if (!$customer) { | |
| 523 | + return $this->sendError([ | |
| 524 | + 'message' => __('Customer not found', 'fluent-cart') | |
| 525 | + ]); | |
| 526 | + } | |
| 527 | + | |
| 528 | + $subscription = Subscription::query() | |
| 529 | + ->where('uuid', $subscription_uuid) | |
| 530 | + ->where('customer_id', $customer->id) | |
| 531 | + ->first(); | |
| 532 | + | |
| 533 | + if (empty($subscription)) { | |
| 534 | + return $this->sendError([ | |
| 535 | + 'message' => __('Subscription not found', 'fluent-cart') | |
| 536 | + ]); | |
| 537 | + } | |
| 538 | + | |
| 539 | + $reason = __('Paused by customer from customer portal', 'fluent-cart'); | |
| 540 | + $result = $subscription->pauseSubscription($reason); | |
| 541 | + | |
| 542 | + if (is_wp_error($result)) { | |
| 543 | + return $this->sendError([ | |
| 544 | + 'message' => $result->get_error_message() | |
| 545 | + ]); | |
| 546 | + } | |
| 547 | + | |
| 548 | + return [ | |
| 549 | + 'message' => __('Your subscription has been successfully paused', 'fluent-cart') | |
| 550 | + ]; | |
| 551 | + } | |
| 552 | + | |
| 553 | + public function resumeSubscription(Request $request, $subscription_uuid) | |
| 554 | + { | |
| 555 | + $errorResponse = $this->checkUserLoggedIn(); | |
| 556 | + if ($errorResponse !== null) { | |
| 557 | + return $errorResponse; | |
| 558 | + } | |
| 559 | + | |
| 560 | + $customer = CustomerResource::getCurrentCustomer(); | |
| 561 | + if (!$customer) { | |
| 562 | + return $this->sendError([ | |
| 563 | + 'message' => __('Customer not found', 'fluent-cart') | |
| 564 | + ]); | |
| 565 | + } | |
| 566 | + | |
| 567 | + $subscription = Subscription::query() | |
| 568 | + ->where('uuid', $subscription_uuid) | |
| 569 | + ->where('customer_id', $customer->id) | |
| 570 | + ->first(); | |
| 571 | + | |
| 572 | + if (empty($subscription)) { | |
| 573 | + return $this->sendError([ | |
| 574 | + 'message' => __('Subscription not found', 'fluent-cart') | |
| 575 | + ]); | |
| 576 | + } | |
| 577 | + | |
| 578 | + $reason = __('Resumed by customer from customer portal', 'fluent-cart'); | |
| 579 | + $result = $subscription->resumeSubscription($reason); | |
| 580 | + | |
| 581 | + if (is_wp_error($result)) { | |
| 582 | + return $this->sendError([ | |
| 583 | + 'message' => $result->get_error_message() | |
| 584 | + ]); | |
| 585 | + } | |
| 586 | + | |
| 587 | + return [ | |
| 588 | + 'message' => __('Your subscription has been successfully resumed', 'fluent-cart') | |
| 589 | + ]; | |
| 466 | 590 | } |
| 467 | 591 | |
| 468 | 592 | public function getSetupIntentRemainingAttempts($subscription_uuid) |
| 469 | 593 | { |