src/Api/OrderCallbackApi.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\Api;
  3. use App\Middleware\OrdersMiddleware;
  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 OrderCallbackApi extends AbstractController
  15. {
  16.   private $store_id;
  17.   private $order_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->order_id = isset($_SERVER['HTTP_X_SHOPIFY_ORDER_ID']) ? $_SERVER['HTTP_X_SHOPIFY_ORDER_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'--------------Order Callback Starts-------------- ');
  33.     fwrite($fpPHP_EOL);
  34.     fwrite($fpjson_encode($this->postdata));
  35.     fwrite($fpPHP_EOL);
  36.     fwrite($fpjson_encode($_SERVER));
  37.     fwrite($fpPHP_EOL);
  38.     fwrite($fpPHP_EOL);
  39.     fwrite($fp'--------------Order 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/order", name="api_shopify_callback_create_order", methods={"POST"})
  49.    */
  50.   public function createOrder(Request $requestOrdersMiddleware $ordersMiddleware): 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->order_id) {
  61.       throw new NotFoundHttpException("Order 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.       'orderId' => $this->order_id,
  73.       'json' => $this->postdata,
  74.       'userId' => $this->user_id,
  75.       'aedSync' => 'TO_BE_SYNCED',
  76.       'status' => 'ACTIVE',
  77.       'createdDate' => new \DateTime(),
  78.       'updatedDate' => new \DateTime()
  79.     );
  80.     return new JsonResponse($ordersMiddleware->save($parameter));
  81.   }
  82.   /**
  83.    * @Route("/api/shopify/callback/update/order", name="api_shopify_callback_update_order", methods={"POST"})
  84.    */
  85.   public function updateOrder(Request $requestOrdersMiddleware $ordersMiddleware): JsonResponse
  86.   {
  87.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  88.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  89.     } else if (!$this->hmac) {
  90.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  91.     } else if (!$this->postdata) {
  92.       throw new NotFoundHttpException("Request Body not present");
  93.     } else if (!$this->store_id) {
  94.       throw new NotFoundHttpException("Store Id not present");
  95.     } else if (!$this->order_id) {
  96.       throw new NotFoundHttpException("Order Id not present");
  97.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  98.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  99.     }
  100.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  101.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  102.       throw new NotFoundHttpException("No user-id found in store");
  103.     }
  104.     $this->user_id $shopifyUserData->getId();
  105.     $parameter = array(
  106.       'storeId' => $this->store_id,
  107.       'orderId' => $this->order_id,
  108.       'json' => $this->postdata,
  109.       'userId' => $this->user_id,
  110.       'aedSync' => 'TO_BE_SYNCED',
  111.       'status' => 'ACTIVE',
  112.       'createdDate' => new \DateTime(),
  113.       'updatedDate' => new \DateTime()
  114.     );
  115.     return new JsonResponse($ordersMiddleware->save($parameter));
  116.   }
  117.   /**
  118.    * @Route("/api/shopify/callback/delete/order", name="api_shopify_callback_delete_order", methods={"POST"})
  119.    */
  120.   public function deleteOrder(Request $requestOrdersMiddleware $ordersMiddleware): JsonResponse
  121.   {
  122.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  123.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  124.     } else if (!$this->hmac) {
  125.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  126.     } else if (!$this->postdata) {
  127.       throw new NotFoundHttpException("Request Body not present");
  128.     } else if (!$this->store_id) {
  129.       throw new NotFoundHttpException("Store Id not present");
  130.     } else if (!$this->order_id) {
  131.       throw new NotFoundHttpException("Order Id not present");
  132.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  133.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  134.     }
  135.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  136.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  137.       throw new NotFoundHttpException("No user-id found in store");
  138.     }
  139.     $this->user_id $shopifyUserData->getId();
  140.     $parameter = array(
  141.       'storeId' => $this->store_id,
  142.       'orderId' => $this->order_id,
  143.       'userId' => $this->user_id,
  144.       'aedSync' => 'TO_BE_SYNCED',
  145.       'status' => 'ACTIVE',
  146.       'createdDate' => new \DateTime(),
  147.       'updatedDate' => new \DateTime()
  148.     );
  149.     return new JsonResponse($ordersMiddleware->save($parameter));
  150.   }
  151.   /**
  152.    * @Route("/api/shopify/callback/cancel/order", name="api_shopify_callback_cancel_order", methods={"POST"})
  153.    */
  154.   public function cancelOrder(Request $requestOrdersMiddleware $ordersMiddleware): JsonResponse
  155.   {
  156.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  157.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  158.     } else if (!$this->hmac) {
  159.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  160.     } else if (!$this->postdata) {
  161.       throw new NotFoundHttpException("Request Body not present");
  162.     } else if (!$this->store_id) {
  163.       throw new NotFoundHttpException("Store Id not present");
  164.     } else if (!$this->order_id) {
  165.       throw new NotFoundHttpException("Order Id not present");
  166.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  167.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  168.     }
  169.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  170.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  171.       throw new NotFoundHttpException("No user-id found in store");
  172.     }
  173.     $this->user_id $shopifyUserData->getId();
  174.     $parameter = array(
  175.       'storeId' => $this->store_id,
  176.       'orderId' => $this->order_id,
  177.       'json' => $this->postdata,
  178.       'userId' => $this->user_id,
  179.       'aedSync' => 'TO_BE_SYNCED',
  180.       'status' => 'ACTIVE',
  181.       'createdDate' => new \DateTime(),
  182.       'updatedDate' => new \DateTime()
  183.     );
  184.     return new JsonResponse($ordersMiddleware->save($parameter));
  185.   }
  186.   /**
  187.    * @Route("/api/shopify/callback/payment/order", name="api_shopify_callback_payment_order", methods={"POST"})
  188.    */
  189.   public function orderPayment(Request $requestOrdersMiddleware $ordersMiddleware): JsonResponse
  190.   {
  191.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  192.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  193.     } else if (!$this->hmac) {
  194.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  195.     } else if (!$this->postdata) {
  196.       throw new NotFoundHttpException("Request Body not present");
  197.     } else if (!$this->store_id) {
  198.       throw new NotFoundHttpException("Store Id not present");
  199.     } else if (!$this->order_id) {
  200.       throw new NotFoundHttpException("Order Id not present");
  201.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  202.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  203.     }
  204.     $shopifyUserData $this->userMiddleware->get(array('store' => $this->store_id));
  205.     if (!$shopifyUserData || !$shopifyUserData->getId()) {
  206.       throw new NotFoundHttpException("No user-id found in store");
  207.     }
  208.     $this->user_id $shopifyUserData->getId();
  209.     $parameter = array(
  210.       'storeId' => $this->store_id,
  211.       'orderId' => $this->order_id,
  212.       'json' => $this->postdata,
  213.       'userId' => $this->user_id,
  214.       'aedSync' => 'TO_BE_SYNCED',
  215.       'status' => 'ACTIVE',
  216.       'createdDate' => new \DateTime(),
  217.       'updatedDate' => new \DateTime()
  218.     );
  219.     return new JsonResponse($ordersMiddleware->save($parameter));
  220.   }
  221.   /**
  222.    * @Route("/api/shopify/callback/shop/update", name="api_shopify_callback_shop_update", methods={"POST"})
  223.    */
  224.   public function updateShop(Request $request): JsonResponse
  225.   {
  226.     if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  227.       throw new MethodNotAllowedHttpException(["error" => "Invalid Request Method"], 405);
  228.     } else if (!$this->hmac) {
  229.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  230.     } else if (!$this->postdata) {
  231.       throw new NotFoundHttpException("Request Body not present");
  232.     } else if (!$this->verifyWebhook($this->postdata$this->hmac)) {
  233.       throw new UnauthorizedHttpException("Unauthorized Request"401);
  234.     }
  235.     return new JsonResponse($this->postdata);
  236.   }
  237. }