/** * 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)) { throw new NostoException(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; }
/** * 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; }
require_once dirname(__FILE__) . '/classes/Nosto.php'; require_once dirname(__FILE__) . '/classes/NostoAccount.php'; require_once dirname(__FILE__) . '/classes/NostoAccountMeta.php'; require_once dirname(__FILE__) . '/classes/NostoCipher.php'; require_once dirname(__FILE__) . '/classes/NostoDotEnv.php'; require_once dirname(__FILE__) . '/classes/NostoMessage.php'; require_once dirname(__FILE__) . '/classes/NostoOrderConfirmation.php'; require_once dirname(__FILE__) . '/classes/NostoProductReCrawl.php'; require_once dirname(__FILE__) . '/classes/NostoValidator.php'; require_once dirname(__FILE__) . '/classes/NostoExchangeRate.php'; require_once dirname(__FILE__) . '/classes/NostoCurrency.php'; require_once dirname(__FILE__) . '/classes/NostoCurrencyCode.php'; require_once dirname(__FILE__) . '/classes/NostoCurrencySymbol.php'; require_once dirname(__FILE__) . '/classes/NostoCurrencyFormat.php'; require_once dirname(__FILE__) . '/classes/NostoCurrencyInfo.php'; // Libs require_once dirname(__FILE__) . '/libs/phpseclib/crypt/NostoCryptBase.php'; require_once dirname(__FILE__) . '/libs/phpseclib/crypt/NostoCryptRijndael.php'; require_once dirname(__FILE__) . '/libs/phpseclib/crypt/NostoCryptAES.php'; require_once dirname(__FILE__) . '/libs/phpseclib/crypt/NostoCryptRandom.php'; // Parse .env if exists and assign configured environment variables. NostoDotEnv::getInstance()->init(dirname(__FILE__)); if (isset($_ENV['NOSTO_API_BASE_URL'])) { NostoApiRequest::$baseUrl = $_ENV['NOSTO_API_BASE_URL']; } if (isset($_ENV['NOSTO_OAUTH_BASE_URL'])) { NostoOAuthClient::$baseUrl = $_ENV['NOSTO_OAUTH_BASE_URL']; } if (isset($_ENV['NOSTO_WEB_HOOK_BASE_URL'])) { NostoHttpRequest::$baseUrl = $_ENV['NOSTO_WEB_HOOK_BASE_URL']; }
* and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its contributors * may be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Nosto Solutions Ltd <*****@*****.**> * @copyright 2016 Nosto Solutions Ltd * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause * */ // This is global bootstrap for autoloading date_default_timezone_set('Europe/Helsinki'); // Pre-load all sdk classes. require_once dirname(__FILE__) . '/../src/config.inc.php'; // Configure API, Web Hooks, and OAuth client to use Mock server when testing. NostoApiRequest::$baseUrl = 'http://localhost:3000'; NostoOAuthClient::$baseUrl = 'http://localhost:3000'; NostoHttpRequest::$baseUrl = 'http://localhost:3000'; NostoHttpRequest::buildUserAgent('PHPUnit', '1.0.0', '1.0.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; }
/** * Create and returns a new API request object initialized with: * - content type * - auth token * * @return NostoApiRequest the newly created request object. * @throws NostoException if the account does not have the `products` token set. */ protected function initApiRequest() { $token = $this->account->getApiToken('products'); if (is_null($token)) { throw new NostoException('No `products` API token found for account.'); } $request = new NostoApiRequest(); $request->setContentType('application/json'); $request->setAuthBasic('', $token->getValue()); return $request; }
/** * Builds the API request and returns it. * * @param string $path the request API path. * @param string $auth the basic auth token. * @return NostoApiRequest the request object. */ protected function initApiRequest($path, $auth) { $request = new NostoApiRequest(); $request->setContentType('application/json'); $request->setAuthBasic('', $auth); $request->setPath($path); return $request; }
/** * 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; }