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