Bazaar.Handler behaviour (Bazaar v0.3.0)

Copy Markdown View Source

Behaviour for implementing UCP merchant handlers.

Implement this behaviour to define your commerce logic. The callbacks correspond to UCP capabilities (checkout, orders, identity).

Example

defmodule MyApp.Commerce.Handler do
  use Bazaar.Handler

  @impl Bazaar.Handler
  def capabilities, do: [:checkout, :orders]

  @impl Bazaar.Handler
  def create_checkout(params, conn) do
    case MyApp.Checkouts.create(params) do
      {:ok, checkout} -> {:ok, checkout}
      {:error, changeset} -> {:error, changeset}
    end
  end

  @impl Bazaar.Handler
  def get_checkout(id, _conn) do
    case MyApp.Checkouts.get(id) do
      nil -> {:error, :not_found}
      checkout -> {:ok, checkout}
    end
  end
end

Required Callbacks

Depending on which capabilities you declare, you must implement the corresponding callbacks:

Checkout Capability

  • create_checkout/2 - Create a new checkout session
  • get_checkout/2 - Retrieve a checkout session
  • update_checkout/3 - Update a checkout session
  • complete_checkout/2 - Complete a checkout session and create an order
  • cancel_checkout/2 - Cancel a checkout session

Orders Capability

  • get_order/2 - Retrieve an order
  • cancel_order/2 - Cancel an order

Catalog Capability

  • list_products/2 - List products with optional filters (category, limit, cursor)
  • get_product/2 - Get a single product by ID or SKU
  • search_products/2 - Search products by query string

Identity Capability

  • link_identity/2 - Link a user identity via OAuth

Summary

Types

capability()

@type capability() ::
  :checkout
  | :orders
  | :identity
  | :fulfillment
  | :discount
  | :buyer_consent
  | :catalog

conn()

@type conn() :: Plug.Conn.t()

id()

@type id() :: String.t()

params()

@type params() :: map()

Callbacks

business_profile()

@callback business_profile() :: map()

cancel_checkout(id, conn)

(optional)
@callback cancel_checkout(id(), conn()) :: {:ok, map()} | {:error, :not_found | term()}

cancel_order(id, conn)

(optional)
@callback cancel_order(id(), conn()) :: {:ok, map()} | {:error, :not_found | term()}

capabilities()

@callback capabilities() :: [capability()]

complete_checkout(id, conn)

(optional)
@callback complete_checkout(id(), conn()) ::
  {:ok, map()} | {:error, :not_found | :invalid_state | term()}

create_checkout(params, conn)

(optional)
@callback create_checkout(params(), conn()) :: {:ok, map()} | {:error, term()}

fulfillment_config()

(optional)
@callback fulfillment_config() :: map()

get_checkout(id, conn)

(optional)
@callback get_checkout(id(), conn()) :: {:ok, map()} | {:error, :not_found | term()}

get_order(id, conn)

(optional)
@callback get_order(id(), conn()) :: {:ok, map()} | {:error, :not_found | term()}

get_product(id, conn)

(optional)
@callback get_product(id(), conn()) :: {:ok, map()} | {:error, :not_found | term()}

handle_webhook(map)

(optional)
@callback handle_webhook(map()) :: {:ok, term()} | {:error, term()}

list_products(params, conn)

(optional)
@callback list_products(params(), conn()) :: {:ok, map()} | {:error, term()}

search_products(params, conn)

(optional)
@callback search_products(params(), conn()) :: {:ok, map()} | {:error, term()}

update_checkout(id, params, conn)

(optional)
@callback update_checkout(id(), params(), conn()) ::
  {:ok, map()} | {:error, :not_found | term()}

update_order(id, params, conn)

(optional)
@callback update_order(id(), params(), conn()) ::
  {:ok, map()} | {:error, :not_found | term()}