This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic billing setup - assumes you’re using cashier
- Loading branch information
Showing
17 changed files
with
3,300 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/Billing/app/Http/Controllers/Account/BillingController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Account; | ||
|
||
use Auth; | ||
use Activity; | ||
use Carbon\Carbon; | ||
use App\Http\Requests; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
|
||
class BillingController extends Controller | ||
{ | ||
/** | ||
* Billing selection | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function getSubscribe(Request $request) | ||
{ | ||
$user = $request->user(); | ||
$invoice = $user->account->upcomingInvoice(); | ||
|
||
if ($user->account->subscribed('main') && ! is_null($invoice)) { | ||
return view('billing.details') | ||
->with('invoice', $invoice) | ||
->with('invoiceDate', Carbon::createFromTimestamp($invoice->date)) | ||
->with('account', $user); | ||
} | ||
|
||
return view('billing.subscribe') | ||
->with('account', $user); | ||
} | ||
|
||
/** | ||
* Create a subscription | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function postSubscribe(Request $request) | ||
{ | ||
$inputs = $request->all(); | ||
$creditCardToken = $inputs['stripeToken']; | ||
|
||
try { | ||
Auth::user()->account->newSubscription('main', env('SUBSCRIPTION'))->create($creditCardToken); | ||
return redirect('account/billing/details')->with('message', ['success', 'You\'re now subscribed!']); | ||
} catch (Exception $e) { | ||
throw new Exception("Could not process the billing please try again.", 1); | ||
} | ||
|
||
return back()->withErrors(['Could not complete billing, please try again.']); | ||
} | ||
|
||
/** | ||
* change a credit card | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function getChangeCard(Request $request) | ||
{ | ||
$user = $request->user(); | ||
|
||
return view('billing.change-card') | ||
->with('account', $user); | ||
} | ||
|
||
/** | ||
* Save new credit card | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function postChangeCard(Request $request) | ||
{ | ||
$inputs = $request->all(); | ||
$creditCardToken = $inputs['stripeToken']; | ||
|
||
try { | ||
Auth::user()->account->updateCard($creditCardToken); | ||
return redirect('account/billing/details')->with('message', ['success', 'Your subscription has been updated!']); | ||
} catch (Exception $e) { | ||
throw new Exception("Could not process the billing please try again.", 1); | ||
} | ||
|
||
return back()->withErrors(['Could not complete billing, please try again.']); | ||
} | ||
|
||
/** | ||
* Get invoices | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function getInvoices(Request $request) | ||
{ | ||
$user = $request->user(); | ||
$invoices = $user->account->invoices('main'); | ||
|
||
return view('billing.invoices') | ||
->with('invoices', $invoices) | ||
->with('account', $user); | ||
} | ||
|
||
/** | ||
* Get one invoice | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function getInvoiceById($id, Request $request) | ||
{ | ||
$user = $request->user(); | ||
|
||
try { | ||
$response = $user->account->downloadInvoice($id, [ | ||
'vendor' => config("invoice.company"), | ||
'street' => config("invoice.street"), | ||
'location' => config("invoice.location"), | ||
'phone' => config("invoice.phone"), | ||
'url' => config("invoice.url"), | ||
'product' => config("invoice.product"), | ||
'description' => 'Subscription', | ||
]); | ||
} catch (Exception $e) { | ||
$response = back()->withErrors(['Could not find this invoice, please try again.']); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Cancel Subscription | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function cancelSubscription(Request $request) | ||
{ | ||
$user = $request->user(); | ||
$invoice = $user->account->upcomingInvoice(); | ||
$date = Carbon::createFromTimestamp($invoice->date); | ||
|
||
try { | ||
$user->account->subscription('main')->cancel(); | ||
return redirect('account/billing/details')->with('message', ['success', 'Your subscription has been cancelled! It will be availale until '.$date]); | ||
} catch (Exception $e) { | ||
throw new Exception("Could not process the billing please try again.", 1); | ||
} | ||
|
||
return back()->withErrors(['Could not complete billing, please try again.']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Billing Routes | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
Route::group(['middleware' => 'web'], function() { | ||
Route::group(['middleware' => 'auth'], function(){ | ||
Route::group(['prefix' => 'account', 'namespace' => 'Account'], function(){ | ||
|
||
Route::group(['prefix' => 'billing'], function() { | ||
Route::get('details', 'BillingController@getSubscribe'); | ||
Route::post('subscribe', 'BillingController@postSubscribe'); | ||
Route::group(['gateway' => 'access-billing'], function() { | ||
Route::get('change-card', 'BillingController@getChangeCard'); | ||
Route::post('change-card', 'BillingController@postChangeCard'); | ||
Route::get('cancellation', 'BillingController@cancelSubscription'); | ||
Route::get('invoices', 'BillingController@getInvoices'); | ||
Route::get('invoice/{id}', 'BillingController@getInvoiceById'); | ||
Route::get('coupon', 'BillingController@getCoupon'); | ||
Route::post('coupon', 'BillingController@postCoupon'); | ||
}); | ||
}); | ||
|
||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Repositories\Account; | ||
|
||
use Eloquent; | ||
use Laravel\Cashier\Billable; | ||
|
||
class Account extends Eloquent | ||
{ | ||
use Billable; | ||
|
||
/** | ||
* The database table used by the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'accounts'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'user_id', | ||
'phone', | ||
'marketing', | ||
'stripe_id', | ||
'card_brand', | ||
'card_last_four', | ||
]; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Invoice Config | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
return [ | ||
|
||
'company' => '', | ||
'street' => '', | ||
'location' => '', | ||
'phone' => '', | ||
'url' => '', | ||
'product' => '', | ||
|
||
]; |
Oops, something went wrong.