/** * 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(); }