add Printful webhook endpoint with token verification

New POST /webhooks/printful route with VerifyPrintfulWebhook plug
(shared secret token via header or query param). Handles package_shipped,
order_failed, order_canceled, product_updated, product_synced, and
product_deleted events. Webhook registration via Printful v2 API with
token appended to URL. 19 new tests (819 total).

Also marks task #28 as done — Printful sync products already include
preview mockup images handled by the existing ImageDownloadWorker
pipeline.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey
2026-02-15 09:32:14 +00:00
parent 0cfcb2448e
commit 24d61f7a9e
8 changed files with 519 additions and 8 deletions

View File

@@ -504,6 +504,69 @@ defmodule SimpleshopTheme.Providers.Printful do
}
end
# =============================================================================
# Webhooks
# =============================================================================
@webhook_events [
"package_shipped",
"package_returned",
"order_failed",
"order_canceled",
"product_synced",
"product_updated",
"product_deleted"
]
@doc """
Registers webhooks with Printful for this store.
The webhook URL should include a token query param for verification,
e.g. `https://example.com/webhooks/printful?token=SECRET`.
"""
def register_webhooks(%ProviderConnection{config: config} = conn, webhook_url) do
store_id = config["store_id"]
secret = config["webhook_secret"]
cond do
is_nil(store_id) ->
{:error, :no_store_id}
is_nil(secret) or secret == "" ->
{:error, :no_webhook_secret}
true ->
with api_key when is_binary(api_key) <- ProviderConnection.get_api_key(conn),
:ok <- set_credentials(api_key, store_id) do
url_with_token = append_token(webhook_url, secret)
Client.setup_webhooks(url_with_token, @webhook_events)
else
nil -> {:error, :no_api_key}
end
end
end
@doc """
Lists currently registered webhooks for this store.
"""
def list_webhooks(%ProviderConnection{config: config} = conn) do
store_id = config["store_id"]
with api_key when is_binary(api_key) <- ProviderConnection.get_api_key(conn),
:ok <- set_credentials(api_key, store_id) do
Client.get_webhooks()
else
nil -> {:error, :no_api_key}
end
end
defp append_token(url, token) do
uri = URI.parse(url)
params = URI.decode_query(uri.query || "")
query = URI.encode_query(Map.put(params, "token", token))
URI.to_string(%{uri | query: query})
end
# =============================================================================
# Helpers
# =============================================================================