/** * 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 "update_order" action. * * @throws ShopgateLibraryException * @see http://wiki.shopgate.com/Shopgate_Plugin_API_update_order/ */ protected function updateOrder() { if (!isset($this->params['order_number'])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_ORDER_NUMBER); } $orders = $this->merchantApi->getOrders(array('order_numbers[0]' => $this->params['order_number'], 'with_items' => 1))->getData(); if (empty($orders)) { throw new ShopgateLibraryException(ShopgateLibraryException::MERCHANT_API_INVALID_RESPONSE, '"order" not set. Response: ' . var_export($orders, true)); } if (count($orders) > 1) { throw new ShopgateLibraryException(ShopgateLibraryException::MERCHANT_API_INVALID_RESPONSE, 'more than one order in response. Response: ' . var_export($orders, true)); } $payment = 0; $shipping = 0; if (isset($this->params['payment'])) { $payment = (bool) $this->params['payment']; } if (isset($this->params['shipping'])) { $shipping = (bool) $this->params['shipping']; } $orders[0]->setUpdatePayment($payment); $orders[0]->setUpdateShipping($shipping); if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id); } $orderData = $this->plugin->updateOrder($orders[0]); if (is_array($orderData)) { $this->responseData = $orderData; } else { $this->responseData['external_order_id'] = $orderData; $this->responseData['external_order_number'] = null; } }
/** * Updates the (skip) keywords array from cache file or Shopgate Merchant API if enabled. */ protected function updateRedirectKeywords() { // load the keywords try { $redirectKeywordsFromFile = $this->loadKeywordsFromFile($this->config->getRedirectKeywordCachePath()); $skipRedirectKeywordsFromFile = $this->loadKeywordsFromFile($this->config->getRedirectSkipKeywordCachePath()); } catch (ShopgateLibraryException $e) { // if reading the files fails DO NOT UPDATE return; } // conditions for updating keywords $updateDesired = $this->updateRedirectKeywords && !empty($this->merchantApi) && (time() - ($redirectKeywordsFromFile['timestamp'] + $this->redirectKeywordCacheTime * 3600) > 0 || time() - ($skipRedirectKeywordsFromFile['timestamp'] + $this->redirectKeywordCacheTime * 3600) > 0); // strip timestamp, it's not needed anymore $redirectKeywords = $redirectKeywordsFromFile['keywords']; $skipRedirectKeywords = $skipRedirectKeywordsFromFile['keywords']; // perform update if ($updateDesired) { try { // fetch keywords from Shopgate Merchant API $keywordsFromApi = $this->merchantApi->getMobileRedirectUserAgents(); $redirectKeywords = $keywordsFromApi['keywords']; $skipRedirectKeywords = $keywordsFromApi['skip_keywords']; // save keywords to their files $this->saveKeywordsToFile($redirectKeywords, $this->config->getRedirectKeywordCachePath()); $this->saveKeywordsToFile($skipRedirectKeywords, $this->config->getRedirectSkipKeywordCachePath()); } catch (Exception $e) { /* do not abort */ $newTimestamp = time() - $this->redirectKeywordCacheTime * 3600 + 300; // save old keywords $this->saveKeywordsToFile($redirectKeywords, $this->config->getRedirectKeywordCachePath(), $newTimestamp); $this->saveKeywordsToFile($skipRedirectKeywords, $this->config->getRedirectSkipKeywordCachePath(), $newTimestamp); } } // set keywords $this->setRedirectKeywords($redirectKeywords); $this->setSkipRedirectKeywords($skipRedirectKeywords); }