/** * @param ShopgateConfigInterface $config */ public function setup(ShopgateConfigInterface $config) { // needs to check if an old config is present without any access token if ($config->getCustomerNumber() && $config->getShopNumber() && $config->getApiKey() && !$config->getOauthAccessToken()) { // needs to load the non-oauth-url since the new access token needs to be generated using the classic shopgate merchant api authentication $apiUrls = $config->getApiUrls(); $apiUrl = $config->getServer() == 'custom' ? str_replace('/api/merchant2', '/api/merchant', $config->getApiUrl()) : $apiUrls[$config->getServer()][ShopgateConfigInterface::SHOPGATE_AUTH_SERVICE_CLASS_NAME_SHOPGATE]; $smaAuthServiceShopgate = new ShopgateAuthenticationServiceShopgate($config->getCustomerNumber(), $config->getApiKey()); $smaAuthServiceShopgate->setup($config); $classicSma = new ShopgateMerchantApi($smaAuthServiceShopgate, $config->getShopNumber(), $apiUrl); // the "get_shop_info" returns an oauth access token $shopInfo = $classicSma->getShopInfo()->getData(); // set newly generated access token $this->accessToken = $shopInfo['oauth_access_token']; // 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 $config->load($shopgateSettingsNew); $config->save(array_keys($shopgateSettingsNew), true); } elseif (!$this->accessToken && $config->getOauthAccessToken()) { // this would mean the data was somehow not in sync (should no be happening by default) $this->accessToken = $config->getOauthAccessToken(); } else { // skip this since the connection is fully functional or there has not been made any connection at all, yet // -> missing data (except the oauth access token) is treated as "no valid connection available" // -> in either case there is nothing to do here } }
/** * Builds the Shopgate Library object graph for ShopgateMerchantApi and returns the instance. * * @return ShopgateMerchantApi */ public function buildMerchantApi() { $authService = new ShopgateAuthentificationService($this->config->getCustomerNumber(), $this->config->getApikey()); $merchantApi = new ShopgateMerchantApi($authService, $this->config->getShopNumber(), $this->config->getApiUrl()); return $merchantApi; }
/** * Builds the Shopgate Library object graph for ShopgateMerchantApi and returns the instance. * * @return ShopgateMerchantApi */ public function buildMerchantApi() { $merchantApi = null; switch ($smaAuthServiceClassName = $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 trigger_error('Invalid SMA-Auth-Service defined - this should not happen with valid plugin code', E_USER_ERROR); break; } return $merchantApi; }
public function handleRequest(array $data = array()) { // log incoming request $this->log(ShopgateLogger::getInstance()->cleanParamsForLog($data), ShopgateLogger::LOGTYPE_ACCESS); // save the params $this->params = $data; // save trace_id if (isset($this->params['trace_id'])) { $this->trace_id = $this->params['trace_id']; } try { $this->authService->checkAuthentification(); // set error handler to Shopgate's handler if requested if (!empty($this->params['use_errorhandler'])) { set_error_handler('ShopgateErrorHandler'); } // check if the request is for the correct shop number or an adapter-plugin if (!$this->config->getIsShopgateAdapter() && !empty($this->params['shop_number']) && $this->params['shop_number'] != $this->config->getShopNumber()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_SHOP_NUMBER, "{$this->params['shop_number']}"); } // check if an action to call has been passed, is known and enabled if (empty($this->params['action'])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_ACTION, 'Passed parameters: ' . var_export($data, true)); } // check if the action is white-listed if (!in_array($this->params['action'], $this->actionWhitelist)) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_ACTION, "{$this->params['action']}"); } // check if action is enabled in the config $configArray = $this->config->toArray(); if (empty($configArray['enable_' . $this->params['action']])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_DISABLED_ACTION, "{$this->params['action']}"); } // enable debugging if requested if (!empty($data['debug_log'])) { ShopgateLogger::getInstance()->enableDebug(); ShopgateLogger::getInstance()->keepDebugLog(!empty($data['keep_debug_log'])); } // enable error handler if requested if (!empty($data['error_reporting'])) { error_reporting($data['error_reporting']); } // call the action $action = $this->camelize($this->params['action']); $this->{$action}(); } catch (ShopgateLibraryException $e) { $error = $e->getCode(); $errortext = $e->getMessage(); } catch (ShopgateMerchantApiException $e) { $error = ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED; $errortext = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED) . ': "' . $e->getCode() . ' - ' . $e->getMessage() . '"'; } catch (Exception $e) { $message = "\n" . get_class($e) . "\n"; $message .= 'with code: ' . $e->getCode() . "\n"; $message .= 'and message: \'' . $e->getMessage() . "'\n"; // new ShopgateLibraryException to build proper error message and perform logging $se = new ShopgateLibraryException($message); $error = $se->getCode(); $errortext = $se->getMessage(); } // print out the response if (!empty($error)) { if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id); } $this->response->markError($error, $errortext); } if (empty($this->response)) { trigger_error('No response object defined. This should _never_ happen.', E_USER_ERROR); } $this->response->setData($this->responseData); $this->response->send(); // return true or false return empty($error); }