diff --git a/composer.json b/composer.json index e14ceba1..27e20cec 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "funeralzone/valueobjects": "^0.5", "jenssegers/agent": "^2.6", "laravel/framework": "^7.0 || ^8.0", - "osiset/basic-shopify-api": "^9.0 || ^10.0" + "osiset/basic-shopify-api": "dev-master" }, "require-dev": { "ergebnis/composer-normalize": "^2.8", diff --git a/src/ShopifyApp/Actions/DispatchWebhooks.php b/src/ShopifyApp/Actions/DispatchWebhooks.php index f8ffa759..cf7883a4 100644 --- a/src/ShopifyApp/Actions/DispatchWebhooks.php +++ b/src/ShopifyApp/Actions/DispatchWebhooks.php @@ -66,10 +66,17 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool $webhooks ); } else { + if (defined("{$this->jobClass}::DELAY_SECONDS")) { + $delayTime = now()->addSecond(constant("{$this->jobClass}::DELAY_SECONDS")); + } else { + $delayTime = now(); + } + ($this->jobClass)::dispatch( $shop->getId(), $webhooks - )->onQueue(getShopifyConfig('job_queues')['webhooks']); + )->delay($delayTime) + ->onQueue(getShopifyConfig('job_queues')['webhooks']); } return true; diff --git a/src/ShopifyApp/Traits/WebhookController.php b/src/ShopifyApp/Traits/WebhookController.php index efbf179d..056cef45 100644 --- a/src/ShopifyApp/Traits/WebhookController.php +++ b/src/ShopifyApp/Traits/WebhookController.php @@ -2,6 +2,7 @@ namespace Osiset\ShopifyApp\Traits; +use App\Jobs\ShopifyWebhooks\AbstractShopifyWebhookJob; use Illuminate\Http\Request; use Illuminate\Http\Response as ResponseResponse; use Illuminate\Support\Facades\Response; @@ -24,12 +25,17 @@ public function handle(string $type, Request $request): ResponseResponse { // Get the job class and dispatch $jobClass = getShopifyConfig('job_namespace').str_replace('-', '', ucwords($type, '-')).'Job'; - $jobData = json_decode($request->getContent()); + + if (defined("{$jobClass}::DELAY_SECONDS")) { + $delayTime = now()->addSecond(constant("{$jobClass}::DELAY_SECONDS")); + } else { + $delayTime = now(); + } $jobClass::dispatch( $request->header('x-shopify-shop-domain'), - $jobData - )->onQueue(getShopifyConfig('job_queues')['webhooks']); + AbstractShopifyWebhookJob::compressPayload($request->getContent()) + )->delay($delayTime)->onQueue(getShopifyConfig('job_queues')['webhooks']); return Response::make('', 201); }