/** * 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; }
/** * Serializes the order into an array structure. * * @param NostoOrderInterface $order the order to serialize. * @return array the serialized data. */ public function serialize(NostoOrderInterface $order) { /** @var NostoFormatterDate $dateFormatter */ $dateFormatter = Nosto::formatter('date'); /** @var NostoFormatterPrice $priceFormatter */ $priceFormatter = Nosto::formatter('price'); $data = array('order_number' => $order->getOrderNumber(), 'buyer' => array(), 'purchased_items' => array()); if ($order->getCreatedDate() instanceof NostoDate) { $data['created_at'] = $dateFormatter->format($order->getCreatedDate()); } else { $data['created_at'] = ''; } if ($order->getStatus() instanceof NostoOrderStatusInterface) { $data['order_status_code'] = $order->getStatus()->getCode(); $data['order_status_label'] = $order->getStatus()->getLabel(); } elseif (is_string($order->getStatus()) || is_numeric($order->getStatus())) { $data['order_status_code'] = $order->getStatus(); $data['order_status_label'] = $order->getStatus(); } if ($order->getPaymentProvider() instanceof NostoOrderPaymentProviderInterface) { $data['payment_provider'] = $order->getPaymentProvider()->getProvider(); } elseif (is_string($order->getPaymentProvider()) || is_numeric($order->getPaymentProvider())) { $data['payment_provider'] = $order->getPaymentProvider(); } foreach ($order->getItems() as $item) { $itemData = array('product_id' => $item->getItemId(), 'quantity' => (int) $item->getQuantity(), 'name' => $item->getName()); if ($item->getUnitPrice() instanceof NostoPrice) { $itemData['unit_price'] = $priceFormatter->format($item->getUnitPrice()); } elseif (is_numeric($item->getUnitPrice())) { $itemData['unit_price'] = $item->getUnitPrice(); } else { $itemData['unit_price'] = ''; } if ($item->getCurrency() instanceof NostoCurrencyCode) { $itemData['price_currency_code'] = $item->getCurrency()->getCode(); } elseif (is_string($item->getCurrency())) { $itemData['price_currency_code'] = $item->getCurrency(); } else { $itemData['price_currency_code'] = ''; } $data['purchased_items'][] = $itemData; } // Add optional order reference if set. if ($order->getExternalRef()) { $data['external_order_ref'] = $order->getExternalRef(); } // Add optional buyer info. if ($order->getBuyer() instanceof NostoOrderBuyerInterface) { $data['buyer']['first_name'] = $order->getBuyer()->getFirstName(); $data['buyer']['last_name'] = $order->getBuyer()->getLastName(); $data['buyer']['email'] = $order->getBuyer()->getEmail(); } // Add optional order status history if set. if ($order->getHistoryStatuses() !== array()) { $dateFormat = new NostoDateFormat(NostoDateFormat::ISO_8601); $statuses = array(); foreach ($order->getHistoryStatuses() as $status) { if ($status instanceof NostoOrderStatusInterface && $status->getCreatedAt()) { if (!isset($statuses[$status->getCode()])) { $statuses[$status->getCode()] = array(); } $statuses[$status->getCode()][] = $dateFormatter->format($status->getCreatedAt(), $dateFormat); } } if (count($statuses) > 0) { $data['order_statuses'] = $statuses; } } return $data; }