/** * Execute the console command. * * @return mixed */ public function fire() { // $shop_id = $this->argument('shop'); if (!$shop_id) { return $this->error('You must provide a shop ID.'); } $shop = Shop::where('id', $shop_id)->first(); if (!$shop) { return $this->error('The shop ID you provided is invalid.'); } $access_title = $this->argument('access_title'); if (!$access_title) { $access_title = 'Free'; } $accessLevel = AccessLevel::where('title', $access_title)->first(); if (!$accessLevel) { return $this->error('The access level you provided is invalid.'); } $apiKey = new ApiKey(); $apiKey->shop_id = $shop_id; $apiKey->public_key = Hash::make($shop_id . 'REMEDY'); $apiKey->access_level_id = $accessLevel->id; $apiKey->save(); $this->info('The generated API key is:'); return $this->info($apiKey->public_key); }
/** * Main Controller Method for Shopify Authorization */ public function installOrAuthenticate() { if (Input::get('code')) { // New install Log::info('New Install: ' . Input::get('shop')); $sh = App::make('ShopifyAPI', ['API_KEY' => Config::get('shopify.APP_API_KEY'), 'API_SECRET' => Config::get('shopify.APP_API_SECRET'), 'SHOP_DOMAIN' => Input::get('shop')]); // Get Access Token try { $accessToken = $sh->getAccessToken(Input::get('code')); } catch (Exception $e) { Log::error($e->getMessage()); die('<pre>Error: ' . $e->getMessage() . '</pre>'); } $shop = Shop::where('domain', Input::get('shop'))->first(); if (!$shop) { //Log::info(__LINE__ . ': New Shop'); $shop = new Shop(); } $shop->setDomain(Input::get('shop')); $shop->setAccessToken($accessToken); $shop->save(); $this->updateShopInfo($shop); /** * Create the shop's first api key automatically, on install */ $apiKey = new ApiKey(); $apiKey->shop_id = $shop->id; $apiKey->public_key = Hash::make($shop->id . 'REMEDY'); $apiKey->access_level_id = AccessLevel::where('title', 'Free Plus')->first()->id; $apiKey->save(); /** * Create webhook for uninstall */ $hookData = array('webhook' => array('topic' => 'app/uninstalled', 'address' => 'https://' . $_ENV['HOST'] . '/uninstall-hook', 'format' => 'json')); try { $sh->setup(['ACCESS_TOKEN' => $shop->getAccessToken()]); $sh->call(['URL' => 'webhooks.json', 'METHOD' => 'POST', 'DATA' => $hookData]); } catch (Exception $e) { Log::error('Issue creating uninstall webhook - ' . $shop->domain . ' : ' . $e->getMessage()); } Session::put('shop', $shop->domain); return Redirect::to('/'); } else { // Accessing app from apps screen $shop = Shop::where('domain', Input::get('shop'))->first(); if ($shop) { Log::info('Shop found after Auth: ' . Input::get('shop')); $this->updateShopInfo($shop); Session::put('shop', Input::get('shop')); return Redirect::to('/'); } else { Log::warning('Shop redirecting to install: ' . Input::get('shop')); $sh = App::make('ShopifyAPI', ['API_KEY' => Config::get('shopify.APP_API_KEY'), 'SHOP_DOMAIN' => Input::get('shop')]); return Redirect::to($sh->installURL(['permissions' => Config::get('shopify.APP_API_SCOPE'), 'redirect' => 'https://' . $_ENV['HOST'] . '/auth'])); } } }