<?php
namespace App\Api;
use App\Middleware\OrdersMiddleware;
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 OrderCallbackApi extends AbstractController
{
private $store_id;
private $order_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->order_id = isset($_SERVER['HTTP_X_SHOPIFY_ORDER_ID']) ? $_SERVER['HTTP_X_SHOPIFY_ORDER_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, '--------------Order 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, '--------------Order 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/order", name="api_shopify_callback_create_order", methods={"POST"})
*/
public function createOrder(Request $request, OrdersMiddleware $ordersMiddleware): 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->order_id) {
throw new NotFoundHttpException("Order 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,
'orderId' => $this->order_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($ordersMiddleware->save($parameter));
}
/**
* @Route("/api/shopify/callback/update/order", name="api_shopify_callback_update_order", methods={"POST"})
*/
public function updateOrder(Request $request, OrdersMiddleware $ordersMiddleware): 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->order_id) {
throw new NotFoundHttpException("Order 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,
'orderId' => $this->order_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($ordersMiddleware->save($parameter));
}
/**
* @Route("/api/shopify/callback/delete/order", name="api_shopify_callback_delete_order", methods={"POST"})
*/
public function deleteOrder(Request $request, OrdersMiddleware $ordersMiddleware): 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->order_id) {
throw new NotFoundHttpException("Order 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,
'orderId' => $this->order_id,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($ordersMiddleware->save($parameter));
}
/**
* @Route("/api/shopify/callback/cancel/order", name="api_shopify_callback_cancel_order", methods={"POST"})
*/
public function cancelOrder(Request $request, OrdersMiddleware $ordersMiddleware): 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->order_id) {
throw new NotFoundHttpException("Order 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,
'orderId' => $this->order_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($ordersMiddleware->save($parameter));
}
/**
* @Route("/api/shopify/callback/payment/order", name="api_shopify_callback_payment_order", methods={"POST"})
*/
public function orderPayment(Request $request, OrdersMiddleware $ordersMiddleware): 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->order_id) {
throw new NotFoundHttpException("Order 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,
'orderId' => $this->order_id,
'json' => $this->postdata,
'userId' => $this->user_id,
'aedSync' => 'TO_BE_SYNCED',
'status' => 'ACTIVE',
'createdDate' => new \DateTime(),
'updatedDate' => new \DateTime()
);
return new JsonResponse($ordersMiddleware->save($parameter));
}
/**
* @Route("/api/shopify/callback/shop/update", name="api_shopify_callback_shop_update", methods={"POST"})
*/
public function updateShop(Request $request): 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->verifyWebhook($this->postdata, $this->hmac)) {
throw new UnauthorizedHttpException("Unauthorized Request", 401);
}
return new JsonResponse($this->postdata);
}
}