<?php
namespace App\Api;
use App\Middleware\ItemsMiddleware;
use App\Middleware\UserMiddleware;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Annotation\Route;
class ItemCallbackApi extends AbstractController
{
private $store_id;
private $item_id;
private $user_id;
private $hmac;
private $postdata;
private $userMiddleware;
public function __construct(
UserMiddleware $userMiddleware
) {
$this->userMiddleware = $userMiddleware;
$this->store_id = isset($_SERVER['HTTP_X_SHOPIFY_SHOP_DOMAIN']) ? $_SERVER['HTTP_X_SHOPIFY_SHOP_DOMAIN'] : null;
$this->item_id = isset($_SERVER['HTTP_X_SHOPIFY_PRODUCT_ID']) ? $_SERVER['HTTP_X_SHOPIFY_PRODUCT_ID'] : null;
$this->hmac = isset($_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']) ? $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'] : "";
$this->postdata = file_get_contents("php://input");
// $log_path = '/var/www/symfony/src/Api/shopify.log';
// $fp = fopen($log_path, 'a');
// fwrite($fp, '--------------Item Callback Starts-------------- ');
// fwrite($fp, PHP_EOL);
// fwrite($fp, json_encode($this->postdata));
// fwrite($fp, PHP_EOL);
// fwrite($fp, json_encode($_SERVER));
// fwrite($fp, PHP_EOL);
// fwrite($fp, PHP_EOL);
// fwrite($fp, '--------------Item Callback Ends-------------- ');
// fclose($fp);
}
public function verifyWebhook($data, $hmac)
{
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, $this->getParameter('SHOPIFY_SECRET'), true));
return hash_equals($calculated_hmac, $hmac);
}
/**
* @Route("/api/shopify/callback/create/item", name="api_shopify_callback_create_item", methods={"POST"})
*/
public function createItem(Request $request, ItemsMiddleware $itemsMiddleware): JsonResponse
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
} else if (!$this->hmac) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
} else if (!$this->postdata) {
throw new NotFoundHttpException("Request Body not present");
} else if (!$this->store_id) {
throw new NotFoundHttpException("Store Id not present");
} else if (!$this->item_id) {
throw new NotFoundHttpException("Item Id not present");
} else if (!$this->verifyWebhook($this->postdata, $this->hmac)) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
}
$shopifyUserData = $this->userMiddleware->get(array('store' => $this->store_id));
if (!$shopifyUserData || !$shopifyUserData->getId()) {
throw new NotFoundHttpException("No user-id found in store");
}
$this->user_id = $shopifyUserData->getId();
$parameter = array(
'storeId' => $this->store_id,
'itemId' => $this->item_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'accessToken' => $shopifyUserData->getAccessToken(),
'aedSync' => 'TO_BE_SYNCED',
'isUpdate' => false,
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($itemsMiddleware->webHookSave($parameter));
}
/**
* @Route("/api/shopify/callback/update/item", name="api_shopify_callback_update_item", methods={"POST"})
*/
public function updateItem(Request $request, ItemsMiddleware $itemsMiddleware): JsonResponse
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
} else if (!$this->hmac) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
} else if (!$this->postdata) {
throw new NotFoundHttpException("Request Body not present");
} else if (!$this->store_id) {
throw new NotFoundHttpException("Store Id not present");
} else if (!$this->item_id) {
throw new NotFoundHttpException("Item Id not present");
} else if (!$this->verifyWebhook($this->postdata, $this->hmac)) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
}
$shopifyUserData = $this->userMiddleware->get(array('store' => $this->store_id));
if (!$shopifyUserData || !$shopifyUserData->getId()) {
throw new NotFoundHttpException("No user-id found in store");
}
$this->user_id = $shopifyUserData->getId();
$parameter = array(
'storeId' => $this->store_id,
'itemId' => $this->item_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'accessToken' => $shopifyUserData->getAccessToken(),
'isUpdate' => true,
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($itemsMiddleware->webHookUpdate($parameter));
}
/**
* @Route("/api/shopify/callback/delete/item", name="api_shopify_callback_delete_item", methods={"POST"})
*/
public function deleteItem(Request $request, ItemsMiddleware $itemsMiddleware): JsonResponse
{
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
} else if (!$this->hmac) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
} else if (!$this->postdata) {
throw new NotFoundHttpException("Request Body not present");
} else if (!$this->store_id) {
throw new NotFoundHttpException("Store Id not present");
} else if (!$this->item_id) {
throw new NotFoundHttpException("Item Id not present");
} else if (!$this->verifyWebhook($this->postdata, $this->hmac)) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
}
$shopifyUserData = $this->userMiddleware->get(array('store' => $this->store_id));
if (!$shopifyUserData || !$shopifyUserData->getId()) {
throw new NotFoundHttpException("No user-id found in store");
}
$this->user_id = $shopifyUserData->getId();
$parameter = array(
'storeId' => $this->store_id,
'itemId' => $this->item_id,
'userId' => $this->user_id,
'isUpdate' => true,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'DELETE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($itemsMiddleware->save($parameter));
}
}