Skip to content

Instantly share code, notes, and snippets.

@harini-ua
Last active June 20, 2017 10:09
Show Gist options
  • Save harini-ua/38c7e5fc15ab235f80e60c6c8f485e49 to your computer and use it in GitHub Desktop.
Save harini-ua/38c7e5fc15ab235f80e60c6c8f485e49 to your computer and use it in GitHub Desktop.
<?php
// migrations
Schema::create('subscriptions', function ($table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->string('stripe_id');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
// UserController.php
public function getSubscription()
{
Auth::user()->fresh();
if (Auth::user()->subscribed('monthly')) {
return view('account.payments');
}
return view('account.subscription');
}
public function processSubscription(Request $request)
{
$subscription = Auth::user()->newSubscription('monthly', 'LT3-MONTHLY');
if ($request->has('coupon')) {
$subscription->withCoupon('code');
}
// Create the subscription
try {
$subscription->create($request->input('stripeToken'));
return redirect()->route('account.subscription')->with('success','You have been subscribed!' );
} catch (\Exception $e) {
return redirect()->back()->withInput()->with('error', 'Something went wrong while trying to authorise your card: '.$e->getMessage().'');
}
}
public function processCancellation()
{
Auth::user()->subscription('monthly')->cancel();
return redirect()->back()->with('success', 'Your account has been downgraded to a free plan.');
}
public function processReactivation()
{
Auth::user()->subscription('monthly')->resume();
return redirect()->back()->with('success', 'Your account has been re-activated!');
}
// User.php
public function isSubscriber() {
if (Auth::check() && Auth::user()->subscribed('monthly')) {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment