From c8d5eb840cc5782956b1862c5cb024fd87fe65f4 Mon Sep 17 00:00:00 2001 From: sfinktah Date: Tue, 3 Oct 2023 11:47:59 +1100 Subject: [PATCH 1/3] added http_raw and delete webhook --- src/Webhooks/Delivery/HttpDelivery.php | 4 +- src/Webhooks/DeliveryMethod.php | 24 ++++++++++- src/Webhooks/Registry.php | 60 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/Webhooks/Delivery/HttpDelivery.php b/src/Webhooks/Delivery/HttpDelivery.php index 8d4686d9..f59a3233 100644 --- a/src/Webhooks/Delivery/HttpDelivery.php +++ b/src/Webhooks/Delivery/HttpDelivery.php @@ -13,7 +13,9 @@ class HttpDelivery extends DeliveryMethod */ public function getCallbackAddress(string $path): string { - return 'https://' . Context::$HOST_NAME . '/' . ltrim($path, '/'); + $rv = 'https://' . Context::$HOST_NAME . '/' . ltrim($path, '/'); + var_dump($rv); + return $rv; } /** 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 0a6a5825..7e2635a0 100644 --- a/src/Webhooks/Registry.php +++ b/src/Webhooks/Registry.php @@ -15,6 +15,7 @@ use Shopify\Utils; use Shopify\Webhooks\Delivery\EventBridge; use Shopify\Webhooks\Delivery\HttpDelivery; +use Shopify\Webhooks\Delivery\HttpDeliveryRaw; use Shopify\Webhooks\Delivery\PubSub; /** @@ -25,6 +26,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 = []; @@ -87,6 +89,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'"); } @@ -102,6 +107,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) { @@ -187,6 +206,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( + << Date: Thu, 19 Oct 2023 22:01:17 +1100 Subject: [PATCH 2/3] "raw" http endpoints & deletion for webhooks --- CHANGELOG.md | 1 + src/Webhooks/Delivery/HttpDelivery.php | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bebb6dd..7aa89e97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased +- [#2xx](https://github.com/Shopify/shopify-api-php/pull/2xx) [Minor] Raw URLs for Webhooks, deletion on empty path - [#297](https://github.com/Shopify/shopify-api-php/pull/297) [Patch] Fix CustomerAddress methods, FulfillmentRequest save method ## v5.1.0 - 2023-07-11 diff --git a/src/Webhooks/Delivery/HttpDelivery.php b/src/Webhooks/Delivery/HttpDelivery.php index f59a3233..8d4686d9 100644 --- a/src/Webhooks/Delivery/HttpDelivery.php +++ b/src/Webhooks/Delivery/HttpDelivery.php @@ -13,9 +13,7 @@ class HttpDelivery extends DeliveryMethod */ public function getCallbackAddress(string $path): string { - $rv = 'https://' . Context::$HOST_NAME . '/' . ltrim($path, '/'); - var_dump($rv); - return $rv; + return 'https://' . Context::$HOST_NAME . '/' . ltrim($path, '/'); } /** From 433dc906da89fbaa2339b579c1dab11dbc498325 Mon Sep 17 00:00:00 2001 From: sfinktah Date: Thu, 19 Oct 2023 22:16:17 +1100 Subject: [PATCH 3/3] updated PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aa89e97..db14ba9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased -- [#2xx](https://github.com/Shopify/shopify-api-php/pull/2xx) [Minor] Raw URLs for Webhooks, deletion on empty path +- [#304](https://github.com/Shopify/shopify-api-php/pull/304) [Minor] Raw URLs for Webhooks, deletion on empty path - [#297](https://github.com/Shopify/shopify-api-php/pull/297) [Patch] Fix CustomerAddress methods, FulfillmentRequest save method ## v5.1.0 - 2023-07-11