Skip to main content

Get Commercetools Order Number from Simple Key or Webhook Payload

You can implement a solution based on this recipe that retrieves an order from Commercetools, parsing the order identifier from either a typical DX Engine Context JSON format or from the structure of a webhook from the Adyen payment provider. This approach should apply to almost any commerce and payment system integration or any wherever incoming data formats contain the same data in different structures.

Overall Orchestration Flow

This solution depends on a Commercetools DX Connection. You can follow this recipe to create a Commercetools DX Connection.

When you control the format of the context passed to the DX Component that retrieves an order from Commercetools, you can use a simple key with the order number as its value such as the following:

{
"orderNumber": "H4HRQN9PGTGLNK82"
}

You can use the same component to retrieve an order when Adyen POSTs the order number as a webhook in a more complex structure such as the following, where merchantReference specifies the Commercetools order number:

{
"live": "false",
"notificationItems": [
{
"NotificationRequestItem": {
"eventCode": "CAPTURE",
"success": "true",
"eventDate": "2019-06-28T18:03:50+01:00",
"merchantAccountCode": "ConsciaECOM",
"pspReference": "7914073381342284",
"merchantReference": "H4HRQN9PGTGLNK82",
"amount": {
"value": 44986,
"currency": "USD"
}
}
}
]
}

The DX Component that retrieves the order passes the order number to the Commercetools Orders Webservice API.

Mapping Out DX Engine Components and Templates

This solution involves a single DX Engine Component: Get Commercetools Order (get-commercetools-order).

DX Engine Configuration Details

To create the Get Commercetools Order DX Engine Component, in the Conscia DX Engine UI:

  1. In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
  2. Click Add Component. The Create Component wizard appears.
  3. For Component Code, enter an identifier for the Component, such as get-commercetools-order.
  4. For Component Name, enter a name for the Component, such as Get Commercetools Order.
  5. Optionally, enter a Component Description.
  6. Select the No Rules checkbox. This Component does not support Experience Rules.
  7. For Component Type, select Conscia - Universal API Connector. This type of component can invoke almost any Webservice API.
  8. Click Submit.

To configure the Get Commercetools Order DX Engine Component, in the Manage Components page:

  1. Click the Edit button next to the Get Commercetools Order Component. The Edit Component wizard appears.
  2. Under Options, for Connection, select your Commercetools Connection.
  3. For Webservice Path, select JS Expression and enter the following value:
`/orders/order-number=${_.isEmpty(contextField('notificationItems')) ? contextField('orderNumber') : _.castArray(contextField('notificationItems'))[0].NotificationRequestItem.merchantReference}`
  1. Click Submit.

The expression specified for Webservice Path in this Component uses the Lodash isEmpty() function:

If the context passed to the DX Engine by an Webservice API call or a webhook contains a notificationItems key, then the request came from an Adyen webhook, and the merchantReference key in the first NotificationRequestItem entry of its payload provides the order number. Otherwise, the orderNumber key in the context passed in the Webservice API call provides the order number. In either case, this JavaScript expression appends the order number to the Commercetools Webservice URL.

Because orders can change at any time, in most cases, you should not cache the output of this DX Engine Component.