BitaG
2/26/2019 - 1:48 PM

Insale integration

<?php

Route::get('/', function () {
    return view('home');
});

// install router
Route::get('/install',
    [
        'as'    => 'install',
        'uses'  => 'Insale@install'
    ]
);

// uninstall router
Route::get('/uninstall',
    [
        'as'    => 'uninstall',
        'uses'  => 'Insale@uninstall'
    ]
);

// login router
Route::get('/login',
    [
        'as'    => 'login',
        'uses'  => 'Insale@login'
    ]
);

Route::get('/autologin',
    [
        'as'    => 'autologin',
        'uses'  => 'Insale@autologin'
    ]
);

Route::post('/webhook',
    [
        'as'    => 'webhook',
        'uses'  => 'Bomber@webHook'
    ]);

Route::get('error/{code?}',
    [
        'as'    => 'error',
        'uses'  => 'Error@show'
    ]
);
<?php

namespace App\Http\Controllers;
use App\Shop;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Cookie;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Log;

use Illuminate\Routing\Controller;
use App\Http\Controllers\Error;
use Illuminate\Support\Facades\DB;

use GuzzleHttp\Client;
use App\Classes\InsaleClient;
use App\Classes\InsaleWebHook;

class Insale extends Controller
{

    public function install(Request $request)
    {
        $shopId = $request->insales_id;
        if ( $shopId === null )
        {
            return redirect()->route('error');
        }

        $insale_client  = new InsaleClient();
        $shop = Shop::where('shop_id',$shopId)->value('id');

        if( $shop === null )
        {
            $newShop = new Shop;
            $newShop->shop_id   = $shopId;
            $newShop->password  = $insale_client->generatePass( $request->token );
            $newShop->shop_url  = $request->shop;
            $newShop->save();

            Log::info('Add shop: '.$shopId);
        }
    }

    public function uninstall(Request $request)
    {

        $shopId     = $request->insales_id;
        if ( $shopId === null )
        {
            return redirect()->route('error');
        }

        $shop = Shop::where('shop_id', $shopId)->first();
        if ($shop->webhook != null){

            $insaleClient   = new InsaleClient();
            $insaleWebhook  = new InsaleWebHook($shop, $insaleClient->getAppName());
            $insaleWebhook->deleteWebHook();
        }

        Log::info('Delete shop: '.$shopId);
        $shop->delete();
    }

    public function login(Request $request)
    {
        $sessionShopId = $request->session()->get('shopId');
        $cookieShopId = $request->cookie('shopId');
        $shopId = $request->insales_id;

        if ( $shopId === null )
        {
            return redirect()->route('error');
        }

        $insale_client  = new InsaleClient();
        $shop           = Shop::where('shop_id', $shopId)->first();

        //setWebHook
        if( $shop->webhook === null )
        {

            $insaleClient   = new InsaleClient();
            $insaleWebhook  = new InsaleWebHook($shop, $insaleClient->getAppName());

            if( $insaleWebhook->installWebHook(url('/')) )
            {
                $shop->webhook = $insaleWebhook->webhookId;
                $shop->save();
            }
        }


        if ( !empty($sessionShopId) && !empty($cookieShopId))
        {
            if ($sessionShopId === $cookieShopId)
            {
                return redirect()->route('devices');
            }
        }

        $token = md5(microtime().md5(microtime()));

        $url = sprintf('http://%s/admin/applications/%s/login?token=%s&login=%s/autologin',
            $request->shop,
            $insale_client->getAppName(),
            $token,
            url('/')
        );

        $request->session()->put('token', $token);
        $request->session()->put('shopId', $shopId);

        return redirect($url);
    }

    public function autologin( Request $request)
    {

        $token3         = $request->token3;
        $shopId         = $request->session()->get('shopId');
        $password       = Shop::where('shop_id', $shopId)->value('password');

        //collect string
        $string =
            $request->session()->get('token').
            $request->user_email.
            $request->user_name.
            $request->user_id.
            $request->email_confirmed.
            $password;

        if ($token3 == md5($string))
        {
            Cookie::queue('shopId', $shopId, 60);
            return redirect()->route('devices');
        }

        return redirect()->route('error');
    }

}
<?php
namespace App\Classes;

class InsaleClient
{
    //app config
    private $app_name   = 'bomber_bit';
    private $app_secret = '7d269d55756b5cc7b286a02a860c66f8';

    public function generatePass( $token )
    {
        return md5($token.$this->app_secret);
    }

    public function getAppSecret()
    {
        return $this->app_secret;
    }

    public function getAppName()
    {
        return $this->app_name;
    }
}
<?php
namespace App\Classes;

use GuzzleHttp\Client;

class InsaleWebHook
{
   private $client;
   private $appName;
   private $shop;
   public  $webhookId;

   public function __construct($shop, $appName)
   {
       $this->shop      = $shop;
       $this->appName   = $appName;
       $this->webhookId = $shop->webhook;
       $baseUrl = 'http://'.$appName.':'.$shop->password.'@'.$shop->shop_url;
       $this->client  = new Client( ['base_uri' => $baseUrl ] );

   }


    public function installWebHook($hostUrl)
    {
        $url  =  $hostUrl.'/webhook';

        $responce = $this->client->request('POST', '/admin/webhooks.json', [
            'json'=>
                [
                    'address'=>$url,
                    'topic'=>'orders/create',
                    'format-type'=>'json'
                ]
        ]);

        $statusCode = $responce->getStatusCode();

        if ($statusCode != 201){
            return false;
        }

        $body = $responce->getBody()->getContents();
        $data = json_decode($body,true);//get assoc array`s
        $this->webhookId = $data['id'];

        return true;
    }

    public function deleteWebHook()
    {
        $id = $this->webhookId;
        $responce   = $this->client->request('DELETE', '/admin/webhooks/'.$id.'.json');
    }

    public function getWebHook()
    {

        $responce   = $this->client->request('GET', '/admin/webhooks.json');
        $body       = $responce->getBody()->getContents();

        var_dump($body);

    }


}