Order placement validation
As a shopper places an order, a third-party system is used to confirm that the items added to the order can be shipped to the selected address.
Webhook name
PaaS only plugin.magento.sales.api.order_management.place
SaaS only plugin.sales.api.order_management.place
You can create a similar webhook with observer.sales_order_place_before
(PaaS) or observer.sales_order_place_before
(SaaS). It contains similar data described in this use case, but the placement of the order
information within the payload structure differs.
Payloads
In the following example default payload, some data has been removed for brevity.
Copied to your clipboard{"order": {"base_currency_code": "USD","base_discount_amount": 0.0,"base_grand_total": 110.0,"base_discount_tax_compensation_amount": 0.0,"base_shipping_amount": 20.0,..."items": [{"sku": "Pr-1","name": "Product 1",...},{"sku": "Pr-2","name": "Product 2",...}],"status_histories": [...],"extension_attributes": [...],"addresses": [{"region_id": "57","postcode": 78768,"country_id": "US","address_type": "shipping",...},{"region_id": "57","postcode": 78768,"country_id": "US","address_type": "billing",...}],"shipping_method": "tablerate_bestway","payment": {"method": "checkmo","additional_data": null,...},"gift_cards": "[]","gift_cards_amount": 0,...}}
Copied to your clipboard{"order": {"items": [{"sku": "Pr-1"},{"sku": "Pr-2"}],"addresses": [{"region_id": "57","postcode": 78768,"country_id": "US","address_type": "shipping",...},{"region_id": "57","postcode": 78768,"country_id": "US","address_type": "billing",...}]}}
Configuration
Copied to your clipboard<method name="plugin.magento.sales.api.order_management.place" type="before"><hooks><batch name="order_validation" order="200"><hook name="validate_product_shipping_address" url="{env:APP_BUILDER_URL}/validate-order" priority="100" fallbackErrorMessage="Could not validate the shipping address" timeout="5000" softTimeout="1000" required="true" active="true" method="POST"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><fields><field name="order.items[].sku"/><field name="order.addresses"/></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: plugin.sales.api.order_management.placeWebhook type: beforeBatch name: order_validationBatch order: 200Hook name: validate_product_shipping_addressHook priority: 100URL: <Host>/validate-orderTimeout: 5000Soft timeout: 1000Fallback Error Message: Could not validate the shipping addressRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: order.items[].skuSource: order.items[].skuActive: YesName: order.addressesSource: order.addressesActive: Yes
Copied to your clipboard<method name="plugin.magento.sales.api.order_management.place" type="before"><hooks><batch name="order_validation" order="200"><hook name="validate_product_shipping_address" url="{env:APP_BUILDER_URL}/validate-order" priority="100" fallbackErrorMessage="Could not validate the shipping address" timeout="5000" softTimeout="1000" required="true" active="true" method="POST"><headers><header name="x-gw-ims-org-id">{env:APP_BUILDER_IMS_ORG_ID}</header><header name="Authorization">Bearer {env:APP_BUILDER_AUTH_TOKEN}</header></headers><fields><field name="order.items[].sku"/><field name="order.addresses"/></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: plugin.sales.api.order_management.placeWebhook type: beforeBatch name: order_validationBatch order: 200Hook name: validate_product_shipping_addressHook priority: 100URL: <Host>/validate-orderTimeout: 5000Soft timeout: 1000Fallback Error Message: Could not validate the shipping addressRequired: RequiredActive: YesMethod: POSTDeveloper Console OAuthClient ID: The client ID for the OAuth credential.Client Secret: The client secret for the OAuth credential.Organization ID: The organization ID for the OAuth credential.Hook FieldsName: order.items[].skuSource: order.items[].skuActive: YesName: order.addressesSource: order.addressesActive: Yes
Endpoint code example
The following code example shows how to implement the webhook on your custom endpoint.
Copied to your clipboardconst { Core } = require('@adobe/aio-sdk')const { errorResponse, stringParameters, checkMissingRequestInputs } = require('../utils')// main function that will be executed by Adobe I/O Runtimeasync function main (params) {// create a Loggerconst logger = Core.Logger('main', { level: params.LOG_LEVEL || 'info' })try {// 'info' is the default level if not setlogger.info('Calling the main action')// log parameters, only if params.LOG_LEVEL === 'debug'logger.debug(stringParameters(params))// check for missing request input parameters and headersconst requiredParams = [/* add required params */]const requiredHeaders = ['Authorization']const errorMessage = checkMissingRequestInputs(params, requiredParams, requiredHeaders)if (errorMessage) {// return and log client errorsreturn errorResponse(400, errorMessage, logger)}// Place the real call to a 3rd party endpoint here.// In this example, we check if the shipping address' postcode is greater than 70000.// If it is, an exception with a message about being unable to ship is thrown.const response = {statusCode: 200}for (let i = 0; i < params.order.addresses.length; i++) {let address = params.order.addresses[i]if (address.address_type === 'shipping' && address.postcode > 70000) {response.body = JSON.stringify({op: "exception",message: `App Builder Webhook Response: Products can not be shipped to postcode "${address.postcode}"`})return response;}}response.body = JSON.stringify({op: "success"})return response} catch (error) {// log any server errorslogger.error(error)// return with 500return errorResponse(500, 'server error', logger)}}exports.main = main
If the products in the order cannot be shipped to the selected address, the runtime AppBuilder action returns an exception message. The message is visible to the customer.
Copied to your clipboardresponse.body = JSON.stringify({op: "exception",message: `App Builder Webhook Response: Products can not be shipped to postcode "${address.postcode}"`})