diff --git a/CHANGELOG.md b/CHANGELOG.md index a6015805..a0084e39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased +- [#304](https://github.com/Shopify/shopify-api-php/pull/304) [Minor] Raw URLs for Webhooks, deletion on empty path + ## v5.5.1 - 2024-05-24 - [#345](https://github.com/Shopify/shopify-api-php/pull/345) [Patch] Stop storing a session in the database when beginning OAuth, only when completing it diff --git a/src/Webhooks/DeliveryMethod.php b/src/Webhooks/DeliveryMethod.php index 89b3becb..a33487f7 100644 --- a/src/Webhooks/DeliveryMethod.php +++ b/src/Webhooks/DeliveryMethod.php @@ -80,6 +80,27 @@ public function buildRegisterQuery( QUERY; } + public function buildDeleteQuery( + string $topic, + string $callbackAddress, + string $webhookId + ): string { + $mutationName = $this->getDeletionMutationName(); + $identifier = "id: \"$webhookId\""; + + return <<getMutationName($webhookId)]['webhookSubscription']); + return !empty($result['data'][$this->getMutationName($webhookId)]['webhookSubscription']) + || !empty($result['data'][$this->getDeletionMutationName()]['deletedWebhookSubscriptionId']); } } diff --git a/src/Webhooks/Registry.php b/src/Webhooks/Registry.php index 37fef0d5..7f000cd8 100644 --- a/src/Webhooks/Registry.php +++ b/src/Webhooks/Registry.php @@ -19,6 +19,7 @@ use Shopify\Utils; use Shopify\Webhooks\Delivery\EventBridge; use Shopify\Webhooks\Delivery\HttpDelivery; +use Shopify\Webhooks\Delivery\HttpDeliveryRaw; use Shopify\Webhooks\Delivery\PubSub; /** @@ -29,6 +30,7 @@ final class Registry public const DELIVERY_METHOD_HTTP = 'http'; public const DELIVERY_METHOD_EVENT_BRIDGE = 'eventbridge'; public const DELIVERY_METHOD_PUB_SUB = 'pubsub'; + public const DELIVERY_METHOD_RAW = 'raw'; /** @var Handler[] */ private static $REGISTRY = []; @@ -91,6 +93,9 @@ public static function register( case self::DELIVERY_METHOD_HTTP: $method = new HttpDelivery(); break; + case self::DELIVERY_METHOD_RAW: + $method = new HttpDeliveryRaw(); + break; default: throw new InvalidArgumentException("Unrecognized delivery method '$deliveryMethod'"); } @@ -106,6 +111,20 @@ public static function register( $method ); + // unregister if empty path is passed (not sure what $webhookId is + // when no registration exists, but assuming it will be false-ish + if (empty($path) && $webhookId) { + $body = self::sendDeleteRequest( + $client, + $topic, + $callbackAddress, + $method, + $webhookId + ); + $registered = $method->isSuccess($body, $webhookId); + return new RegisterResponse($registered, $body); + } + $registered = true; $body = null; if ($mustRegister) { @@ -191,6 +210,7 @@ private static function isWebhookRegistrationNeeded( $checkStatusCode = $checkResponse->getStatusCode(); $checkBody = $checkResponse->getDecodedBody(); + print_r($checkBody); if ($checkStatusCode !== 200) { throw new WebhookRegistrationException( <<query( + data: $deliveryMethod->buildDeleteQuery($topic, $callbackAddress, $webhookId), + ); + + $statusCode = $deleteResponse->getStatusCode(); + $body = $deleteResponse->getDecodedBody(); + if ($statusCode !== 200) { + throw new WebhookRegistrationException( + <<