Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Jan 8, 2024
2 parents 8ea1eda + 049aa32 commit 38ed14f
Show file tree
Hide file tree
Showing 18 changed files with 2,223 additions and 2,119 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
----

## [v2.18.0] - 2024-01-08

### Changed
- Billing system is more streamlined

### Fixed
- Some code styles

## [v2.17.2] - 2023-10-26

### Changed
Expand Down
118 changes: 15 additions & 103 deletions app/Http/Controllers/User/BillingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
namespace App\Http\Controllers\User;

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

class BillingController extends Controller
{
Expand All @@ -26,126 +20,44 @@ public function index(Request $request)
$user->createOrGetStripeCustomer();
$user->updateDefaultPaymentMethodFromStripe();

$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')->with([
'user' => $user,
'form' => $form,
'subscribeForm' => $subscribeForm,
'swapForm' => $swapForm,
'couponForm' => $couponForm,
'upcomingPayment' => $upcomingPayment,
]);
}

public function update(Request $request)
public function success(Request $request)
{
$user = $request->user();

$user->update([
'billing_email' => $request->billing_email,
'state' => $request->state,
'country' => $request->country,
]);

activity('Updated billing information.');
$user->clearSubscriptionCache();

$user->updateStripeCustomer([
'email' => $request->billing_email,
'address' => [
'state' => $request->state,
'country' => $request->country,
],
]);
activity('Subscribed.');

return redirect()->back()->withMessage('Information updated.');
return redirect()->route('user.billing')->withMessage('You\'re subscribed!');
}

public function subscribe(Request $request)
public function cancelled(Request $request)
{
try {
$paymentMethod = $request->user()->defaultPaymentMethod();
$plan = $request->plan;

$request->user()->newSubscription(
'main', $plan
)->create($paymentMethod->id);

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

$request->user()->clearSubscriptionCache();

if ($request->user()->onTrial()) {
$request->user()->update([
'trial_ends_at' => null,
]);
}

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

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

/**
* Swap subscription plans.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function swap(Request $request)
{
try {
$request->user()
->subscription(config('billing.subscription_name'))
->swap($request->plan);

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

$request->user()->clearSubscriptionCache();
$user->clearSubscriptionCache();

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

return redirect()->back()
->withErrors(['Could not change your subscription, please try again.']);
return redirect()->route('user.billing')->withMessage('You cancelled! Let us know if you need help.');
}

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

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

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

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.']);
return $request->user()->checkout([$request->plan], [
'success_url' => route('user.billing.subscribe.success'),
'cancel_url' => route('user.billing.subscribe.cancelled'),
'mode' => 'subscription',
]);
}
}
3 changes: 0 additions & 3 deletions app/Http/Controllers/User/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public function update(UserUpdateRequest $request)
'email' => $request->email,
'avatar' => UpdateUserAvatar::handle($request) ?? $request->user()->avatar,
'allow_email_based_notifications' => $request->filled('allow_email_based_notifications') ?? false,
'billing_email' => $request->billing_email,
'state' => $request->state,
'country' => $request->country,
]);

activity('Settings updated.');
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

class Team extends Model
{
use Invitable;
use HasAvatar;
use HasFactory;
use HasForm;
use HasSubscribedUser;
use Invitable;

public $form = TeamForm::class;

Expand Down
22 changes: 11 additions & 11 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
class User extends Authenticatable implements MustVerifyEmail
{
use Billable;
use Notifiable;
use HasTeams;
use HasForm;
use CanAccessFeatures;
use DatabaseSearchable;
use HasActivity;
use HasAvatar;
use HasApiTokens;
use HasAvatar;
use HasCachedValues;
use HasSubscription;
use HasRoles;
use HasPermissions;
use HasFactory;
use HasTwoFactor;
use DatabaseSearchable;
use HasSessions;
use CanAccessFeatures;
use HasForm;
use HasJavascriptData;
use HasPermissions;
use HasRoles;
use HasSessions;
use HasSubscription;
use HasTeams;
use HasTwoFactor;
use Notifiable;

public $form = UserForm::class;

Expand Down
58 changes: 0 additions & 58 deletions app/View/Forms/BillingForm.php

This file was deleted.

34 changes: 0 additions & 34 deletions app/View/Forms/CouponForm.php

This file was deleted.

45 changes: 0 additions & 45 deletions app/View/Forms/SubscribeForm.php

This file was deleted.

Loading

0 comments on commit 38ed14f

Please sign in to comment.