Skip to content

Commit

Permalink
because of new Stripe and Cashier stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Aug 6, 2023
1 parent 12ee1f2 commit 38af973
Show file tree
Hide file tree
Showing 39 changed files with 769 additions and 1,769 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

89 changes: 0 additions & 89 deletions app/Http/Controllers/Ajax/SubscriptionController.php

This file was deleted.

220 changes: 72 additions & 148 deletions app/Http/Controllers/User/BillingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,93 @@

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Notifications\InAppNotification;
use Exception;
use App\View\Forms\SwapForm;
use Illuminate\Http\Request;
use App\View\Forms\CouponForm;
use App\View\Forms\BillingForm;
use App\View\Forms\SubscribeForm;
use Illuminate\Support\Facades\Log;
use Throwable;
use App\Http\Controllers\Controller;

class BillingController extends Controller
{
/**
* Billing settings.
* Get subscription details.
*
* @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
* @return \Illuminate\View\View
*/
public function subscribe(Request $request)
public function index(Request $request)
{
$user = $request->user();
$upcomingPayment = null;

if ($user->hasActiveSubscription()) {
return redirect()->route('user.billing.details');
}
$user->createOrGetStripeCustomer();
$user->updateDefaultPaymentMethodFromStripe();

try {
$intent = $user->createSetupIntent();
} catch (Throwable $th) {
$intent = null;
$form = app(BillingForm::class)->setUser($user)->make();
$subscribeForm = app(SubscribeForm::class)->setUser($user)->make();
$swapForm = app(SwapForm::class)->setUser($user)->make();
$couponForm = app(CouponForm::class)->make();

if ($user->hasActiveSubscription()) {
$upcomingPayment = $user->upcomingInvoice();
}

return view('user.billing.subscribe')->with(compact('user', 'intent'));
return view('user.billing')->with([
'user' => $user,
'form' => $form,
'subscribeForm' => $subscribeForm,
'swapForm' => $swapForm,
'couponForm' => $couponForm,
'upcomingPayment' => $upcomingPayment,
]);
}

/**
* Billing renew.
*
* @return \Illuminate\View\View
*/
public function renewSubscription(Request $request)
public function update(Request $request)
{
$user = $request->user();

try {
$intent = $user->createSetupIntent();
} catch (Throwable $th) {
$intent = null;
}
$user->update([
'billing_email' => $request->billing_email,
'state' => $request->state,
'country' => $request->country,
]);

activity("Updated billing information.");

return view('user.billing.renew')->with(compact('user', 'intent'));
$user->updateStripeCustomer([
'email' => $request->billing_email,
'address' => [
'state' => $request->state,
'country' => $request->country,
],
]);

return redirect()->back()->withMessage('Information updated.');
}

/**
* Get subscription details.
*
* @return \Illuminate\View\View
*/
public function getSubscription(Request $request)
public function subscribe(Request $request)
{
$user = $request->user();

try {
$invoice = $user->upcomingInvoice();
} catch (Throwable $th) {
$invoice = null;
}
$paymentMethod = $request->user()->defaultPaymentMethod();
$plan = $request->plan;

$subscription = $user->subscription(config('billing.subscription_name'));
$request->user()->newSubscription(
'main', $plan
)->create($paymentMethod->id);

return view('user.billing.details')->with(compact('user', 'invoice', 'subscription'));
}

/**
* Change the payment method.
*
* @return \Illuminate\View\View
*/
public function paymentMethod(Request $request)
{
$user = $request->user();
activity("Subscribed to {$request->plan} subscription plan.");

try {
$intent = $user->createSetupIntent();
} catch (Throwable $th) {
$intent = null;
return redirect()
->route('user.billing')
->withMessage('You\'re subscribed!');
} catch (Exception $e) {
Log::error($e->getMessage());
}

return view('user.billing.payment-method')->with(compact('user', 'intent'));
return redirect()->back()
->withErrors(['Could not set your subscription, please try again.']);
}

/**
Expand All @@ -106,124 +108,46 @@ public function getChangePlan(Request $request)
*
* @return \Illuminate\Http\RedirectResponse
*/
public function swapPlan(Request $request)
public function swap(Request $request)
{
try {
$request->user()->subscription(config('billing.subscription_name'))->swap($request->plan);
$request->user()
->subscription(config('billing.subscription_name'))
->swap($request->plan);

activity("Switched to {$request->plan} subscription plan.");

return redirect()->route('user.billing.details')->withMessage('Your subscription was swapped!');
return redirect()->route('user.billing')
->withMessage('Your subscription was swapped!');
} catch (Exception $e) {
Log::error($e->getMessage());
}

return redirect()->back()->withErrors(['Could not change your subscription, please try again.']);
}

/**
* Add a coupon.
*
* @return \Illuminate\View\View
*/
public function getCoupon(Request $request)
{
$user = $request->user();

return view('user.billing.coupon')->with(compact('user'));
return redirect()->back()
->withErrors(['Could not change your subscription, please try again.']);
}

/**
* Apply a coupon.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function applyCoupon(Request $request)
public function coupon(Request $request)
{
try {
$request->user()->applyCoupon($request->coupon);
$coupon = $request->user()->findPromotionCode($request->coupon)->coupon();

$request->user()
->applyCoupon($coupon->id);

activity("Used coupon: {$request->coupon}.");

return redirect()->route('user.billing.details')->withMessage('Your coupon was used!');
return redirect()->route('user.billing')
->withMessage('Your coupon was used!');
} catch (Exception $e) {
Log::error($e->getMessage());
}

return redirect()->back()->withErrors(['Could not process your coupon, please try again.']);
}

/**
* Get invoices.
*
* @return \Illuminate\View\View
*/
public function getInvoices(Request $request)
{
$user = $request->user();

try {
$invoices = $user->invoices(config('billing.subscription_name'));
} catch (Throwable $th) {
$invoices = null;
}

return view('user.billing.invoices')->with(compact('user', 'invoices'));
}

/**
* Get one invoice.
*
* @return \Illuminate\Http\Response
*/
public function getInvoiceById($id, Request $request)
{
try {
$user = $request->user();

$response = $user->downloadInvoice($id, [
'vendor' => config('billing.invoice.company'),
'street' => config('billing.invoice.street'),
'location' => config('billing.invoice.location'),
'phone' => config('billing.invoice.phone'),
'url' => config('billing.invoice.url'),
'product' => config('billing.invoice.product'),
'description' => 'Subscription',
]);
} catch (Exception $e) {
$response = back()->withErrors(['Could not find this invoice, please try again.']);
}

return $response;
}

/**
* Cancel Subscription.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function cancelSubscription(Request $request)
{
try {
$user = $request->user();
$invoice = $user->upcomingInvoice();

$user->subscription(config('billing.subscription_name'))->cancel();

$date = $invoice->date()->format('Y-m-d');
$message = 'Your subscription has been cancelled! It will be availale until '.$date;

activity($message);

$notification = new InAppNotification($message);
$notification->isImportant();
$user->notify($notification);

return redirect()->route('user.billing.details')->withMessage($message);
} catch (Exception $e) {
Log::error($e->getMessage());
}

return redirect()->back()->withErrors(['Could not cancel billing, please try again.']);
}
}
19 changes: 0 additions & 19 deletions app/Http/Controllers/WebhookController.php

This file was deleted.

Loading

0 comments on commit 38af973

Please sign in to comment.