berrypod/lib/simpleshop_theme_web/controllers/webhook_controller.ex
jamey a9c15ea6ae feat: add Printify webhook endpoint for real-time product updates
- Add /webhooks/printify endpoint with HMAC-SHA256 signature verification
- Add Webhooks context to handle product:updated, product:deleted events
- Add ProductDeleteWorker for async product deletion
- Add webhook API methods to Printify client (create, list, delete)
- Add register_webhooks/2 to Printify provider
- Add mix register_webhooks task for one-time webhook registration
- Cache raw request body in endpoint for signature verification

Usage:
1. Generate webhook secret: openssl rand -hex 20
2. Add to provider connection config as "webhook_secret"
3. Register with Printify: mix register_webhooks https://yourshop.com/webhooks/printify

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 22:41:15 +00:00

37 lines
972 B
Elixir

defmodule SimpleshopThemeWeb.WebhookController do
use SimpleshopThemeWeb, :controller
alias SimpleshopTheme.Webhooks
require Logger
@doc """
Receives Printify webhook events.
Events:
- product:publish:started - Product publish initiated
- product:updated - Product was modified
- product:deleted - Product was deleted
- shop:disconnected - Shop was disconnected
"""
def printify(conn, params) do
event_type = params["type"] || params["event"]
resource = params["resource"] || params["data"] || %{}
Logger.info("Received Printify webhook: #{event_type}")
case Webhooks.handle_printify_event(event_type, resource) do
:ok ->
json(conn, %{status: "ok"})
{:ok, _} ->
json(conn, %{status: "ok"})
{:error, reason} ->
Logger.warning("Webhook handling failed: #{inspect(reason)}")
# Still return 200 to prevent Printify retrying
json(conn, %{status: "ok"})
end
end
end