Skip to content
This repository has been archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
Billing setup
Browse files Browse the repository at this point in the history
Basic billing setup - assumes you’re using cashier
  • Loading branch information
mlantz committed Feb 29, 2016
1 parent c11ee8f commit a7ae24c
Show file tree
Hide file tree
Showing 17 changed files with 3,300 additions and 8 deletions.
113 changes: 105 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ $gate->define('team-member', function ($user, $team) {
});
```

### Migrate
You will need to migrate to add in the users, accounts, roles and teams tables. The seeding is run to set the initial roles for your application.

```php
php artisan migrate --seed
```

### Testing
You will want to create an sqlite memory test DB

Expand Down Expand Up @@ -107,15 +100,119 @@ Please consult the documentation here: [http://laracogs.com](http://laracogs.com
The commands provided by Laracogs are as follows:

#### Starter
Once you've added in all these parts you may want to run the starter for your application!

```php
php artisan laracogs:starter
```

Once the files are all set up it may be best to run: `artisan migrate`

Migrate
----
You will need to migrate to add in the users, accounts, roles and teams tables. The seeding is run to set the initial roles for your application.

```php
php artisan migrate --seed
```

#### Boostrap
Boostrap prepares your application with bootstrap as a view/ css framework

```php
php artisan laracogs:bootstrap
```

#### Billing
The billing command sets up your app with Laravel's cashier - it prepares the whole app to handle subscriptions with a policy structure.
```php
php artisan laracogs:billing
```

Requires
----
```php
composer require laravel/cashier
```

You may want to add this line to your navigation:
```php
<li><a href="{!! url('account/billing/details') !!}"><span class="fa fa-dollar"></span> Billing</a></li>
```

This to the app/Providers/ReouteServiceProvider.php:
```php
require app_path('Http/billing-routes.php');
```

This to the .env:
```php
SUBSCRIPTION=basic
STRIPE_SECRET=public-key
STRIPE_PUBLIC=secret-key
```

This to the app/Providers/ReouteServiceProvider.php:
```php
$gate->define('access-billing', function ($user) {
return ($user->account->subscribed('main') && is_null($user->account->subscription('main')->endDate));
});
```

For the `config/services.php` you will want yours to resemble:
```php
'stripe' => [
'model' => App\Repositories\Account\Account::class,
'key' => env('STRIPE_PUBLIC'),
'secret' => env('STRIPE_SECRET'),
],
```

Finally run migrate to add the subscrptions and bind them to the accounts:
```php
php artisan migrate
```

You will also want to update your gulpfile.js to include the card.js, and subscription.js
```js
elixir(function(mix) {
mix.scripts([
'app.js',
'card.js',
'subscription.js'
]);
});
```

#### Crud
The CRUD command builds a basic crud for a table
```php
php artisan laracogs:crud {table} {--migration}
```

#### Docs
The docs can prepare documentation for buisness rules or prepare your app for API doc generation with Sami.
```php
php artisan laracogs:crud {action} {name=null} {version=null}
```

## Facades/ Utilities
Laracogs provides a handful of easy to use tools outside of the app starter kit, and CRUD builder:

#### Crypto
```php
Crypto::encrypt('string');
Crypto::decrypt('enc-string');
Crypto::shared()->encrypt('string');
Crypto::shared()->decrypt('enc-string');
```

#### FormMaker
#### InputMaker
```php
FormMaker::fromTable($table, $columns = null, $class = 'form-control', $view = null, $reformatted = true, $populated = false, $idAndTimestamps = false)
FormMaker::fromObject($object, $columns = null, $view = null, $class = 'form-control', $populated = true, $reformatted = false, $idAndTimestamps = false)
FormMaker::fromArray($array, $columns = null, $view = null, $class = 'form-control', $populated = true, $reformatted = false, $idAndTimestamps = false)
```

## License
Laracogs is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
Expand Down
155 changes: 155 additions & 0 deletions src/Billing/app/Http/Controllers/Account/BillingController.php
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.']);
}
}
29 changes: 29 additions & 0 deletions src/Billing/app/Http/billing-routes.php
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');
});
});

});
});
});
33 changes: 33 additions & 0 deletions src/Billing/app/Repositories/Account/Account.php
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',
];

}
18 changes: 18 additions & 0 deletions src/Billing/config/invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
|--------------------------------------------------------------------------
| Invoice Config
|--------------------------------------------------------------------------
*/

return [

'company' => '',
'street' => '',
'location' => '',
'phone' => '',
'url' => '',
'product' => '',

];
Loading

0 comments on commit a7ae24c

Please sign in to comment.