Skip to content

Commit

Permalink
App installation process with external services creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Menno committed May 27, 2015
1 parent 8fcbec8 commit c91e914
Show file tree
Hide file tree
Showing 25 changed files with 363 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

SEOSHOP_ENV=live
SEOSHOP_KEY=your app key goes here
SEOSHOP_SECRET=your app secret goes here

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Expand Down
20 changes: 18 additions & 2 deletions app/Helpers/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static function payload($file)
{
if (preg_match('/^([a-zA-Z0-9\_]+)$/', $file))
{
return file_get_contents(base_path().'/resources/json/'.$file.'_payload.json');
return file_get_contents(base_path().'/resources/examples/'.$file.'_payload.json');
}

return 'Incorrect filename';
Expand All @@ -28,7 +28,23 @@ public static function response($file)
{
if (preg_match('/^([a-zA-Z0-9\_]+)$/', $file))
{
return file_get_contents(base_path().'/resources/json/'.$file.'_response.json');
return file_get_contents(base_path().'/resources/examples/'.$file.'_response.json');
}

return 'Incorrect filename';
}

/**
* Fetch a PHP example
*
* @param $file
* @return string
*/
public static function example($file)
{
if (preg_match('/^([a-zA-Z0-9\_]+)$/', $file))
{
return file_get_contents(base_path().'/resources/examples/'.$file.'_example.php');
}

return 'Incorrect filename';
Expand Down
66 changes: 64 additions & 2 deletions app/Http/Controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\libraries\Webshop;
use App\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AppController extends Controller {

Expand All @@ -17,14 +20,73 @@ public function home()
return view('app/home');
}

/**
* The dashboard of the logged-in user
*
* @return \Illuminate\View\View
*/
public function dashboard()
{
$externalServices = Webshop::instance()->external_services->get();

return view('app/dashboard', [
'external_services' => $externalServices
]);
}

/**
* This url is called when a SEOshop customer installs this application
*
* @param Request $request
* @return \Illuminate\View\View
* @throws \Exception
*/
public function install()
public function install(Request $request)
{
return view('app/install');
// Make sure we have received all required information
$this->validate($request, [
'language' => 'required',
'shop_id' => 'required',
'signature' => 'required',
'timestamp' => 'required',
'token' => 'required'
]);

// Validate the signature
$signature = '';
$input = $request->except('signature');

ksort($input);

// Construct the signature
foreach ($input as $key => $value)
{
$signature .= $key . '=' . $value;
}

// The signature contains the app secret
$signature = md5($signature . config('services.seoshop.secret'));

// Do the signatures match?
if ($signature != $request->input('signature'))
{
throw new \Exception('The signature does not match. You haven\'t secretly tampered with it no?');
}

// Find or create the user
$shop = Shop::firstOrNew(array('shop_id' => $request->input('shop_id')));
$shop->language = $request->input('language');
$shop->token = $request->input('token');
$shop->save();

// Authenticate the user
Auth::loginUsingId($shop->id);

// Create the external services
Webshop::instance()->installExternalServices();

// Were done here
return redirect('dashboard');
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
Route::post('/payment', 'PaymentsController@create');
Route::get('/payment/{id}', 'PaymentsController@one');

// Authenticated routes
Route::group(['middleware' => ['auth']], function()
{
Route::get('/dashboard', 'AppController@dashboard');
});

// Other routes
Route::get('/payment/pay/{id}/{status}', 'PaymentsController@update');
Expand Down
71 changes: 71 additions & 0 deletions app/Libraries/Webshop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php namespace App\libraries;

use Illuminate\Support\Facades\Auth;

class Webshop extends \WebshopappApiClient
{
protected static $instance;

public function __construct()
{
$config = config('services.seoshop');

return parent::__construct($config['env'], $config['key'], md5(Auth::user()->token . $config['secret']), Auth::user()->language);
}

public static function instance()
{
if (self::$instance)
{
return self::$instance;
}

return self::$instance = new self();
}

/**
* This method will fetch the current external services
* If these do not exist yet it will create them
*/
public function installExternalServices()
{
$currentServices = Webshop::instance()->external_services->get();

$hasShipmentService = false;
$hasPaymentService = false;

foreach ($currentServices as $currentService)
{
if ($currentService['type'] == 'shipment')
{
$hasShipmentService = true;
}

if ($currentService['type'] == 'payment')
{
$hasPaymentService = true;
}
}

if ($hasShipmentService === false)
{
Webshop::instance()->external_services->create([
'type' => 'shipment',
'name' => 'SEOshop Shipments',
'urlEndpoint' => url('/', [], true),
'rateEstimate' => true,
'isActive' => true
]);
}

if ($hasPaymentService === false)
{
Webshop::instance()->external_services->create([
'type' => 'payment',
'name' => 'SEOshop Payments',
'urlEndpoint' => url('/', [], true),
'isActive' => true
]);
}
}
}
14 changes: 14 additions & 0 deletions app/Shop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class Shop extends Model implements AuthenticatableContract {

use Authenticatable;

protected $table = 'shops';

protected $fillable = ['shop_id'];
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"type": "project",
"require": {
"laravel/framework": "5.0.*"
"laravel/framework": "5.0.*",
"seoshop/seoshop-php": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
|
*/

'model' => 'App\User',
'model' => 'App\Shop',

/*
|--------------------------------------------------------------------------
Expand All @@ -41,7 +41,7 @@
|
*/

'table' => 'users',
'table' => 'shops',

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

'seoshop' => [
'env' => env('SEOSHOP_ENV', 'live'),
'key' => env('SEOSHOP_KEY', false),
'secret' => env('SEOSHOP_SECRET', false)
]
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2015_05_27_194139_create_shops_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateShopsTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shops', function(Blueprint $table)
{
$table->increments('id');
$table->integer('shop_id');
$table->string('token');
$table->string('language');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('shops');
}

}
6 changes: 3 additions & 3 deletions public/css/app.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c91e914

Please sign in to comment.