opnform/app/Http/Controllers/SubscriptionController.php

98 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Subscriptions\UpdateStripeDetailsRequest;
2022-09-20 19:59:52 +00:00
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Laravel\Cashier\Subscription;
class SubscriptionController extends Controller
{
2024-02-23 10:54:12 +00:00
public const SUBSCRIPTION_PLANS = ['monthly', 'yearly'];
2022-09-20 19:59:52 +00:00
2024-02-23 10:54:12 +00:00
public const PRO_SUBSCRIPTION_NAME = 'default';
public const SUBSCRIPTION_NAMES = [
2022-09-20 19:59:52 +00:00
self::PRO_SUBSCRIPTION_NAME,
];
/**
* Returns stripe checkout URL
*
* $plan is constrained with regex in the api.php
*/
public function checkout($pricing, $plan, $trial = null)
{
$this->middleware('not-subscribed');
// Check User does not have a pending subscription
$user = Auth::user();
if ($user->subscriptions()->where('stripe_status', 'past_due')->first()) {
return $this->error([
'message' => 'You already have a past due subscription. Please verify your details in the billing page,
2024-02-23 10:54:12 +00:00
and contact us if the issue persists.',
2022-09-20 19:59:52 +00:00
]);
}
$checkoutBuilder = $user
->newSubscription($pricing, $this->getPricing($pricing)[$plan])
->allowPromotionCodes();
if ($trial != null) {
$checkoutBuilder->trialUntil(now()->addDays(3)->addHour());
2022-09-20 19:59:52 +00:00
}
$checkout = $checkoutBuilder
->collectTaxIds()
->checkout([
'success_url' => front_url('/subscriptions/success'),
'cancel_url' => front_url('/subscriptions/error'),
'billing_address_collection' => 'required',
'customer_update' => [
'address' => 'auto',
'name' => 'never',
2024-02-23 10:54:12 +00:00
],
2022-09-20 19:59:52 +00:00
]);
return $this->success([
2024-02-23 10:54:12 +00:00
'checkout_url' => $checkout->url,
2022-09-20 19:59:52 +00:00
]);
}
public function updateStripeDetails(UpdateStripeDetailsRequest $request)
{
$user = Auth::user();
2024-02-23 10:54:12 +00:00
if (! $user->hasStripeId()) {
$user->createAsStripeCustomer();
}
$user->updateStripeCustomer([
'email' => $request->email,
'name' => $request->name,
]);
return $this->success([
'message' => 'Details saved.',
]);
}
2022-09-20 19:59:52 +00:00
public function billingPortal()
{
$this->middleware('auth');
2024-02-23 10:54:12 +00:00
if (! Auth::user()->has_customer_id) {
2022-09-20 19:59:52 +00:00
return $this->error([
2024-02-23 10:54:12 +00:00
'message' => 'Please subscribe before accessing your billing portal.',
2022-09-20 19:59:52 +00:00
]);
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this->success([
2024-02-23 10:54:12 +00:00
'portal_url' => Auth::user()->billingPortalUrl(front_url('/home')),
2022-09-20 19:59:52 +00:00
]);
}
private function getPricing($product = 'default')
2022-09-20 19:59:52 +00:00
{
return App::environment() == 'production' ? config('pricing.production.'.$product.'.pricing') : config('pricing.test.'.$product.'.pricing');
}
}