2017 © Pedro Peláez
 

library php-sdk

PHP SDK for developing Nosto modules for e-commerce platforms

image

nosto/php-sdk

PHP SDK for developing Nosto modules for e-commerce platforms

  • Tuesday, June 26, 2018
  • by nosto
  • Repository
  • 30 Watchers
  • 4 Stars
  • 66,807 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 4 Forks
  • 14 Open issues
  • 100 Versions
  • 19 % Grown

The README.md

php-sdk

Provides tools for building modules that integrate Nosto into your e-commerce platform., (*1)

Requirements

  • The Nosto PHP SDK requires at least PHP version 5.4.0 and it's also compatible with PHP >= 7.0.0

Getting started

Creating a new Nosto account

A Nosto account is needed for every shop and every language within each shop., (*2)

    .....
    try {
        /** @var NostoSignupInterface $meta */
        /** @var NostoSignup $account */
        $account = NostoSignup::create($meta);
        // save newly created account according to the platforms requirements
        .....
    } catch (NostoException $e) {
        // handle failure
        .....
    }
    .....

Connecting with an existing Nosto account

This should be done in the shops back end when the admin user wants to connect an existing Nosto account to the shop., (*3)

First redirect to the Nosto OAuth2 server., (*4)

    .....
    /** @var OAuthInterface $meta */
    $client = new NostoOAuthClient($meta);
    header('Location: ' . $client->getAuthorizationUrl());

Then have a public endpoint ready to handle the return request., (*5)

    if (isset($_GET['code'])) {
        try {
            /** @var OAuthInterface $meta */
            $account = NostoSignup::syncFromNosto($meta, $_GET['code']);
            // save the synced account according to the platforms requirements
        } catch (NostoException $e) {
            // handle failures
        }
        // redirect to the admin page where the user can see the account controls
        .....
    }
    } elseif (isset($_GET['error'])) {
        // handle errors; 3 parameter will be sent, 'error', 'error_reason' and 'error_description'
        // redirect to the admin page where the user can see an error message
        .....
    } else {
        // 404
        .....
    }

Deleting a Nosto account

This should be used when you delete a Nosto account for a shop. It will notify Nosto that this account is no longer used., (*6)

    try {
        /** @var NostoSignup $account */
        $account->delete();
    } catch (NostoException $e) {
        // handle failure
    }

Get authenticated Nosto URL for the account controls

The Nosto account can be created and managed through the controls that should be accessible to the admin user in the shop's backend. The account controls will redirect to nosto.com., (*7)

    .....
    /**
     * @param ConnectionMetadataInterface $connection the connection meta data.
     * @param AccountInterface|null $account the configuration to return the url for.
     * @param UserInterface|null $user
     * @param array $params additional parameters to add to the connection url.
     */
    try
    {
        $url = Nosto::helper('connection')->getUrl($meta, $account, $user, $params);
    }
    catch (NostoException $e)
    {
        // handle failure
    }
    // render the link to the user with given url
    .....

Sending order confirmations using the Nosto API

Sending order confirmations to Nosto is a vital part of the functionality. OrderConfirm confirmations should be sent when an order has been completed in the shop. It is NOT recommended to do this when the "thank you" page is shown to the user, as payment gateways work differently and you cannot rely on the user always being redirected back to the shop after a payment has been made. Therefore, it is recommended to send the order conformation when the order is marked as payed in the shop., (*8)

OrderConfirm confirmations can be sent two different ways:, (*9)

  • matched orders; where we know the Nosto customer ID of the user who placed the order
  • un-matched orders: where we do not know the Nosto customer ID of the user who placed the order

The Nosto customer ID is set in a cookie "2c.cId" by Nosto and it is up to the platform to keep a link between users and the Nosto customer ID. It is recommended to tie the Nosto customer ID to the order or shopping cart instead of an user ID, as the platform may support guest checkouts., (*10)

    .....
    try {
        /**
         * @var NostoOrderInterface $order
         * @var NostoSignupInterface $account
         * @var string $customerId
         */
        NostoOrderConfirmation::send($order, $account, $customerId);
    } catch (NostoException $e) {
        // handle error
    }
    .....

Sending product re-crawl requests using the Nosto API

Note: this feature has been deprecated in favor of the create/update/delete method below., (*11)

When a product changes in the store, stock is reduced, price is updated etc. it is recommended to send an API request to Nosto that initiates a product "re-crawl" event. This is done to update the recommendations including that product so that the newest information can be shown to the users on the site., (*12)

Note: the $product model needs to include only productId and url properties, all others can be omitted., (*13)

    .....
    try {
        /**
         * @var NostoProductInterface $product
         * @var NostoSignupInterface $account
         */
        NostoProductReCrawl::send($product, $account);
    } catch (NostoException $e) {
        // handle error
    }
    .....

Batch re-crawling is also possible by creating a collection of product models:, (*14)

    .....
    try {
        /**
         * @var NostoExportProductCollection $collection
         * @var NostoProductInterface $product
         * @var NostoSignupInterface $account
         */
        $collection[] = $product;
        NostoProductReCrawl::sendBatch($collection, $account);
    } catch (NostoException $e) {
        // handle error
    }
    .....

Sending product create/update/delete requests using the Nosto API

When a product changes in the store, stock is reduced, price is updated etc. it is recommended to send an API request to Nosto to handle the updated product info. This is also true when adding new products as well as deleting existing ones. This is done to update the recommendations including that product so that the newest information can be shown to the users on the site., (*15)

Creating new products:, (*16)

    .....
    try {
        /**
         * @var NostoProductInterface $product
         * @var NostoSignupInterface $account
         */
        $op = new UpsertProduct($account);
        $op->addProduct($product);
        $op->create();
    } catch (NostoException $e) {
        // handle error
    }
    .....

Note: you can call addProduct multiple times to add more products to the request. This way you can batch create products., (*17)

Updating existing products:, (*18)

    .....
    try {
        /**
         * @var NostoProductInterface $product
         * @var NostoSignupInterface $account
         */
        $op = new UpsertProduct($account);
        $op->addProduct($product);
        $op->update();
    } catch (NostoException $e) {
        // handle error
    }
    .....

Note: you can call addProduct multiple times to add more products to the request. This way you can batch update products., (*19)

Deleting existing products:, (*20)

    .....
    try {
        /**
         * @var NostoProductInterface $product
         * @var NostoSignupInterface $account
         */
        $op = new UpsertProduct($account);
        $op->addProduct($product);
        $op->delete();
    } catch (NostoException $e) {
        // handle error
    }
    .....

Note: you can call addProduct multiple times to add more products to the request. This way you can batch delete products., (*21)

Exporting encrypted product/order information that Nosto can request

When new Nosto accounts are created for a shop, Nosto will try to fetch historical data about products and orders. This information is used to bootstrap recommendations and decreases the time needed to get accurate recommendations showing in the shop., (*22)

For this to work, Nosto requires 2 public endpoints that can be called once a new account has been created through the API. These endpoints should serve the history data encrypted with AES. The SDK comes bundled with the ability to encrypt the data with a pure PHP solution (http://phpseclib.sourceforge.net/), It is recommended, but not required, to have mcrypt installed on the server., (*23)

Additionally, the endpoints need to support the ability to limit the amount of products/orders to export and an offset for fetching batches of data. These must be implemented as GET parameters "limit" and "offset" which will be sent as integer values and expected to be applied to the data set being exported., (*24)

    .....
    /**
     * @var NostoProductInterface[] $products
     * @var NostoSignupInterface $account
     */
    $collection = new NostoExportProductCollection();
    foreach ($products as $product) {
        $collection[] = $product;
    }
    // The exported will encrypt the collection and output the result.
    $cipher_text = NostoExporter::export($account, $collection);
    echo $cipher_text;
    // It is important to stop the script execution after the export, in order to avoid any additional data being outputted.
    die();
    .....
    /**
     * @var NostoOrderInterface[] $orders
     * @var NostoSignupInterface $account
     */
    $collection = new NostoExportOrderCollection();
    foreach ($orders as $order) {
        $collection[] = $order;
    }
    // The exported will encrypt the collection and output the result.
    $cipher_text = NostoExporter::export($account, $collection);
    echo $cipher_text;
    // It is important to stop the script execution after the export, in order to avoid any additional data being outputted.
    die();

Testing

The SDK is unit tested with Codeception (http://codeception.com/)., (*25)

Running tests

First cd into the root directory., (*26)

Then install Codeception via composer:, (*27)

    php composer.phar install

Then run the tests:, (*28)

    vendor/bin/codecept run

Testing new added operation

The SDK unit test uses the apiary as the stub server. The apiary pulls the api-blueprint.md from master branch and builds fake api endpoints based on it. A way to test new added operation before merging it to master is using Drakov API Bleuprint Mock Server (https://github.com/Aconex/drakov) running on Node., (*29)

First cd into the root directory., (*30)

Then install Codeception via composer:, (*31)

    php composer.phar install

After that you can install the drakov server via npm:, (*32)

    npm install -g drakov

Update the endpoints in the tests/.env file:, (*33)

    NOSTO_API_BASE_URL=localhost:3000
    NOSTO_OAUTH_BASE_URL=localhost:3000/oauth
    NOSTO_WEB_HOOK_BASE_URL=http://localhost:3000

Then start the drakov mock server with the API blueprint:, (*34)

    drakov -f tests/api-blueprint.md

Then in another window run the tests:, (*35)

    vendor/bin/codecept run

You can pass debugMode flag to the drakov server for debugging purposes:, (*36)

    drakov -f tests/api-blueprint.md --debugMode

Running phpcs

First cd into the root directory., (*37)

Then the phpcs:, (*38)

    phpcs --standard=ruleset.xml -v .

The Versions

26/06 2018

dev-feature/marketing-permission-operation

dev-feature/marketing-permission-operation

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

30/05 2018

dev-hotfix/Fix-simple-serializable-objects

dev-hotfix/Fix-simple-serializable-objects

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

29/05 2018

dev-feature/markupable-category-pagetype

dev-feature/markupable-category-pagetype

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

02/03 2018

dev-enhancement/add-to-json-helper-for-collection

dev-enhancement/add-to-json-helper-for-collection

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

02/03 2018

dev-bugfix/do-not-convert-custom-field-key-to-snake-case

dev-bugfix/do-not-convert-custom-field-key-to-snake-case

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

25/01 2018

dev-feature/support-for-discontinuing-products

dev-feature/support-for-discontinuing-products

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

24/01 2018

dev-bugfix/handle-array-in-serializer

dev-bugfix/handle-array-in-serializer

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

24/01 2018

dev-refactor/remove-redundant-constructors

dev-refactor/remove-redundant-constructors

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

12/01 2018

dev-feature/support-update-cart-operation

dev-feature/support-update-cart-operation

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

12/01 2018
28/12 2017

dev-feature/add-sku-id-to-cart-and-order-tagging

dev-feature/add-sku-id-to-cart-and-order-tagging

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

21/12 2017

dev-enhancement/extract-base-class-for-sku-and-product

dev-enhancement/extract-base-class-for-sku-and-product

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

03/11 2017

dev-bugfix/fix-variation-id-tagging

dev-bugfix/fix-variation-id-tagging

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

31/10 2017

dev-bugfix/html-markup-omit-property-values-are-null

dev-bugfix/html-markup-omit-property-values-are-null

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

16/10 2017
12/10 2017

dev-feature/fix-phan-findings

dev-feature/fix-phan-findings

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

11/10 2017

dev-feature/backward-compatibility

dev-feature/backward-compatibility

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

10/10 2017

dev-feature/add-notranslate-tag

dev-feature/add-notranslate-tag

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

10/10 2017

dev-feature/add-get-env-variable-methods

dev-feature/add-get-env-variable-methods

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

10/10 2017

dev-feature/increase-timeout-for-account-operation

dev-feature/increase-timeout-for-account-operation

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

10/10 2017

dev-feature/product-custom-fields

dev-feature/product-custom-fields

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

15/09 2017

dev-feature/support-delete-product

dev-feature/support-delete-product

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

23/08 2017

dev-feature/progammatic-markup-generation

dev-feature/progammatic-markup-generation

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

21/08 2017

dev-release/programmatic-markup-generation

dev-release/programmatic-markup-generation

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

07/06 2017

dev-release/1.16.4

dev-release/1.16.4

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

07/06 2017

1.16.4

1.16.4.0

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

07/06 2017

dev-feature/add-merchant-traffic-info

dev-feature/add-merchant-traffic-info

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

05/06 2017
02/06 2017
02/06 2017

dev-feature/support-thumb-urls

dev-feature/support-thumb-urls

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

02/06 2017
01/06 2017
01/06 2017

dev-hotfix/fix-default-nosto-backend-issue

dev-hotfix/fix-default-nosto-backend-issue

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

23/05 2017
09/05 2017
08/05 2017
08/05 2017

dev-bugfix/sku-fixes

dev-bugfix/sku-fixes

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

08/05 2017
02/05 2017
28/04 2017

dev-feature/refactor-and-improve-sdk

dev-feature/refactor-and-improve-sdk

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

28/04 2017

dev-feature/add-crypt-interface

dev-feature/add-crypt-interface

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

12/04 2017

dev-bugfix/namespace-fix

dev-bugfix/namespace-fix

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

30/03 2017

1.16.3

1.16.3.0

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

30/03 2017

dev-hotfix/changelog

dev-hotfix/changelog

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

30/03 2017

dev-hotfix/1.16.3

dev-hotfix/1.16.3

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

07/03 2017

dev-release/2.5.0

dev-release/2.5.0

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires

07/03 2017

2.5.2

2.5.2.0

PHP SDK for developing Nosto modules for e-commerce platforms

  Sources   Download

BSD-3-Clause

The Development Requires