/** * 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 static function create(NostoAccountMetaDataInterface $meta) { $params = array('title' => $meta->getTitle(), 'name' => $meta->getName(), 'platform' => $meta->getPlatform(), 'front_page_url' => $meta->getFrontPageUrl(), 'currency_code' => strtoupper($meta->getCurrencyCode()), 'language_code' => strtolower($meta->getOwnerLanguageCode()), 'owner' => array('first_name' => $meta->getOwner()->getFirstName(), 'last_name' => $meta->getOwner()->getLastName(), 'email' => $meta->getOwner()->getEmail()), 'api_tokens' => array()); // Add optional billing details if the required data is set. $billingDetails = array('country' => strtoupper($meta->getBillingDetails()->getCountry())); if (!empty($billingDetails['country'])) { $params['billing_details'] = $billingDetails; } // Add optional partner code if one is set. $partnerCode = $meta->getPartnerCode(); if (!empty($partnerCode)) { $params['partner_code'] = $partnerCode; } // Request all available API tokens for the account. foreach (NostoApiToken::$tokenNames as $name) { $params['api_tokens'][] = 'api_' . $name; } $request = new NostoApiRequest(); $request->setPath(NostoApiRequest::PATH_SIGN_UP); $request->setReplaceParams(array('{lang}' => $meta->getLanguageCode())); $request->setContentType('application/json'); $request->setAuthBasic('', $meta->getSignUpApiToken()); $response = $request->post(json_encode($params)); if ($response->getCode() !== 200) { Nosto::throwHttpException('Nosto account could not be created.', $request, $response); } $account = new self($meta->getPlatform() . '-' . $meta->getName()); $account->tokens = NostoApiToken::parseTokens($response->getJsonResult(true), '', '_token'); return $account; }
/** * 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; }