External order validation
How to validate orders via external services
Last updated
How to validate orders via external services
Last updated
Sometimes, you may need to perform custom validation on orders, as an additional step before the order placement. For example, this could be useful in case you want to support different or more complex validation rules specific to your business logic.
If you want to use the external order validation feature in a , fill in the market's external_order_validation_url
field with your external service endpoint and make sure your service is compliant with the specifications described below.
When you want to validate an order based on your external logic, it and set the _validate
attribute to true
:
Upon this call, Commerce Layer triggers a POST
request to the specified external_validation_url
endpoint, sending the order payload (including the related line items) in the request body.
To check if an external order validation has been triggered you can inspect the order's validations_count
attribute and make sure it gets incremented.
The request payload is a JSON:API-compliant object you can query to perform your own computation. Aside from the target resource — order
in this specific case — some relationships are also included to avoid useless API roundtrips:
billing_address
shipping_address
line_items.item
line_items
market
customer
shipments
shipments.shipping_method
In case the call to your external endpoint goes timeout, responds with an internal server error, or is blocked by the open circuit breaker, the API keeps responding with a 200 OK
status code. No validation errors are attached to the order, but the validations_count
value and the circuit breaker internal counter (if closed) are incremented.
Your service response (or error) must match the format described in the example below.
Remember to pass the attribute-specific errors within the data
object, associating them with the related order's attribute. If some error is not associated with any particular attribute use the base
key (automatically filled with the error messages associated with any invalid attribute, if present).
The successful response must be a JSON object, returning an empty body, meaning that the external logic running the validation has found no errors:
When you activate external validation, a shared secret is generated at the market level. We recommend verifying the callback authenticity by signing the payload with that shared secret and comparing the result with the callback signature header.
Callbacks securityIf the external validation fails the API responds to the PATCH
above with a 422
error, showing the message(s) that your logic has returned. Please note that the order is not marked as invalid and can still be placed (in case it passes the standard, automatic validation at the time of placement). External validation errors are stored (until order approval) in which cannot be fetched by a sales channel (e.g. directly from the customer), so it's up to the client side to warn customers about them.