question

fakade avatar image
fakade asked fakade answered

Copy an order from existing order with SDK?

Motive : I am trying to create an order from existing order with all the line items and its modifiers. I have tried the following methods but still I get an error.

Method #1:
The below ones copy only the main line items and not the modifiers of the line items.

Order existingOrder = mOrderConnector.getOrder(currentOrderId); Order newOrder = mOrderConnector.createOrder(new Order()); for (LineItem i : existingOrder.getLineItems()) { if (i.getId() != null) { mOrderConnector.addFixedPriceLineItem(newOrder.getId(), i.getItem().getId(), null, null); } }

The below snippet to copy the modifier from individual line items from existing order and put to new order. This created a 400 error, even though i gave my application full access for all the provided ones in clover dashboard. Also I made sure that the modifier has all the values set and the id is valid.

mOrderConnector.addLineItemModification(newOrder.getId(),"MODIFIER_ID", newMod);
W/System.err: com.clover.sdk.v1.ClientException: status code: 400 Order{json='ORDER JSON', bundle=null, changeLog=null} at com.clover.sdk.v1.ServiceConnector.throwOnFailure(ServiceConnector.java:318) at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:251) at com.clover.sdk.v3.order.OrderV31Connector.addLineItemModification(OrderV31Connector.java:385)


Method #2:

The below method also gave me the same 400 error which is mentioned above.

mOrderConnector.updateLineItems(newOrder.getId(), lineItemList);


Method #3:

The below method also gave me the same 400 error which was mentioned on the #1 method.

mOrderConnector.createLineItemsFrom(newOrder.getId(), existingOrder.getId(), lineItemList);


Method #4:

Also tried the below method and got the following error.

Order existingOrder = mOrderConnector.getOrder(currentOrderId);
Log.d(TAG, "Current order id : " + currentOrderId);
Log.d(TAG, "Current order JSON : " + existingOrder);

Order newOrder = new Order(existingOrder);
newOrder.setId("AZQWERTY05231");
Log.d(TAG, "New order id : " + newOrder.getId());
Log.d(TAG, "New order JSON : " + newOrder);

mOrderConnector.updateOrder(newOrder);

This is the response for the above snippet.

Current order id : 05V2YDCDFSH50
Current order JSON : Order{json='ORDER_JSON_1', bundle=null, changeLog=null}
New order id : AZQWERTY05231
New order JSON : Order{json='ORDER_JSON_1', bundle=null, changeLog=Bundle[{id=null}]}
W/System.err: com.clover.sdk.v1.ClientException: status code: 400 cannot update one or more of these fields: Order{json='{"id":"AZQWERTY05231"}', bundle=null, changeLog=null}
W/System.err:     at com.clover.sdk.v1.ServiceConnector.throwOnFailure(ServiceConnector.java:318)
W/System.err:     at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:251)
W/System.err:     at com.clover.sdk.v3.order.OrderV31Connector.updateOrder(OrderV31Connector.java:240)



Is there any other way to do this ? Thanks for your time.

OrdersInventory
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fakade avatar image
fakade answered

Below snippet will help you to copy the items from one order to new order along with all the modifications for the lineitems.


for(int i=0; i<existingOrder.getLineItems().size();i++){
    String currentItemId = existingOrder.getLineItems().get(i).getItem().getId();
    mOrderConnector.addFixedPriceLineItem(newOrder.getId(), currentItemId, null,null);
    newOrder = mOrderConnector.updateOrder(newOrder);
    List<ModifierGroup> modifierGroups = mInventoryConnector.getModifierGroupsForItem(currentItemId);
    if (modifierGroups.size() > 0){
        List<Modifier> modifiers = modifierGroups.get(0).getModifiers();
        if(modifiers.size() > 0 ){
            for (Modifier mod : modifiers){
                for(Modification modification : existingOrder.getLineItems().get(i).getModifications()){
                    if(mod.getId().equals(modification.getModifier().getId())){
                        mOrderConnector.addLineItemModification(newOrder.getId(), newOrder.getLineItems().get(i).getId(), mod);
                    }
                }
            }
        }
    }
}
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Marginian avatar image
David Marginian Deactivated answered David Marginian Deactivated edited

A 400 is a bad request and not related to permissions.

  1. mOrderConnector.addLineItemModification(newOrder.getId(),"MODIFIER_ID", newMod);

The second parameter is supposed to be the uuid of the "line item". MODIFIER_ID is not valid.

19 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fakade avatar image fakade commented ·

Hey David,

Tried with the below code snippet as per your suggestion. It seems i copied the wrong value to paste in the forum, updating the snippet and its response below.


Order existingOrder = mOrderConnector.getOrder("05V2YDCDFSH50");
Order newOrder = mOrderConnector.createOrder(new Order());

String lineItemId = null;
for (LineItem i : existingOrder.getLineItems()) {
    if (i.getId() != null) {
        mOrderConnector.addFixedPriceLineItem(newOrder.getId(), i.getItem().getId(), null, null);
        lineItemId = i.getItem().getId();
        Log.d(TAG, "copyAndOpenRegister: LineItem uuid : " + lineItemId);
    }
}
mOrderConnector.updateOrder(newOrder);
Log.d(TAG, "Before MOD: New order JSON : " + newOrder);

Modifier mod = new Modifier();
mod.setId("88HY8CB6PCY5M");
mod.setName("Medium");
mod.setPrice((long) 100);

Log.d(TAG, "New order JSON : " + newOrder);
Log.d(TAG, "Line item id : " + lineItemId);
mOrderConnector.addLineItemModification(newOrder.getId(), lineItemId, mod);
mOrderConnector.updateOrder(newOrder);


Adding the response in next comment.

0 Likes 0 ·
fakade avatar image fakade fakade commented ·

Response for the above given snippet.

Existing order JSON : Order{json='EXISTING ORDER JSON', bundle=null, changeLog=nul
Before MOD: New order JSON : Order{json='{"id":"2TA3M9AJ24DDG","device":{"id":"1c36ed52-c6ce-40de-8daa-95721241592a"},"currency":"USD","isVat":false,"groupLineItems":true,"testMode":false,"createdTime":1609220890931,"clientCreatedTime":1609220890931,"taxRemoved":false,"manualTransaction":false,"total":0,"employee":{"id":"938N79ZQ9T5QT"}}', bundle=null, changeLog=Bundle[{customers=null}]}
New order JSON : Order{json='{"id":"2TA3M9AJ24DDG","device":{"id":"1c36ed52-c6ce-40de-8daa-95721241592a"},"currency":"USD","isVat":false,"groupLineItems":true,"testMode":false,"createdTime":1609220890931,"clientCreatedTime":1609220890931,"taxRemoved":false,"manualTransaction":false,"total":0,"employee":{"id":"938N79ZQ9T5QT"}}', bundle=null, changeLog=Bundle[{customers=null}]}

W/System.err: com.clover.sdk.v1.ClientException: status code: -1
W/System.err:     at com.clover.sdk.v1.ServiceConnector.throwOnFailure(ServiceConnector.java:318)
W/System.err:     at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:251)
W/System.err:     at com.clover.sdk.v3.order.OrderV31Connector.addLineItemModification(OrderV31Connector.java:385)


0 Likes 0 ·
David Marginian avatar image David Marginian ♦♦ fakade commented ·

It looks like you are setting lineItemId to the inventory item id: i.getItem().getId(), shouldn't that be i.getId()?

0 Likes 0 ·
Show more comments

Welcome to the
Clover Developer Community