Customer address modification
When a customer signs in and adds a new address, the address must be converted to the proper format. Before the new address is saved, Commerce can call a third-party address system to validate and update the input information. If the address is not in the correct format it's updated and saved.
Webhook name
PaaS only plugin.magento.customer.api.address_repository.save
SaaS only plugin.customer.api.address_repository.save
Payloads
In this example, the default payload is the same as the target payload.
Copied to your clipboard{"address": {"id": "int","customer_id": "int","region": {"region_code": "string","region": "string","region_id": "int","extension_attributes": "\Magento\Customer\Api\Data\RegionExtensionInterface"},"region_id": "int","country_id": "string","street": "string[]","company": "string","telephone": "string","fax": "string","postcode": "string","city": "string","firstname": "string","lastname": "string","middlename": "string","prefix": "string","suffix": "string","vat_id": "string","default_shipping": "bool","default_billing": "bool","extension_attributes": []}}
Configuration
The entirety of the address
object in the payload will be sent to the configured endpoint.
webhook.xml (PaaS)
Copied to your clipboard<method name="plugin.magento.customer.api.address_repository.save" type="before"><hooks><batch name="update_address"><hook name="update_address" url="{env:APP_BUILDER_URL}/update-address"method="POST" timeout="5000" softTimeout="1000" fallbackErrorMessage="The address can not be updated"><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="address" /></fields></hook></batch></hooks></method>
Admin (SaaS)
Copied to your clipboardHook SettingsWebhook method: plugin.magento.customer.api.address_repository.saveWebhook type: beforeBatch name: update_addressHook name: update_addressURL: <Host>/update-addressTimeout: 5000Soft timeout: 1000Fallback Error Message: The address cannot be updatedRequired: 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: addressSource: addressActive: Yes
Copied to your clipboard<method name="plugin.magento.customer.api.address_repository.save" type="before"><hooks><batch name="update_address"><hook name="update_address" url="{env:APP_BUILDER_URL}/update-address"method="POST" timeout="5000" softTimeout="1000" fallbackErrorMessage="The address can not be updated"><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="address" /></fields></hook></batch></hooks></method>
Copied to your clipboardHook SettingsWebhook method: plugin.magento.customer.api.address_repository.saveWebhook type: beforeBatch name: update_addressHook name: update_addressURL: <Host>/update-addressTimeout: 5000Soft timeout: 1000Fallback Error Message: The address cannot be updatedRequired: 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: addressSource: addressActive: Yes
Endpoint code example
The following code example shows how to implement the webhook on your custom endpoint.
Copied to your clipboardconst fetch = require('node-fetch')const { 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 validation (calling 3rd party endpoints) here.// In this example, we check if the postal code is missing 4 digit code and add it to the provided postcode.const address = params.addresslet operations = [];if (address.postcode.length === 5) {// Creates operations to update the address postcode// In the same way other address fields can be updatedconst plusFour = '4023'operations.push({op: "replace",path: "address/postcode",value: address.postcode + '-' + plusFour});} else {// If no updates is needed the success operation must be returnedoperations.push({op: 'success'})}return {statusCode: 200,body: JSON.stringify(operations)}} catch (error) {// log any server errorslogger.error(error)// return with 500return errorResponse(500, 'server error', logger)}}exports.main = main