AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\Billing;
  4. use App\Billing\UsageLimits;
  5. use App\Commerce\Payments\PaymentException;
  6. use App\Models\Plan;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\Rule;
  10. use Inertia\Inertia;
  11. use Inertia\Response;
  12. use Symfony\Component\HttpFoundation\Response as HttpResponse;
  13. /**
  14. * The merchant's own plan: see it, start it, change it, cancel it.
  15. */
  16. class BillingController extends Controller
  17. {
  18. public function index(Request $request, Billing $billing, UsageLimits $usage): Response|RedirectResponse
  19. {
  20. $user = $request->user();
  21. // Coming back from the payment page: attach the new subscription, then show a clean address.
  22. if (is_string($request->query('session_id'))) {
  23. try {
  24. $attached = $billing->completeCheckout($user, $request->query('session_id'));
  25. } catch (PaymentException $e) {
  26. report($e);
  27. $attached = false;
  28. }
  29. Inertia::flash('toast', $attached
  30. ? ['type' => 'success', 'message' => __('Your plan is active. Thank you!')]
  31. : ['type' => 'error', 'message' => __('We could not confirm the payment. If you were charged, it will show here shortly.')]);
  32. return to_route('billing');
  33. }
  34. // Ask Stripe for the latest state, so this page is right even without webhooks.
  35. $billing->refresh($user);
  36. $subscription = $user->subscription()->with('plan')->first();
  37. return Inertia::render('billing/index', [
  38. 'usage' => $usage->for($user),
  39. 'plans' => Plan::query()->where('price', '>', 0)->orderBy('sort')->get()->map(fn (Plan $plan) => $plan->toDisplay()),
  40. 'subscription' => $subscription !== null && $subscription->stripe_subscription_id !== null ? [
  41. 'active' => $subscription->isActive(),
  42. 'status' => $subscription->status,
  43. 'plan' => $subscription->plan?->key,
  44. 'current_period_end' => $subscription->current_period_end?->toIso8601String(),
  45. 'cancel_at_period_end' => $subscription->cancel_at_period_end,
  46. ] : null,
  47. ]);
  48. }
  49. /**
  50. * Start a paid plan, or move to a different one.
  51. */
  52. public function subscribe(Request $request, Billing $billing): HttpResponse
  53. {
  54. $validated = $request->validate([
  55. 'plan' => ['required', 'string', Rule::exists('plans', 'key')->where(fn ($query) => $query->where('price', '>', 0))],
  56. ]);
  57. $user = $request->user();
  58. $plan = Plan::query()->where('key', $validated['plan'])->firstOrFail();
  59. try {
  60. // Already paying: switch plans in place. Stripe works out the price difference.
  61. if ($user->subscription?->isActive()) {
  62. $billing->changePlan($user, $plan);
  63. return $this->back('success', __('You are now on the :plan plan.', ['plan' => $plan->name]));
  64. }
  65. $link = $billing->startCheckout($user, $plan, route('billing'), route('billing'));
  66. } catch (PaymentException $e) {
  67. report($e);
  68. return $this->back('error', __('We could not reach the payment service. Please try again in a moment.'));
  69. }
  70. // The payment page is on another website, so this is a full browser redirect.
  71. return Inertia::location($link->url);
  72. }
  73. /**
  74. * Stop renewing. The plan stays until the end of the period already paid for.
  75. */
  76. public function cancel(Request $request, Billing $billing): RedirectResponse
  77. {
  78. return $this->change($request, $billing, true, __('Your plan will end on the date shown. You can resume it any time before then.'));
  79. }
  80. /**
  81. * Undo a cancellation before the period ends.
  82. */
  83. public function resume(Request $request, Billing $billing): RedirectResponse
  84. {
  85. return $this->change($request, $billing, false, __('Your plan will keep renewing.'));
  86. }
  87. private function change(Request $request, Billing $billing, bool $cancel, string $message): RedirectResponse
  88. {
  89. try {
  90. $billing->setCancel($request->user(), $cancel);
  91. } catch (PaymentException $e) {
  92. report($e);
  93. return $this->back('error', __('We could not reach the payment service. Please try again in a moment.'));
  94. }
  95. return $this->back('success', $message);
  96. }
  97. private function back(string $type, string $message): RedirectResponse
  98. {
  99. Inertia::flash('toast', ['type' => $type, 'message' => $message]);
  100. return to_route('billing');
  101. }
  102. }