Example: validating an address during registration

During user registration, you can call the third-party address validation service to check whether an address exists. In this sample, a mock-up validation method is used to demonstrate how to write a sample code in the Externalized Customization (xC) workspace.

About this task

The following example registers an extension, which checks whether an address parameter is passed from the storefront. It then calls a third-party address validation service to verify whether that the address exists.

Procedure

  1. Create the CSV input files needed for registering your extension.
    Cmdreg.csv
    This file is used to load the data to register the command for the extension. This registration enables the extension for a store so that it can be called before or after or as a replacement for an existing command implementation. The extension is registered and enabled for the store that is identified within this input file. If you want to register the extension for multiple stores, you must include the data for registering the extension for each store on a separate line in the file.
    For an example, see CmdReg_invupdate.csv.
    Ueconfig.csv
    This file is used to configure the extension, such as to define the URL path to where the extension implementation is located, and to define any properties or filters for the extension. By default, the URL path for the provided sample extension is included within the input file. If needed, you can replace it with your own path.

    For an example, see ueconfig.csv.

    Note: The preExecute, replaceExecute, and postExecute xC extensions can all be specified for the same command interface, for any two interfaces, or for any interface.
  2. Run the data load process to load the data from your input files.
  3. Run the method that processes xC extension requests. It must have the following form:
    /**
      * The pre API extension invoked by the UserRegistrationAddCmd controller command.
      * Checks address validation using a third-party address validation service.
      * 
      * @param ueRequest
      *            The UERequest passed.
      * 
      * @return The response.
    */ 
           @POST
    	@Path("/person-reg-validate-addr")
    	@Consumes({ MediaType.APPLICATION_JSON })
    	@Produces({ MediaType.APPLICATION_JSON })
    	@ApiOperation(value = "This is the preUE api for UserRegistrationAddCmd controller command.", notes = "UserRegistrationAddCmd is a controller command which is used to register a new shopper", response = PersonPreUEResponse.class)
    	@ApiResponses(value = { @ApiResponse(code = 400, message = "Application error"),
    			@ApiResponse(code = 500, message = "System error") })
    	public Response personRegisterPreAddrValidate(
    			@ApiParam(name = "personRegisterPreAddrValidate UE input", value = "The UE Request String", required = true) PersonPreUERequest ueRequest) {
    
    		Person personPOJO = ueRequest.getPerson();
    
    		Map<String, Object> inputParameters = ueRequest.getCommandInputs();
    		String regAddr = inputParameters.get("address1").toString();
    
    		// The following checkIfAddressExisted method is only a sample snippet for demonstating how to structure your call. No logic exists for this method. Replace the code with your custom address validation method call.
    		if (!checkIfAddressExisted(regAddr)) {
    			ErrorUEResponse ueResponse = new ErrorUEResponse();
    			List<ExceptionData> errors = new ArrayList<ExceptionData>();
    			ExceptionData error = new ExceptionData();
    			error.setCode("400");
    			error.setMessageKey("_ERR_INVALID_PARAMETER_VALUE");
    			error.setMessage(MessageKey.getMessage(locale, "_ERR_INVALID_PARAMETER_VALUE",
    					new Object[] { "This address is not existed, pls double check ", regAddr }));
    			errors.add(error);
    			ueResponse.setErrors(errors);
    			Response response = Response.status(400).type(MediaType.APPLICATION_JSON).entity(ueResponse).build();
    
    			return response;
    		}
    		PersonPreUEResponse ueResponse = UserExitUtil.createPreUEResponse(personPOJO, inputParameters);
    		Response response = Response.ok(ueResponse).build();
    
    		return response;
    	}
    
  4. Verify the customization assets.
    1. Start the published storefront.
    2. Go to the User registration page. Update the necessary information.
    3. In the Address field, provide data that does not pass the validation of checkIfAddressExisted.
    4. Click Submit.
    5. An error message is displayed indicating that the address does not exist.