In this post, I will discuss how one can expose an endpoint in Hybris to be consumed by an external system. All of the code that we will add will be in an extension which extends the “commercewebservices” extension. Also, we will be accepting XML format.
Request object
The API that we will create will be a POST call. Suppose the external system wants to update paymentStatus on an orderEntry, the request object will look like the following:
<SettlementStatus>
<orderId>11000000074006</orderId>
<lineItems>
<lineItem>
<lineNumber>1</lineNumber>
<paymentStatus>1234567</paymentStatus>
</lineItem>
</lineItems>
</SettlementStatus>
Request object’s bean definition
We will create the following beans in the <site>commercewebservices-beans.xml file:
<bean class="de.hybris.platform.commercewebservicescommons.dto.SettlementStatus">
<property name="orderId" type="String" />
<property name="lineItems" type="de.hybris.platform.commercewebservicescommons.dto.LineItems" />
</bean>
<bean class="de.hybris.platform.commercewebservicescommons.dto.LineItems">
<property name="lineItem" type="java.util.List<de.hybris.platform.commercewebservicescommons.dto.LineItem>" />
</bean>
<bean class="de.hybris.platform.commercewebservicescommons.dto.LineItem">
<property name="lineNumber" type="String" />
<property name="paymentStatus" type="String" />
</bean>
Do ant clean all to general the data classes of the request object.
Response object
The response sample will look like the following:
<MessageResponse>
<message>Updated the payment status successfully.</message>
<statusCode>200</statusCode>
</MessageResponse>
Response object’s bean definition
We will create the following beans in the <site>commercewebservices-beans.xml file:
<bean class="de.hybris.platform.commercewebservicescommons.dto.MessageResponse">
<property name="statusCode" type="String" />
<property name="message" type="String" />
</bean>
Controller for handling the call
Suppose the endpoint that the external system will hit is /oms/paymentStatusUpdateFromExternal
Create the following Java class inside your extension:
package com.<site>commercewebservices.core.v2.controller;
import com.<site>.core.services.ReturnSalesSlipService;
import de.hybris.platform.commercewebservicescommons.dto.SettlementStatus;
import de.hybris.platform.commercewebservicescommons.dto.MessageResponse;
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(value = "/oms/paymentStatusUpdateFromExternal")
public class PaymentStatusUpdateFromExternalController extends BaseController {
private static final Logger LOG = Logger.getLogger(PaymentStatusUpdateFromExternalController.class);
public static final String SUCCESS_CODE = "200";
public static final String ERROR_OCCURRED_CODE = "510";
public static final String PROCESSED_WITH_ERROR_CODE = "511";
@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public MessageResponse updateReturnSalesSlip(@RequestBody final SettlementStatus settlementStatus) {
final MessageResponse messageResponse = new MessageResponse();
// TODO: Handle the request
// TODO: set the response
messageResponse.setStatusCode(SUCCESS_CODE);
messageResponse.setMessage("Updated the payment status successfully.");
return messageResponse;
}
}
Now you should be able to hit this endpoint with the request!
Finally, for all of my posts in SAP Commerce (Hybris) are here.
0 Comments