Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit b222f1c

Browse files
committed
+
0 parents  commit b222f1c

17 files changed

Lines changed: 1320 additions & 0 deletions

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2016 Chec (Chec Commerce, Inc)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

autoload.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* An example of a project-specific implementation.
4+
*
5+
* After registering this autoload function with SPL, the following line
6+
* would cause the function to attempt to load the \Commerce\Baz\Qux class
7+
* from /path/to/project/lib/Baz/Qux.php:
8+
*
9+
* new \Commerce\Baz\Qux;
10+
*
11+
* @param string $class The fully-qualified class name.
12+
* @return void
13+
*/
14+
spl_autoload_register(function ($class) {
15+
16+
// project-specific namespace prefix
17+
$prefix = 'Commerce\\';
18+
19+
// base directory for the namespace prefix
20+
$base_dir = __DIR__ . '/lib/';
21+
22+
// does the class use the namespace prefix?
23+
$len = strlen($prefix);
24+
if (strncmp($prefix, $class, $len) !== 0) {
25+
// no, move to the next registered autoloader
26+
return;
27+
}
28+
29+
// get the relative class name
30+
$relative_class = substr($class, $len);
31+
32+
// replace the namespace prefix with the base directory, replace namespace
33+
// separators with directory separators in the relative class name, append
34+
// with .php
35+
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
36+
37+
// if the file exists, require it
38+
if (file_exists($file)) {
39+
require $file;
40+
}
41+
});

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "chec/commerce.php",
3+
"version": "1.0.0",
4+
"description": "PHP client library for Commerce.js by Chec",
5+
"keywords": [
6+
"chec",
7+
"commerce.js",
8+
"ecommerce",
9+
"shopping cart",
10+
"api",
11+
"sdk"
12+
],
13+
"homepage": "https://commercejs.com/",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Chec Commerce, Inc.",
18+
"homepage": "https://github.com/chec"
19+
}
20+
],
21+
"require": {
22+
"php": ">=5.4.0",
23+
"ext-curl": "*",
24+
"ext-json": "*",
25+
"ext-mbstring": "*",
26+
"mashape/unirest-php": "3.0.4",
27+
"apimatic/jsonmapper": "~1.0.0"
28+
},
29+
"autoload": {
30+
"psr-4": { "Commerce\\" : "lib/" }
31+
}
32+
}

lib/Auth.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Commerce;
4+
5+
class Auth
6+
{
7+
// @var string The Chec API key to be used for requests.
8+
public static $apiKey;
9+
10+
// @var string The base URL for the Chec API.
11+
public static $apiBase = 'https://api.chec.io/v1';
12+
13+
// @var string The base URL for the Chec API uploads endpoint.
14+
public static $apiUploadBase = 'https://uploads.chec.io';
15+
16+
// @var string|null The version of the Chec API to use for requests.
17+
public static $apiVersion = null;
18+
19+
public static $userAgent = 'Commerce.php/v1';
20+
21+
const VERSION = '1.0.0';
22+
23+
public static function getApiKey()
24+
{
25+
return self::$apiKey;
26+
}
27+
28+
public static function setApiKey($apiKey)
29+
{
30+
self::$apiKey = $apiKey;
31+
}
32+
33+
public static function getApiVersion()
34+
{
35+
return self::$apiVersion;
36+
}
37+
38+
public static function setApiVersion($apiVersion)
39+
{
40+
self::$apiVersion = $apiVersion;
41+
}
42+
43+
}

lib/Cart.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Commerce;
4+
5+
use Commerce\Http\ApiHelper;
6+
7+
class Cart extends Http\ApiResource
8+
{
9+
/**
10+
* Commerce\Cart::add();
11+
*/
12+
public static function add($id,$parameters, $query_parameters = [])
13+
{
14+
$_queryBuilder = Auth::$apiBase.'/carts/{id}';
15+
$path_params = ['id' => $id];
16+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
17+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
18+
return Http\ApiResource::Request('POST', $_queryUrl, $parameters);
19+
}
20+
/**
21+
* Commerce\Cart::contents();
22+
*/
23+
public static function contents($id, $query_parameters = [])
24+
{
25+
$_queryBuilder = Auth::$apiBase.'/carts/{id}/items';
26+
$path_params = ['id' => $id];
27+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
28+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
29+
30+
return Http\ApiResource::Request('GET', $_queryUrl);
31+
}
32+
/**
33+
* Commerce\Cart::create();
34+
*/
35+
public static function create( $query_parameters = [])
36+
{
37+
$_queryBuilder = Auth::$apiBase.'/carts';
38+
39+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
40+
41+
return Http\ApiResource::Request('GET', $_queryUrl);
42+
}
43+
/**
44+
* Commerce\Cart::delete();
45+
*/
46+
public static function delete($id, $query_parameters = [])
47+
{
48+
$_queryBuilder = Auth::$apiBase.'/carts/{id}';
49+
$path_params = ['id' => $id];
50+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
51+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
52+
53+
return Http\ApiResource::Request('DELETE', $_queryUrl);
54+
}
55+
/**
56+
* Commerce\Cart::remove();
57+
*/
58+
public static function remove($id,$lineItemId, $query_parameters = [])
59+
{
60+
$_queryBuilder = Auth::$apiBase.'/carts/{id}/items/{line_item_id}';
61+
$path_params = ['id' => $id,'line_item_id' => $lineItemId];
62+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
63+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
64+
65+
return Http\ApiResource::Request('DELETE', $_queryUrl);
66+
}
67+
/**
68+
* Commerce\Cart::reset();
69+
*/
70+
public static function reset($id, $query_parameters = [])
71+
{
72+
$_queryBuilder = Auth::$apiBase.'/carts/{id}/items';
73+
$path_params = ['id' => $id];
74+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
75+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
76+
77+
return Http\ApiResource::Request('DELETE', $_queryUrl);
78+
}
79+
/**
80+
* Commerce\Cart::retrieve();
81+
*/
82+
public static function retrieve($id, $query_parameters = [])
83+
{
84+
$_queryBuilder = Auth::$apiBase.'/carts/{id}';
85+
$path_params = ['id' => $id];
86+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
87+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
88+
89+
return Http\ApiResource::Request('GET', $_queryUrl);
90+
}
91+
/**
92+
* Commerce\Cart::update();
93+
*/
94+
public static function update($id,$lineItemId,$parameters, $query_parameters = [])
95+
{
96+
$_queryBuilder = Auth::$apiBase.'/carts/{id}/items/{line_item_id}';
97+
$path_params = ['id' => $id,'line_item_id' => $lineItemId];
98+
Http\ApiHelper::appendUrlWithTemplateParameters($_queryBuilder, $path_params);
99+
$_queryUrl = ApiHelper::cleanUrl($_queryBuilder);
100+
return Http\ApiResource::Request('PUT', $_queryUrl, $parameters);
101+
}
102+
private static function val($arr, $key, $default = NULL)
103+
{
104+
if(isset($arr[$key])) {
105+
return is_bool($arr[$key]) ? var_export($arr[$key], true) : $arr[$key];
106+
}
107+
return $default;
108+
}
109+
}

0 commit comments

Comments
 (0)