2017 © Pedro Peláez
 

library php-sdk

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

image

moltin/php-sdk

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  • Thursday, February 8, 2018
  • by moltin
  • Repository
  • 11 Watchers
  • 48 Stars
  • 4,786 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 20 Forks
  • 3 Open issues
  • 28 Versions
  • 5 % Grown

The README.md

moltin PHP SDK

This official PHP SDK for interacting with moltin., (*1)

NOTE: master is designed to access V2 of the API. If you want legacy access to V1 of the API, please use the most recent 1.x.x tag, (*2)

Installation

You can install the package manually or by adding it to your composer.json:, (*3)

{
  "require": {
      "moltin/php-sdk": "^2.0.0"
  }
}

Instantiating the SDK Client:

Pass in the configuration to the client:, (*4)

$config = [
    'client_id' => '{your_client_id}',
    'client_secret' => '{your_client_secret}',
    'currency_code' => 'USD',
    'language' => 'en',
    'locale' => 'en_gb',
    'cookie_cart_name' => 'moltin_cart_reference',
    'cookie_lifetime' => '+28 days'
];
$moltin = new Moltin\Client($config);

Or configure after construct:, (*5)

$moltin = new Moltin\Client()
            ->setClientID('xxx')
            ->setClientSecret('yyy')
            ->setCurrencyCode('USD')
            ->setLanguage('en')
            ->setLocale('en_gb')
            ->setCookieCartName('moltin_cart_reference')
            ->setCookieLifetime('+28 days');

Note: if you are unsure what your client_id or client_secret are, please select the store in your account and copy them., (*6)

Enterprise Customers

If you are an enterprise customer and have your own infrastructure with your own domain, you can configure the client to use your domain:, (*7)

$moltin->setBaseURL('https://api.yourdomain.com');

Or by adding the api_endpoint field to the $config array you pass to the constructor., (*8)

Using the client

Multiple Resources

To return a list of your resources (limited to 100 depending your store configuration):, (*9)

// return a list of your products 
$moltin->products->all();

// return your brands
$moltin->brands->all();

// return your categories
$moltin->categories->all();

// return your collections
$moltin->collections->all();

// return your files
$moltin->files->all();

Single Resource by ID

Fetch a Resource by ID:, (*10)

$moltin->products->get($productID);

Fetching the category/brand/collection tree

Categories, brands and collections can be nested to create a tree structure (see the CategoryRelationships example)., (*11)

You can retrieve a full tree of the items rather than having to build them by using tree method:, (*12)

$moltin->categories->tree();

Limiting and Offsetting Results

// limit the number of resources returned:
$moltin->products->limit(10)->all();

// offset the results (page 2):
$moltin->products->limit(10)->offset(10)->all();

Sorting Results

// order by `name`:
$moltin->products->sort('name')->all();

// reversed:
$moltin->products->sort('-name')->all();

Filtering Results

To filter your results when calling resources which support it (e.g. /v2/products)., (*13)

A simple filter to get all products which are in stock may look like this:, (*14)

$moltin->products->filter([
    'gt' => ['stock' => 0]
])->all();

A more advanced filter to find products which are digital, drafted and have a stock greater than 20 would look like this:, (*15)

$moltin->products->filter(new \Moltin\Filter([
    'eq' => ['status' => 'draft', 'commodity_type' => 'digital'],
    'gt' => ['stock' => 20]
])->all();

The array passed to the filter method should contain all of the conditions required to be met by the filter on the API and allow you to use several filters of the same type (as demostrated above)., (*16)

For more information on the filter operations please read the API reference., (*17)

Including data

To include other data in your request (such as products when getting a category) call the with() method on the resource:, (*18)

$response = $moltin->categories->with(['products'])->get($categoryID);
$category = $response->data();
$products = $response->included()->products;

Create Relationships

// create relationships between resources:
$moltin->products->createRelationships($productID, 'categories', [$categoryID]);

// delete a relationship between resources:
$moltin->products->deleteRelationships($productID, 'categories', [$categoryID]);

// (Or an update with an empty array achieves the same result if you're so inclined):
$moltin->products->updateRelationships($productID, 'categories', []);

Requesting a Specific Currency

For calls that support the X-MOLTIN-CURRENCY header, you can specifiy it on the client:, (*19)

$moltin->currency('USD')->products->all();
$moltin->currency('GBP')->products->all();

Working with files

A POST request to the v2/files endpoint allows you to upload a file and store it remotely., (*20)

To create a file using the SDK, you need to have the file on disk:, (*21)

// create a file from a local disk
$moltin->files->create(['public' => 'true', 'file' => '/path/to/file.jpg']);

// create a file from a URL (note: this will download the file to your local disk then upload)
$moltin->files->create(['public' => 'true', 'file' => 'https://placeholdit.imgix.net/~text?&w=350&h=150']);

Integrations

$moltin->integrations->all();
$moltin->integrations->create([
    'type' => 'integration',
    'integration_type' => 'webhook',
    'enabled' => true,
    'name' => 'My Webhook Name',
    'description' => 'An example webhook integration from the SDK',
    'observes' => [
        'product.created',
        'product.updated',
        'product.deleted'
    ],
    'configuration' => [
        'url' => 'https://your.domain.com/webhooks',
        'secret' => 'opensesame'
    ]
]);
$moltin->integrations->update('55f33a71-b45a-4c30-a872-6e6a0f442af1', [
    'data' => [
        'id' => '55f33a71-b45a-4c30-a872-6e6a0f442af1',
        'type' => 'integration',
        'integration_type' => 'webhook',
        'enabled' => false,
        'name' => 'Updated Webhook Name',
        'description' => 'An updated example webhook integration from the SDK',
        'observes' => [
            'product.deleted'
        ],
        'configuration' => [
            'url' => 'https://your.domain.com/webhooks',
            'secret' => 'opensesame'
        ]
    ]
]);
$moltin->integrations->get('55f33a71-b45a-4c30-a872-6e6a0f442af1');
$moltin->integrations->delete('55f33a71-b45a-4c30-a872-6e6a0f442af1');

Getting Logs

// get all integration logs for your store:
$logs = $moltin->integrations->getLogs()->data();

// get all jobs for an integration:
$jobs = $moltin->integrations->getJobs($integrationID)->data();

// get all logs for job:
$logs = $moltin->integrations->getLogs($integrationID, $jobID)->data();

Gateways

Your payment gateways can be managed using the SDK., (*22)

// get all
$gateways = $moltin->gateways->all();

// update
$moltin->gateways->update('stripe', [
    'data' => [
        'enabled': true,
        'login': 'your_stripe_login'
    ]
])

Carts, Orders and Payments

To simplify the way you process carts, orders and payments, we provide some utility functions., (*23)

Carts

Adding items to a cart:, (*24)

$cartReference = 'a_unique_refeference'; // supply a custom cart reference
$moltin->cart($cartReference)->addProduct($productID); // adds 1 x $productID
$moltin->cart($cartReference)->addProduct($productID, 3); // adds 3 x $productID (now has 4)

When no cart reference is supplied, we will get it from the $_COOKIE. You can change the name used in the $_COOKIE by passing it in the config when instantiating the client., (*25)

This is therefore a valid call (although the cart will be a new one if you follow on from the example above):, (*26)

$moltin->cart()->addProduct($productID);

Get the cart items:, (*27)

foreach($moltin->cart()->items() as $item) {
    $cartItemID = $item->id;
    // ... do something
    echo $item->name . "\n";
}

Update the quantity of an item in the cart:, (*28)

$moltin->cart()->updateItemQuantity($cartItemID, 2); // now has 2

Remove an item from the cart:, (*29)

$moltin->cart()->removeItem($cartItemID);

Orders

To convert a cart to an order (which can then be paid):, (*30)

$customer = [ 
    // ... customer data
];
$billing = [
    // ... billing data
];
$shipping = [
    // ... shipping data
];
$order = $moltin->cart($cartReference)->checkout($customer, $billing, $shipping);

Payments

When you have a Moltin\Entities\Order object from the checkout method you can take a payment for it:, (*31)

$gatewaySlug = 'stripe'; // the slug of your payment gateway
$paymentMethod = 'purchase'; // the order payment method (purchase is supported for now)
$params = []; // payment params (these will vary depending on the gateway, check out the example for Stripe and the docs for others)
$payment = $order->pay($gatewaySlug, $paymentMethod, $params);

You can now check the response ($payment) to see what happened with your payment., (*32)

You can also setup test details for most payment gateways, please refer to them for their details and check out the example for more informatiom on cart -> order -> pay., (*33)

Handling Exceptions

Aside from errors that may occur due to the call, there may be other Exceptions thrown. To handle them, simply wrap your call in a try catch block:, (*34)

try {
    $moltin->products->all();
} catch (Exception $e) {
    // do something with $e
}

Internally, there are several custom Exceptions which may be raised - see the Exceptions directory for more information., (*35)

Examples

In the examples directory there are command line implementations using the SDK. To use the examples you will need to:, (*36)

  • Install dependencies with composer install
  • Copy the examples/.env.tmpl to examples/.env and add your credentials to it.
  • Run the example file you want, for example: php ./ProductList.php

By default, for successful calls, we will display the data in a table on the command line. You can, however, use a flag to view the response:, (*37)

php ./ProductList.php format=json, (*38)

Test

phpunit

Generate a coverage report:, (*39)

phpunit --coverage-html ./ignore

The Versions

08/02 2018

dev-master

9999999-dev

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

08/02 2018

dev-docs/fix-link-to-filtering

dev-docs/fix-link-to-filtering

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

11/10 2017

2.0.2

2.0.2.0

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

18/09 2017

dev-revert-63-feature/variations-as-sub-resources

dev-revert-63-feature/variations-as-sub-resources

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

15/08 2017

dev-feature/variations-as-sub-resources

dev-feature/variations-as-sub-resources

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

31/07 2017

dev-new/list-orders-items

dev-new/list-orders-items

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

31/07 2017

2.0.1

2.0.1.0

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

06/04 2017

2.0.0

2.0.0.0

The Moltin PHP SDK is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

01/03 2017

v2.x-dev

2.9999999.9999999.9999999-dev

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

 

The Development Requires

12/10 2016

v1.x-dev

1.9999999.9999999.9999999-dev

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

12/10 2016

1.5

1.5.0.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

17/05 2016

1.4-beta

1.4.0.0-beta

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

17/05 2016

1.3

1.3.0.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

07/05 2016

dev-fix/refresh-token

dev-fix/refresh-token

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

08/04 2016

dev-develop

dev-develop

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

08/04 2016

dev-hotfix/old-refresh-token-being-use-instead-new-one

dev-hotfix/old-refresh-token-being-use-instead-new-one

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

08/04 2016

dev-chg/support-error-and-errors-keys

dev-chg/support-error-and-errors-keys

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

05/04 2016

dev-hotfix/array-to-string-conversion

dev-hotfix/array-to-string-conversion

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.4.0

 

The Development Requires

26/01 2016

1.2.0

1.2.0.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

12/01 2016

1.1.1

1.1.1.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

14/12 2015

1.1

1.1.0.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

04/11 2015

1.0.2

1.0.2.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

19/10 2015

dev-version1

dev-version1

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

19/10 2015

1.0.1

1.0.1.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

17/10 2015

1.0.0

1.0.0.0

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

06/10 2015

dev-short-array-syntax

dev-short-array-syntax

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

06/05 2015

dev-beta

dev-beta

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

20/04 2015

dev-feature/error-handle

dev-feature/error-handle

The Moltin php-sdk is a simple to use interface for the API to help you get off the ground quickly and efficiently

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires