/** * Represents the "receive_authorization" action (OAUTH ONLY!). * Please make sure to allow calls to this action only for admin users with proper rights (only call inside of the * admin area or check for admin-login set when providing an action from outside of the admin area) * @see ShopgatePlugin::checkAdminLogin method * * @throws ShopgateLibraryException * @see http://wiki.shopgate.com/Shopgate_Plugin_API_receive_authorization */ protected function receiveAuthorization() { if ($this->config->getSmaAuthServiceClassName() != ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_OAUTH) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_INVALID_ACTION, '=> "receive_authorization" action can only be called for plugins with SMA-AuthService set to "OAuth" type', true); } if (empty($this->params['code'])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_AUTHORIZATION_CODE); } $tokenRequestUrl = $this->buildShopgateOAuthUrl('token'); // the "receive_authorization" action url is needed (again) for requesting an access token $calledScriptUrl = $this->plugin->getActionUrl($this->params['action']); // Re-initialize the OAuth auth service object and the ShopgateMerchantAPI object $smaAuthService = new ShopgateAuthenticationServiceOAuth(); $accessToken = $smaAuthService->requestOAuthAccessToken($this->params['code'], $calledScriptUrl, $tokenRequestUrl); // at this Point there is a valid access token available, since this point would not be reached otherwise // -> get a new ShopgateMerchantApi object, containing a fully configured OAuth auth service including the access token $this->merchantApi = new ShopgateMerchantApi($smaAuthService, null, $this->config->getApiUrl()); // load all shop info via the MerchantAPI and store it in the config (via OAuth and a valid access token) $shopInfo = $this->merchantApi->getShopInfo()->getData(); if (empty($shopInfo)) { throw new ShopgateLibraryException(ShopgateLibraryException::MERCHANT_API_INVALID_RESPONSE, '-> "shop info" not set. Response data: ' . var_export($shopInfo, true)); } // create a new settings array $shopgateSettingsNew = array($field = 'oauth_access_token' => $shopInfo[$field], $field = 'customer_number' => $shopInfo[$field], $field = 'shop_number' => $shopInfo[$field], $field = 'apikey' => $shopInfo[$field], $field = 'alias' => $shopInfo[$field], $field = 'cname' => $shopInfo[$field]); // save all shop config data to plugin-config using the configs save method $this->config->load($shopgateSettingsNew); $this->config->save(array_keys($shopgateSettingsNew), true); // no specific data needs to be returned if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id); } $this->responseData = array(); }
/** * Represents the "get_reviews_csv" action. * * @throws ShopgateLibraryException * @see http://wiki.shopgate.com/Shopgate_Plugin_API_get_reviews_csv/ */ protected function getReviewsCsv() { if (isset($this->params['limit']) && isset($this->params['offset'])) { $this->plugin->setExportLimit((int) $this->params['limit']); $this->plugin->setExportOffset((int) $this->params['offset']); $this->plugin->setSplittedExport(true); } // generate / update reviews csv file $this->plugin->startGetReviewsCsv(); if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseTextCsv($this->trace_id); } $this->responseData = $this->config->getReviewsCsvPath(); }
/** * Builds the Shopgate Library object graph for a given ShopgatePlugin object. * * This initializes all necessary objects of the library, wires them together and injects them into * the plugin class via its set* methods. * * @param ShopgatePlugin $plugin The ShopgatePlugin instance that should be wired to the framework. */ public function buildLibraryFor(ShopgatePlugin $plugin) { // set error handler if configured if ($this->config->getUseCustomErrorHandler()) { set_error_handler('ShopgateErrorHandler'); } // instantiate API stuff // -> MerchantAPI auth service (needs to be initialized first, since the config still can change along with the authentication information switch ($this->config->getSmaAuthServiceClassName()) { case ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_SHOPGATE: $smaAuthService = new ShopgateAuthenticationServiceShopgate($this->config->getCustomerNumber(), $this->config->getApikey()); $smaAuthService->setup($this->config); $merchantApi = new ShopgateMerchantApi($smaAuthService, $this->config->getShopNumber(), $this->config->getApiUrl()); break; case ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_OAUTH: $smaAuthService = new ShopgateAuthenticationServiceOAuth($this->config->getOauthAccessToken()); $smaAuthService->setup($this->config); $merchantApi = new ShopgateMerchantApi($smaAuthService, null, $this->config->getApiUrl()); break; default: // undefined auth service return trigger_error('Invalid SMA-Auth-Service defined - this should not happen with valid plugin code', E_USER_ERROR); } // -> PluginAPI auth service (currently the plugin API supports only one auth service) $spaAuthService = new ShopgateAuthenticationServiceShopgate($this->config->getCustomerNumber(), $this->config->getApikey()); $pluginApi = new ShopgatePluginApi($this->config, $spaAuthService, $merchantApi, $plugin); if ($this->config->getExportConvertEncoding()) { array_splice(ShopgateObject::$sourceEncodings, 1, 0, $this->config->getEncoding()); ShopgateObject::$sourceEncodings = array_unique(ShopgateObject::$sourceEncodings); } if ($this->config->getForceSourceEncoding()) { ShopgateObject::$sourceEncodings = array($this->config->getEncoding()); } // instantiate export file buffer if (!empty($_REQUEST['action']) && ($_REQUEST['action'] == 'get_items' || $_REQUEST['action'] == 'get_categories' || $_REQUEST['action'] == 'get_reviews')) { $xmlModelNames = array('get_items' => 'Shopgate_Model_Catalog_Product', 'get_categories' => 'Shopgate_Model_Catalog_Category', 'get_reviews' => 'Shopgate_Model_Review'); $format = !empty($_REQUEST['response_type']) ? $_REQUEST['response_type'] : ''; switch ($format) { default: case 'xml': /* @var $xmlModel Shopgate_Model_AbstractExport */ $xmlModel = new $xmlModelNames[$_REQUEST['action']](); $xmlNode = new Shopgate_Model_XmlResultObject($xmlModel->getItemNodeIdentifier()); $fileBuffer = new ShopgateFileBufferXml($xmlModel, $xmlNode, $this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings); break; case 'json': $fileBuffer = new ShopgateFileBufferJson($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings); break; } } else { if (!empty($_REQUEST['action']) && ($_REQUEST['action'] == 'get_items_csv' || $_REQUEST['action'] == 'get_categories_csv' || $_REQUEST['action'] == 'get_reviews_csv')) { $fileBuffer = new ShopgateFileBufferCsv($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings); } else { $fileBuffer = new ShopgateFileBufferCsv($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding(), ShopgateObject::$sourceEncodings); } } // inject apis into plugin $plugin->setConfig($this->config); $plugin->setMerchantApi($merchantApi); $plugin->setPluginApi($pluginApi); $plugin->setBuffer($fileBuffer); }
/** * Builds the Shopgate Library object graph for a given ShopgatePlugin object. * * This initializes all necessary objects of the library, wires them together and injects them into * the plugin class via its set* methods. * * @param ShopgatePlugin $plugin The ShopgatePlugin instance that should be wired to the framework. */ public function buildLibraryFor(ShopgatePlugin $plugin) { // set error handler if configured if ($this->config->getUseCustomErrorHandler()) { set_error_handler('ShopgateErrorHandler'); } // instantiate API stuff $authService = new ShopgateAuthentificationService($this->config->getCustomerNumber(), $this->config->getApikey()); $merchantApi = new ShopgateMerchantApi($authService, $this->config->getShopNumber(), $this->config->getApiUrl()); $pluginApi = new ShopgatePluginApi($this->config, $authService, $merchantApi, $plugin); // instantiate export file buffer $fileBuffer = new ShopgateFileBuffer($this->config->getExportBufferCapacity(), $this->config->getExportConvertEncoding()); // inject apis into plugin $plugin->setConfig($this->config); $plugin->setMerchantApi($merchantApi); $plugin->setPluginApi($pluginApi); $plugin->setBuffer($fileBuffer); }
/** * get additional data from the magento instance * * @return array|mixed[] */ public function createShopInfo() { $shopInfo = parent::createShopInfo(); $entitiesCount = $this->_getHelper()->getEntitiesCount($this->config->getStoreViewId()); $pluginsInstalled = array('plugins_installed' => $this->_getHelper()->getThirdPartyModules()); return array_merge($shopInfo, $entitiesCount, $pluginsInstalled); }