External promotions

How to manage custom promotions via external services

Besides the promotion types that are supported out-of-the-box (buy X pay Y, fixed amountarrow-up-right, fixed pricearrow-up-right, free giftarrow-up-right, free shippingarrow-up-right, and percentage discountarrow-up-right) Commerce Layer lets you integrate any kind of promotional engine as an external promotionarrow-up-right.

When an external promotion activates — i.e. its activation rules are met — Commerce Layer triggers a POST request to the promotion_url endpoint, sending the order payload (including its line items) in the request body.

Request

The request payload is a JSON:APIarrow-up-right compliant object you can query to perform your own computation. Aside from the target resourceorders in the specific case — some relationships are also included to avoid useless API roundtrips:

  • market

  • customer

  • customer.tags

  • line_items

  • line_items.item

  • shipping_address

  • billing_address

{
  "data": {
    "id": "wBXVhKzrnq",
    "type": "orders",
    "links": { ... },
    "attributes": { ... },
    "relationships": { ... },
    "meta": { ... }
  },
  "included": [
    {
      "id": "DvlGRmhdgX",
      "type": "markets",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    {
      "id": "bGvCXzYgNB",
      "type": "customers",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    {
      "id": "YawgnfmKad",
      "type": "tags",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    {
      "id": "kdPgtRXOKL",
      "type": "line_items",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    {
      "id": "XGZwpOSrWL",
      "type": "skus",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    }
    {
      "id": "BgnguJvXmb",
      "type": "addresses",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    {
      "id": "AlrkugwyVW",
      "type": "addresses",
      "links": { ... },
      "attributes": { ... },
      "relationships": { ... },
      "meta": { ... }
    },
    { ... }
  ]
}
circle-info

If needed, you can overwrite the default list of included resources by passing your custom list of order's relationships within the external_includes attribute of the external promotion at creation or update time (nested relationships are allowed using dot notation).

circle-exclamation

Response

Your service response (or error) must match the format described in the examples below.

Applying the discount to all taxable items of the order

The successful response must be a JSON object, returning at least the discount computed by the external logic. If needed, you can optionally enrich the response with some additional information, notification messages, and metadata:

Applying the discount to specific taxable items

The successful response must be a JSON object, returning at least the discount computed by the external logic to be applied to the specified line items. If needed, you can optionally enrich the response with some additional information, notification messages, and metadata:

circle-exclamation

Applying a specific discount to specific taxable items

The successful response must be a JSON object, returning at least a specific discount computed by the external logic to be applied to the specified line items. If needed, you can optionally enrich the response with some additional information, notification messages, and metadata:

circle-exclamation
circle-info

If you specify the messages key a notification with the related information is automatically attached to the associated order.

Security

When you create a new external promotion, a shared secret is generated. We recommend verifying the callback authenticity by signing the payload with that shared secret and comparing the result with the callback signature header.

Callbacks securitychevron-right

Last updated