public function actionSso()
 {
     craft()->log->removeRoute('WebLogRoute');
     craft()->log->removeRoute('ProfileLogRoute');
     if ($this->ssoEnabled) {
         $customerId = 0;
         $auth_token = '';
         $redirect_url = '';
         $fcsid = craft()->request->getParam("fcsid", "");
         $timestamp = craft()->request->getParam("timestamp", 0) + 60 * 30;
         // valid for 30 minutes
         if (!craft()->userSession->isLoggedIn()) {
             // No member
             if ($this->ssoRequireLogin) {
                 // No guest checkouts allowed, redirect to the sites login page
                 $redirect_url = UrlHelper::getUrl(craft()->config->getLoginPath());
             }
         } else {
             $user = craft()->userSession->getUser();
             $customerId = craft()->foxyCart->getCustomerId($user);
             if ($user && !$customerId) {
                 // Member doesn't have a FoxyCart customer id, see if the member exists on FoxyCart
                 $xml = craft()->foxyCart->api("customer_get", array("customer_email" => $user->email));
                 if ($xml !== false) {
                     $customerId = (string) $xml->customer_id;
                 }
                 if (!$customerId || !$xml) {
                     // Member doesn't exist, create one for FoxyCart
                     $customerId = craft()->foxyCart->updateFoxyCartCustomer($user);
                 }
                 if (!$customerId) {
                     FoxyCartPlugin::log("[sso] User creation failed.", LogLevel::Error);
                     // TODO: What should happen here? A user is logged in, but everything failed to get their current customerId? Would that even happen?
                 } else {
                     // Update the current user's customerId as retrieved from FoxyCart
                     craft()->foxyCart->saveCustomerId($user, $customerId);
                 }
             }
         }
         $auth_token = sha1($customerId . '|' . $timestamp . '|' . $this->apikey);
         $redirect_url = $redirect_url != '' ? $redirect_url : 'https://' . $this->storedomain . '/checkout?fc_auth_token=' . $auth_token . '&fc_customer_id=' . $customerId . '&timestamp=' . $timestamp . '&fcsid=' . $fcsid;
         craft()->request->redirect($redirect_url);
     }
 }
 public function api($method, $params = array())
 {
     // Decide if the call can be cached
     $cached = false;
     $cacheableMethods = array('store_includes_get', 'attribute_list', 'category_list', 'downloadable_list', 'customer_list', 'customer_get', 'customer_address_get', 'transaction_list', 'transaction_get', 'subscription_get', 'subscription_list');
     if (in_array($method, $cacheableMethods)) {
         $cached = true;
         $cacheKey = "foxycart_" . $method;
         if (count($params) > 0) {
             $cacheKey .= "_" . hash('sha256', http_build_query($params));
         }
         $cachedResponse = craft()->cache->get($cacheKey);
         if ($cachedResponse) {
             FoxyCartPlugin::log("[api] Returning cached data for " . $method . "?" . http_build_query($params), LogLevel::Info);
             return simplexml_load_string($cachedResponse, NULL, LIBXML_NOCDATA);
         }
     }
     try {
         $client = new \Guzzle\Http\Client("https://" . $this->storedomain);
         $foxy_data = array_merge(array("api_token" => $this->apikey, "api_action" => $method), $params);
         $request = $client->post("/api", array(), array('verify' => self::$curl_ssl_verifypeer, 'timeout' => self::$curl_timeout, 'connect_timeout' => self::$curl_connecttimeout));
         $request = $request->addPostFields($foxy_data);
         $response = $request->send();
         if (!$response->isSuccessful()) {
             return false;
         }
         $xml = simplexml_load_string($response->getBody(true), NULL, LIBXML_NOCDATA);
         if ($xml->result == "ERROR") {
             $errorMessages = array();
             foreach ($xml->messages->message as $message) {
                 array_push($errorMessages, $message);
             }
             FoxyCartPlugin::log('[api] An API request returned an error: ' . join(", ", $errorMessages), LogLevel::Error);
             return false;
         }
         if ($cached) {
             // Cache this call for 10 minutes
             craft()->cache->set($cacheKey, $response->getBody(true), 600);
         }
         return $xml;
     } catch (\Exception $e) {
         FoxyCartPlugin::log('[api] An API request failed: ' . $e->getMessage(), LogLevel::Error);
         return false;
     }
 }