/**
  * Builds the API request and returns it.
  *
  * @param NostoAccount $account the Nosto account object.
  * @param string|null $customerId the Nosto customer ID of the user who placed the order.
  * @return NostoApiRequest the request object.
  */
 protected function initApiRequest(NostoAccount $account, $customerId)
 {
     $request = new NostoApiRequest();
     $request->setContentType('application/json');
     if (!empty($customerId)) {
         $request->setPath(NostoApiRequest::PATH_ORDER_TAGGING);
         $request->setReplaceParams(array('{m}' => $account->getName(), '{cid}' => $customerId));
     } else {
         $request->setPath(NostoApiRequest::PATH_UNMATCHED_ORDER_TAGGING);
         $request->setReplaceParams(array('{m}' => $account->getName()));
     }
     return $request;
 }
 /**
  * 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;
 }
Example #3
0
 /**
  * @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;
 }