2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.WebhookController do
|
|
|
|
|
use BerrypodWeb, :controller
|
2026-01-31 22:41:15 +00:00
|
|
|
|
2026-02-18 21:23:15 +00:00
|
|
|
alias Berrypod.Webhooks
|
2026-01-31 22:41:15 +00:00
|
|
|
|
|
|
|
|
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
|
2026-02-15 09:32:14 +00:00
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
|
Receives Printful webhook events.
|
|
|
|
|
|
|
|
|
|
Events:
|
|
|
|
|
- package_shipped - Package has been shipped
|
|
|
|
|
- order_failed - Order processing failed
|
|
|
|
|
- order_canceled - Order was canceled
|
|
|
|
|
- product_updated - Sync product was updated
|
|
|
|
|
- product_deleted - Sync product was deleted
|
|
|
|
|
"""
|
|
|
|
|
def printful(conn, params) do
|
|
|
|
|
event_type = params["type"]
|
|
|
|
|
data = params["data"] || %{}
|
|
|
|
|
|
|
|
|
|
Logger.info("Received Printful webhook: #{event_type}")
|
|
|
|
|
|
|
|
|
|
case Webhooks.handle_printful_event(event_type, data) do
|
|
|
|
|
:ok ->
|
|
|
|
|
json(conn, %{status: "ok"})
|
|
|
|
|
|
|
|
|
|
{:ok, _} ->
|
|
|
|
|
json(conn, %{status: "ok"})
|
|
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
|
Logger.warning("Printful webhook handling failed: #{inspect(reason)}")
|
|
|
|
|
# Return 200 to prevent Printful retrying
|
|
|
|
|
json(conn, %{status: "ok"})
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-01-31 22:41:15 +00:00
|
|
|
end
|