/** * 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'])); } } }
/** * @before _secure * @after _csrfToken */ public function settings() { $this->seo(array("title" => "Settings")); $view = $this->getActionView(); $user = $this->user; $org = $this->org; $search = ['prop' => 'customField', 'propid' => $org->_id]; $meta = Meta::first($search) ?? (object) []; $view->set('fields', $meta->value ?? []); $apikey = ApiKey::first(["org_id = ?" => $org->id]); $mailConf = Meta::first(['prop' => 'orgSmtp', 'propid' => $this->org->_id]) ?? (object) []; $view->set('mailConf', $mailConf->value ?? [])->set("errors", []); if (RM::type() == 'POST') { $action = RM::post('action', ''); switch ($action) { case 'account': $user->name = RM::post('name'); $user->currency = RM::post('currency', 'INR'); $user->region = ["currency" => RM::post('currency', 'INR'), "zone" => RM::post('timezone', 'Asia/Kolkata')]; $user->phone = RM::post('phone'); $user->save(); $view->set('message', 'Account Updated!!'); break; case 'password': $old = RM::post('password'); $new = RM::post('npassword'); $view->set($user->updatePassword($old, $new)); break; case 'billing': $billing = $org->billing; $billing["aff"]["auto"] = RM::post("autoinvoice", 0); $billing["aff"]["freq"] = RM::post("freq", 15); $billing["aff"]["minpay"] = $this->currency(RM::post('minpay', 100)); $billing["aff"]["ptypes"] = RM::post("ptypes"); $billing["adv"]["paypal"] = RM::post("paypal"); $org->billing = $billing; $org->save(); $this->setOrg($org); $view->set('message', 'Organization Billing Updated!!'); break; case 'org': $meta = $org->meta; if (RM::post("widgets")) { $meta["widgets"] = RM::post("widgets"); $org->meta = $meta; } $zopim = RM::post("zopim"); $meta["zopim"] = $zopim; if (strlen($zopim) == 0) { unset($meta["zopim"]); } $org->name = RM::post('name'); $org->meta = $meta; $org->logo = $this->_upload('logo'); $org->url = RM::post('url'); $org->email = RM::post('email'); $org->save(); $this->setOrg($org); $view->set('message', 'Network Settings updated!!'); break; case 'customField': $label = RM::post("fname"); $type = RM::post("ftype", "text"); $required = RM::post("frequired", 1); $name = strtolower(str_replace(" ", "_", $label)); $field = ['label' => ucwords($label), 'type' => $type, 'name' => $name, 'required' => (bool) $required]; if (!$label) { break; } if (!is_object($meta) || !is_a($meta, 'Meta')) { $meta = new Meta($search); } $fields = $meta->value; $fields[] = $field; $meta->value = $fields; $meta->save(); $view->set('fields', $meta->value ?? []); $view->set('message', 'Extra Field Added!!'); break; case 'smtp': $msg = \Shared\Services\Smtp::create($this->org); $view->set('message', $msg); break; case 'apikey': $view->set('message', "Api Key Updated!!"); if (!$apikey) { $apikey = new ApiKey(['org_id' => $this->org->_id, 'key' => uniqid() . uniqid() . uniqid()]); $view->set('message', "Api Key Created!!"); } $apikey->updateIps(); $apikey->save(); break; } $this->setUser($user); } $view->set("apiKey", $apikey); if (RM::type() === 'DELETE') { if (is_a($meta, 'Meta')) { $meta->delete(); } $view->set('message', 'Extra Fields removed!!'); } $img = RM::get("img"); if (RM::get("action") == "removelogo" && $img === $org->logo) { Utils::media($org->logo, 'remove'); $org->logo = ' '; $this->setOrg($org); $org->save(); $this->redirect("/admin/settings.html"); } }