Giter VIP home page Giter VIP logo

vend-php's Introduction

vend-php

A simple Vend API client in PHP.

The canoncial repository for this stream of development is https://github.com/TeamOffshoot/vend-php

This API Client is still in a pre-1.0 state, so you can expect:

  • some bugs (feel free to submit a pull request with bug fixes and test coverage)
  • possibly some breaking API changes between v0.9 and v1.0

Requirements

  • PHP 5.3 (or higher)
  • ext-curl, ext-json
  • offshoot/http

Development Requirements

  • phpunit/phpunit 3.7

Getting Started

Install vend-php via Composer

Create a composer.json file if you don't already have one in your projects root directory and require vend-php:

{
  "require": {
    "offshoot/vend-php": "0.9.x"
  }
}

To learn more about Composer, including the complete installation process, visit http://getcomposer.org/

Using cURL

If you're using a cURL based HttpClient like the CurlHttpClient, you will want to include the cacert.pem file that can be found at http://curl.haxx.se/docs/caextract.html

You can add this as a dependency in your composer file. Your composer.json might look something like this:

{
  "require": {
    "offshoot/vend-php": "0.9.x",
    "haxx-se/curl": "1.0.0"
  },
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "haxx-se/curl",
        "version": "1.0.0",
        "dist": {
          "url": "http://curl.haxx.se/ca/cacert.pem",
          "type": "file"
        }
      }
    }
  ]
}

You will be able to find the cacert.pem file in vendor/haxx-se/curl/cacert.pem

Usage

Authentication

If you do not already have a Vend API Permanent Access Token, you will need you authenticate with the Vend API first

$pathToCertificateFile = 'vendor/haxx-se/curl/cacert.pem';
$httpClient = new \Offshoot\HttpClient\CurlHttpClient($pathToCertificateFile);
$redirector = new \Offshoot\Redirector\HeaderRedirector();
$authenticate = new \Vend\Api\AuthenticationGateway($httpClient, $redirector);

$authenticate->forStoreName('mycoolstore')
    ->usingClientId('XXX1234567890') // get this from your Vend Account
    ->andReturningTo('http://wherever.you/like')
    ->initiateLogin();

This will redirect your user to a Vend login screen where they will need to authenticate with their Vend credentials. After doing that, Vend will perform a GET request to your redirect URI, that will look like:

GET http://wherever.you/like?code=TEMP_TOKEN&domain_prefix=YOUR_STORE_NAME

Your application will need to capture the code query param from the request and use that to get the permanent access token from Vend

$client = new Vend\Api\Client($httpClient);
$client->setClientSecret('ABC123XYZ'); // get this from your Vend Account

// validate the Vend Request
if ($client->isValidRequest($_GET)) {

    // exchange the token
    $permanentAccessToken = $authenticate->forStoreName('mycoolshop')
        ->usingClientId('XXX1234567890')
        ->usingClientSecret('ABC123XYZ')
        ->toExchange($_GET['code']);

}

Interacting with the Vend API

require 'vendapi.php';

$request = new VendAPI\VendRequest(
  'https://shopname.vendhq.com',
  'username',
  'password',
  array(
    'CURLOPT_CAINFO' => 'path/to/your/cacert.pem'
  )
);

$vend = new VendAPI\VendAPI($request);
$products = $vend->getProducts();

NB this will only grab the first 20 or so results. To grab all results set $vend->automatic_depage to true

$vend->automatic_depage = true;
$products = $vend->getProducts();

Add a Product

$donut = new \VendAPI\VendProduct(null, $vend);
$donut->handle = 'donut01';
$donut->sku = '343434343';
$donut->retail_price = 2.99;
$donut->name = 'Donut w/ Sprinkles';
$donut->save();
echo 'Donut product id is '.$donut->id;

Add a Sale

$sale = new \VendAPI\VendSale(null, $vend);
$sale->register_id = $register_id;
$sale->customer_id = $customer_id;
$sale->status = 'OPEN';
$products = array();
foreach ($items as $item) {
    $products[] = array(
        'product_id' => $item->product_id,
        'quantity' => $item->quantity,
        'price' => $item->price
    );
}
$sale->register_sale_products = $products;
$sale->save();

echo "Created new order with id: ".$sale->id;

Other cool stuff

$vend->getProducts(array('active' => '1', 'since' => '2012-09-15 20:55:00'));

NB Check the vend api docs for supported search fields. If a search field isn't supported all results will be returned rather than the zero I was expecting

$coffee = $vend->getProduct('42c2ccc4-fbf4-11e1-b195-4040782fde00');
echo $coffee->name; // outputs "Hot Coffee"
if ($product->getInventory() == 0) {
  $coffee->setInventory(10);
  $coffee->name = 'Iced Coffee';
  $coffee->save();
}

Debugging

To debug make a call to the debug() function. eg:

$vend->debug(true);

Contributing

Contributions are welcome. Just fork the repository and send a pull request. Please be sure to include test coverage with your pull request. You can learn more about Pull Requests here

In order to run the test suite, ensure that the development dependencies have been installed via composer. Then from your command line, simple run:

vendor/bin/phpunit --bootstrap tests/bootstrap.php tests/

License

This library is released under the GPL 3.0 License

Acknowledgements

Thanks to Bruce Aldridge for his development of the initial code base.

vend-php's People

Contributors

brucealdridge avatar chriswoodford avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.