Skip to content

Commit

Permalink
Fix applying coupons for early supporters when creating a subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
V13Axel committed Dec 9, 2024
1 parent f0e8b66 commit 4541224
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions app/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
use App\Events\UserSubscribedEvent;
use Stripe;
use Illuminate\Http\Request;
use Laravel\Cashier\Coupon;
use Laravel\Cashier\Exceptions\IncompletePayment;
use Laravel\Cashier\Subscription;

class SubscriptionController extends Controller
{
public function __construct() {
public function __construct()
{
$this->middleware('auth')->except(['pricing']);
}

/**
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function pricing(Request $request) {
public function pricing(Request $request)
{
return view('subscription.pricing', [
'subscribed' => $request->user()?->subscriptions()->active()->count(),
'earlySupporter' => $request->user()?->isEarlySupporter(),
Expand All @@ -30,22 +34,30 @@ public function pricing(Request $request) {
* @param $interval
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function subscribe(Request $request, $level, $interval) {
public function subscribe(Request $request, $level, $interval)
{
$stripeCustomer = auth()->user()->createOrGetStripeCustomer();
$stripe = new Stripe\StripeClient(config('cashier.secret'));

if ($stripe->subscriptions->all(['customer' => $stripeCustomer->id])->count()) {
redirect()->route('profile.billing'); // They're subscribed already, send 'em to the subscriptions list;
}

return $request->user()
$subscription = $request->user()
->newSubscription(
$level,
strtolower($level . "_" . $interval)
)->checkout([
'success_url' => route('profile.billing'),
'cancel_url' => route('profile.billing')
]);
);

if ($request->user()->is_early_supporter) {
$coupon = $request->user()->findPromotionCode('EarlySupporter')->coupon();
$subscription = $subscription->withCoupon($coupon->asStripeCoupon()->id);
}

return $subscription->checkout([
'success_url' => route('profile.billing'),
'cancel_url' => route('profile.billing')
]);
}

/**
Expand All @@ -55,7 +67,8 @@ public function subscribe(Request $request, $level, $interval) {
* @return array|\Illuminate\Http\RedirectResponse
* @throws Stripe\Exception\ApiErrorException
*/
public function update(Request $request, $level, $plan) {
public function update(Request $request, $level, $plan)
{
try {
$request->user()->startSubscription($level, $plan, $request->input('token'));
} catch (IncompletePayment $exception) {
Expand All @@ -73,10 +86,11 @@ public function update(Request $request, $level, $plan) {
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function cancel(Request $request) {
public function cancel(Request $request)
{
$subscription = $request->user()->subscription('Timekeeper');

if($subscription->onGracePeriod()) {
if ($subscription->onGracePeriod()) {
$subscription->cancelNow();
} else {
$subscription->cancel();
Expand All @@ -88,8 +102,9 @@ public function cancel(Request $request) {
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function resume(Request $request) {
if($request->user()->subscription('Timekeeper')->onGracePeriod()) {
public function resume(Request $request)
{
if ($request->user()->subscription('Timekeeper')->onGracePeriod()) {
$request->user()->subscription('Timekeeper')->resume();
}

Expand Down

0 comments on commit 4541224

Please sign in to comment.