/** * Sends an order confirmation to Nosto. * * @param Nosto_Tagging_Model_Meta_Order $order the order to confirm. * @param NostoAccount $account the Nosto account object. * @param null $customerId the Nosto customer ID of the user who placed the order. * @throws NostoException on failure. * @return true on success. */ public function confirm(Nosto_Tagging_Model_Meta_Order $order, NostoAccount $account, $customerId = null) { $request = $this->initApiRequest($account, $customerId); $response = $request->post($this->getOrderAsJson($order)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to send order confirmation to Nosto.', $request, $response); } return true; }
/** * Updates exchange rates to Nosto * @return bool * @throws NostoException */ public function update() { $request = $this->initApiRequest(); $response = $request->post($this->getCollectionAsJson()); if ($response->getCode() !== 200) { Nosto::throwHttpException(sprintf('Failed to update currencyCode exchange rates for account %s.', $this->account->getName()), $request, $response); } return true; }
/** * Sends the order confirmation to Nosto. * * @param NostoOrderInterface $order the placed order model. * @param NostoAccountInterface $account the Nosto account for the shop where the order was placed. * @param null $customerId the Nosto customer ID of the user who placed the order. * @throws NostoException on failure. * @return true on success. */ public static function send(NostoOrderInterface $order, NostoAccountInterface $account, $customerId = null) { if (!empty($customerId)) { $path = NostoApiRequest::PATH_ORDER_TAGGING; $replaceParams = array('{m}' => $account->getName(), '{cid}' => $customerId); } else { $path = NostoApiRequest::PATH_UNMATCHED_ORDER_TAGGING; $replaceParams = array('{m}' => $account->getName()); } $request = new NostoApiRequest(); $request->setPath($path); $request->setContentType('application/json'); $request->setReplaceParams($replaceParams); $orderData = array('order_number' => $order->getOrderNumber(), 'order_status_code' => $order->getOrderStatus()->getCode(), 'order_status_label' => $order->getOrderStatus()->getLabel(), 'buyer' => array('first_name' => $order->getBuyerInfo()->getFirstName(), 'last_name' => $order->getBuyerInfo()->getLastName(), 'email' => $order->getBuyerInfo()->getEmail()), 'created_at' => Nosto::helper('date')->format($order->getCreatedDate()), 'payment_provider' => $order->getPaymentProvider(), 'external_order_ref' => $order->getExternalOrderRef(), 'purchased_items' => array()); foreach ($order->getPurchasedItems() as $item) { $orderData['purchased_items'][] = array('product_id' => $item->getProductId(), 'quantity' => (int) $item->getQuantity(), 'name' => $item->getName(), 'unit_price' => Nosto::helper('price')->format($item->getUnitPrice()), 'price_currency_code' => strtoupper($item->getCurrencyCode())); } $response = $request->post(json_encode($orderData)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to send order confirmation to Nosto.', $request, $response); } return true; }
/** * @inheritdoc */ public function ssoLogin(NostoAccountMetaDataIframeInterface $meta) { $token = $this->getApiToken('sso'); if ($token === null) { return false; } $request = new NostoHttpRequest(); $request->setUrl(NostoHttpRequest::$baseUrl . NostoHttpRequest::PATH_SSO_AUTH); $request->setReplaceParams(array('{platform}' => $meta->getPlatform(), '{email}' => $meta->getEmail())); $request->setContentType('application/x-www-form-urlencoded'); $request->setAuthBasic('', $token->getValue()); $response = $request->post(http_build_query(array('fname' => $meta->getFirstName(), 'lname' => $meta->getLastName()))); $result = $response->getJsonResult(); if ($response->getCode() !== 200) { Nosto::throwHttpException('Unable to login employee to Nosto with SSO token.', $request, $response); } if (empty($result->login_url)) { throw new NostoException('No "login_url" returned when logging in employee to Nosto'); } return $result->login_url; }
/** * Authenticates the application with the given code to receive an access token. * * @param string $code code sent by the authorization server to exchange for an access token. * @return NostoOAuthToken * @throws NostoException */ public function authenticate($code) { if (empty($code)) { throw new NostoException('Invalid authentication token'); } $request = new NostoHttpRequest(); $request->setUrl(self::$baseUrl . self::PATH_TOKEN); $request->setReplaceParams(array('{cid}' => $this->clientId, '{sec}' => $this->clientSecret, '{uri}' => $this->redirectUrl, '{cod}' => $code)); $response = $request->get(); $result = $response->getJsonResult(true); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to authenticate with code.', $request, $response); } if (empty($result['access_token'])) { throw new NostoException('No "access_token" returned after authenticating with code'); } if (empty($result['merchant_name'])) { throw new NostoException('No "merchant_name" returned after authenticating with code'); } return NostoOAuthToken::create($result); }
/** * Builds the API request and returns it. * * @return NostoApiRequest the request object. * @throws NostoException if the request object cannot be built. */ protected function initApiRequest() { $token = $this->account->getApiToken(NostoApiToken::API_EXCHANGE_RATES); if (is_null($token)) { Nosto::throwHttpException(sprintf('No `%s` API token found for account "%s".', NostoApiToken::API_EXCHANGE_RATES, $this->account->getName())); } $request = new NostoApiRequest(); $request->setContentType('application/json'); $request->setAuthBasic('', $token->getValue()); $request->setPath(NostoApiRequest::PATH_CURRENCY_EXCHANGE_RATE); return $request; }
/** * Sends a POST request to delete all the products currently in the collection. * * @return bool if the request was successful. * @throws NostoException on failure. */ public function delete() { $request = $this->initApiRequest(); $request->setPath(NostoApiRequest::PATH_PRODUCTS_DISCONTINUE); $response = $request->post($this->getCollectionIdsAsJson()); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to delete Nosto product(s).', $request, $response); } return true; }
/** * Sends the re-crawl API request to Nosto. * * @param NostoAccountInterface $account the account to re-crawl the product(s) for. * @param array $payload the request payload as an array that will be json encoded. * @return bool true on success. * @throws NostoException if the request fails or cannot be made. */ protected static function sendRequest(NostoAccountInterface $account, array $payload) { $token = $account->getApiToken('products'); if ($token === null) { throw new NostoException('Failed to send product re-crawl to Nosto. No `products` API token found for account.'); } $request = new NostoApiRequest(); $request->setPath(NostoApiRequest::PATH_PRODUCT_RE_CRAWL); $request->setContentType('application/json'); $request->setAuthBasic('', $token->getValue()); $response = $request->post(json_encode($payload)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Failed to send product re-crawl to Nosto.', $request, $response); } return true; }