Search code examples
salesforce-commerce-cloud

Salesforce Commerce Cloud Shop API Order status update fails


I have a question about the update of the status of an order. I create the basket using OCAPI and then I can successfully create an order with https://mydomain/dw/shop/v21_10/orders (POST) Order is fine. All data are there. Now the order has status CREATED and I want to change it to OPEN using the Shop API again. https://mydomain/dw/shop/v21_10/orders/MyOrderNumber (PATCH)

This is the message I receive

{"_v":"21.10","fault":{"arguments":{"statusFrom":"CREATED","statusTo":"OPEN"},"type":"StatusTransitionNotPossibleException","message":"The status transition from 'CREATED' to 'OPEN' isn't possible."}}

While if I try to make the same transition in Business Manager it works perfectly.

Anyone knows why?


Solution

  • The reason you can't transition the order from CREATED to OPEN is because there is a step in between.

    Unfortunately, this is poorly documented in OCAPI docs, but we can see a clearer picture if we look at the DW API docs which explain in more detail.

    A bit of background

    First, notice the description of the PATCH /orders/{order_no} endpoint:

    same status transitions are possible as for dw.order.Order.setStatus(int status) plus CREATED to FAILED)

    Okay, so it follows the DW API rules. Let's take a look at the Order.setStatus() method (emphasis mine):

    This method does not support order statuses ORDER_STATUS_CREATED or ORDER_STATUS_FAILED. Please use OrderMgr.placeOrder(Order) or OrderMgr.failOrder(Order).

    So this is telling us that we can't move an order out of CREATED status except by either placing it or failing it. If we take a look at the documentation for OrderMgr.placeOrder(order), it corroborates this:

    This method places an order and is usually called after payment has been authorized. The specified order must be in status CREATED, and will be set to status NEW.

    Now back to OCAPI

    Now we know that OCAPI generally follows the same rules as the DW API when it comes to transitioning order statuses. (with the exception of CREATED -> FAILED)

    So how do we go about placing an order with OCAPI?

    With OCAPI, you can place an order by calling POST orders/{order_no}/payment_instruments which will trigger the following hooks:

    • Authorize the payment with either dw.order.hooks.PaymentHooks.authorizeCreditCard or dw.order.hooks.PaymentHooks.authorize, depending on the payment type
    • Place the order with dw.ocapi.shop.order.afterPostPaymentInstrument

    On success of both those hooks, your order will now be in NEW status, and ready to be transitioned into OPEN.

    This discussion is on the same topic, although talking about the controller context, not OCAPI.

    So, even though business manager lets you get away with stuff (I assume so that admins can override things where necessary), in the context of OCAPI or a controller, the order has to follow the regular placement steps.