Skip to content

Commit

Permalink
Merchant API
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Dec 1, 2016
1 parent 7a2557f commit 8f9bb33
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
129 changes: 129 additions & 0 deletions aplazame/controllers/front/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/**
* @property Aplazame module
*/
class AplazameApiModuleFrontController extends ModuleFrontController
{
public static function forbidden()
{
return array(
'status_code' => 403,
'payload' => array(
'status' => 403,
'type' => 'FORBIDDEN',
),
);
}

public static function not_found()
{
return array(
'status_code' => 404,
'payload' => array(
'status' => 404,
'type' => 'NOT_FOUND',
),
);
}

public static function collection($page, $page_size, array $elements)
{
return array(
'status_code' => 200,
'payload' => array(
'query' => array(
'page' => $page,
'page_size' => $page_size,
),
'elements' => $elements,
),
);
}

public function postProcess()
{
$path = isset($_GET['path']) ? $_GET['path'] : '';
$pathArguments = isset($_GET['path_arguments']) ? json_decode($_GET['path_arguments'], true) : array();
$queryArguments = isset($_GET['query_arguments']) ? json_decode($_GET['query_arguments'], true) : array();

$response = $this->route($path, $pathArguments, $queryArguments);

http_response_code($response['status_code']);
header('Content-Type: application/json');

exit(Tools::jsonEncode($response['payload']));
}

/**
* @param string $path
* @param array $pathArguments
* @param array $queryArguments
*
* @return array
*/
public function route($path, array $pathArguments, array $queryArguments)
{
if (!$this->verify_authentication()) {
return self::forbidden();
}

switch ($path) {
case '/article/':
include_once _PS_MODULE_DIR_ . 'aplazame/controllers/front/Api/article.php';
$controller = new AplazameApiArticle(Db::getInstance());

return $controller->articles($queryArguments);
case '/order/{order_id}/history/':
include_once _PS_MODULE_DIR_ . 'aplazame/controllers/front/Api/order.php';
$controller = new AplazameApiOrder(Db::getInstance());

return $controller->history($pathArguments, $queryArguments);
default:
return self::not_found();
}
}

/**
* @return bool
*/
private function verify_authentication()
{
$privateKey = Configuration::get('APLAZAME_SECRET_KEY');

$authorization = $this->getHeaderAuthorization();
if (!$authorization || empty($privateKey)) {
return false;
}

return ($authorization === $privateKey);
}

private function getHeaderAuthorization()
{
if (function_exists('getallheaders')) {
$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_LOWER);
} else {
$headers = $this->getallheaders();
}

if (isset($headers['authorization'])) {
return trim(str_replace('Bearer', '', $headers['authorization']));
}

return false;
}

private function getallheaders()
{
$headers = '';
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
}
}

return $headers;
}
}
7 changes: 6 additions & 1 deletion aplazame/controllers/front/history.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ private function getCustomerHistory($customerId, $limit)
. ' ORDER BY id_order DESC LIMIT ' . $limit
);

return AplazameSerializers::getHistory($orders);
$historyOrders = array();
foreach ($orders as $orderData) {
$historyOrders[] = Aplazame_Aplazame_Api_BusinessModel_HistoricalOrder::createFromOrder(new Order($orderData['id_order']));
}

return $historyOrders;
}

private function getallheaders()
Expand Down
5 changes: 3 additions & 2 deletions aplazame/controllers/front/redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function postProcess()
$cart = $this->duplicateCart($cart);
}

$checkout = Aplazame_Aplazame_BusinessModel_Checkout::createFromCart($cart, (int) $this->module->id, $this->module->currentOrder);
$this->context->smarty->assign(array(
'aplazame_order' => AplazameSerializers::getCheckout($cart, (int) $this->module->id, $this->module->currentOrder),
'aplazame_order' => Aplazame_Sdk_Serializer_JsonSerializer::serializeValue($checkout),
));

if (_PS_VERSION_ < 1.7) {
Expand All @@ -49,7 +50,7 @@ private function orderExists($mid)
return false;
}

if ($response['is_error'] || empty($response['payload']['results'])) {
if (empty($response['results'])) {
return false;
}

Expand Down

0 comments on commit 8f9bb33

Please sign in to comment.