src/Api/ItemCallbackApi.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Api;
  3. use App\Middleware\ItemsMiddleware;
  4. use App\Middleware\UserMiddleware;
  5. use Exception;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class ItemCallbackApi extends AbstractController
  15. {
  16.   private $store_id;
  17.   private $item_id;
  18.   private $user_id;
  19.   private $hmac;
  20.   private $postdata;
  21.   private $userMiddleware;
  22.   public function __construct(
  23.     UserMiddleware $userMiddleware
  24.   ) {
  25.     $this->userMiddleware $userMiddleware;
  26.     $this->store_id = isset($_SERVER['HTTP_X_SHOPIFY_SHOP_DOMAIN']) ? $_SERVER['HTTP_X_SHOPIFY_SHOP_DOMAIN'] : null;
  27.     $this->item_id = isset($_SERVER['HTTP_X_SHOPIFY_PRODUCT_ID']) ? $_SERVER['HTTP_X_SHOPIFY_PRODUCT_ID'] : null;
  28.     $this->hmac = isset($_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']) ? $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'] : "";
  29.     $this->postdata file_get_contents("php://input");
  30.     // $log_path = '/var/www/symfony/src/Api/shopify.log';
  31.     // $fp = fopen($log_path, 'a');
  32.     // fwrite($fp, '--------------Item Callback Starts-------------- ');
  33.     // fwrite($fp, PHP_EOL);
  34.     // fwrite($fp, json_encode($this->postdata));
  35.     // fwrite($fp, PHP_EOL);
  36.     // fwrite($fp, json_encode($_SERVER));
  37.     // fwrite($fp, PHP_EOL);
  38.     // fwrite($fp, PHP_EOL);
  39.     // fwrite($fp, '--------------Item Callback Ends-------------- ');
  40.     // fclose($fp);
  41.   }
  42.   public function verifyWebhook($data$hmac)
  43.   {
  44.     $calculated_hmac base64_encode(hash_hmac('sha256'$data$this->getParameter('SHOPIFY_SECRET'), true));
  45.     return hash_equals($calculated_hmac$hmac);
  46.   }
  47.   /**
  48.    * @Route("/api/shopify/callback/create/item", name="api_shopify_callback_create_item", methods={"POST"})
  49.    */
  50.   public function createItem(Request $requestItemsMiddleware $itemsMiddleware): JsonResponse
  51.   {
  52.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  53.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  54.     } else if (!$this->hmac) {
  55.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  56.     } else if (!$this->postdata) {
  57.       throw new NotFoundHttpException("Request Body not present");
  58.     } else if (!$this->store_id) {
  59.       throw new NotFoundHttpException("Store Id not present");
  60.     } else if (!$this->item_id) {
  61.       throw new NotFoundHttpException("Item Id not present");
  62.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  63.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  64.     }
  65.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  66.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  67.       throw new NotFoundHttpException("No user-id found in store");
  68.     }
  69.     $this->user_id $shopifyUserData->getId();
  70.     $parameter = array(
  71.       'storeId' => $this->store_id,
  72.       'itemId' => $this->item_id,
  73.       'json' => $this->postdata,
  74.       'userId' => $this->user_id,
  75.       'accessToken' => $shopifyUserData->getAccessToken(),
  76.       'aedSync' => 'TO_BE_SYNCED',
  77.       'isUpdate' => false,
  78.       'status' => 'ACTIVE',
  79.       'createdDate' => new \DateTime(),
  80.       'updatedDate' => new \DateTime()
  81.     );
  82.     return new JsonResponse($itemsMiddleware->webHookSave($parameter));
  83.   }
  84.   /**
  85.    * @Route("/api/shopify/callback/update/item", name="api_shopify_callback_update_item", methods={"POST"})
  86.    */
  87.   public function updateItem(Request $requestItemsMiddleware $itemsMiddleware): JsonResponse
  88.   {
  89.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  90.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  91.     } else if (!$this->hmac) {
  92.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  93.     } else if (!$this->postdata) {
  94.       throw new NotFoundHttpException("Request Body not present");
  95.     } else if (!$this->store_id) {
  96.       throw new NotFoundHttpException("Store Id not present");
  97.     } else if (!$this->item_id) {
  98.       throw new NotFoundHttpException("Item Id not present");
  99.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  100.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  101.     }
  102.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  103.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  104.       throw new NotFoundHttpException("No user-id found in store");
  105.     }
  106.     $this->user_id $shopifyUserData->getId();
  107.     $parameter = array(
  108.       'storeId' => $this->store_id,
  109.       'itemId' => $this->item_id,
  110.       'json' => $this->postdata,
  111.       'userId' => $this->user_id,
  112.       'aedSync' => 'TO_BE_SYNCED',
  113.       'accessToken' => $shopifyUserData->getAccessToken(),
  114.       'isUpdate' => true,
  115.       'status' => 'ACTIVE',
  116.       'createdDate' => new \DateTime(),
  117.       'updatedDate' => new \DateTime()
  118.     );
  119.     return new JsonResponse($itemsMiddleware->webHookUpdate($parameter));
  120.   }
  121.   /**
  122.    * @Route("/api/shopify/callback/delete/item", name="api_shopify_callback_delete_item", methods={"POST"})
  123.    */
  124.   public function deleteItem(Request $requestItemsMiddleware $itemsMiddleware): JsonResponse
  125.   {
  126.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  127.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  128.     } else if (!$this->hmac) {
  129.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  130.     } else if (!$this->postdata) {
  131.       throw new NotFoundHttpException("Request Body not present");
  132.     } else if (!$this->store_id) {
  133.       throw new NotFoundHttpException("Store Id not present");
  134.     } else if (!$this->item_id) {
  135.       throw new NotFoundHttpException("Item Id not present");
  136.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  137.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  138.     }
  139.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  140.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  141.       throw new NotFoundHttpException("No user-id found in store");
  142.     }
  143.     $this->user_id $shopifyUserData->getId();
  144.     $parameter = array(
  145.       'storeId' => $this->store_id,
  146.       'itemId' => $this->item_id,
  147.       'userId' => $this->user_id,
  148.       'isUpdate' => true,
  149.       'aedSync' => 'TO_BE_SYNCED',
  150.       'status' => 'DELETE',
  151.       'createdDate' => new \DateTime(),
  152.       'updatedDate' => new \DateTime()
  153.     );
  154.     return new JsonResponse($itemsMiddleware->save($parameter));
  155.   }
  156. }