question

lisatak avatar image
lisatak asked lisatak answered

Adding discount to custom line item

I'm having trouble adding a discount to a custom line item. Here is the code:

mOrder = mOrderConnector.createOrder(new Order());
                LineItem lineItem = new LineItem();
                lineItem.setName("Gift Card");
                lineItem.setPrice(cardPrice.getCardPrice());

                if (category.equals("Return")) {
                    //make discount object
                    Discount discount = new Discount();
                    discount.setAmount(-cardPrice.getCardPrice());
                    discount.setName("Return");
                    
                    //make discount list
                    List<Discount> discounts = new ArrayList<>();
                    discounts.add(discount);
                    
                    //add discount to line item
                    lineItem.setDiscounts(discounts);
                }
                //add line item to order
                mOrderConnector.addCustomLineItem(mOrder.getId(), lineItem, false);

I get this error:

2021-08-25 13:18:29.369 12589-12630/com.example.cocoa_giftcards W/System.err: com.clover.sdk.v1.ClientException: status code: 400 cannot update one or more of these fields: LineItem{json='{"discounts":{"elements":[{"amount":-2000,"name":"Return"}]},"name":"Gift Card","price":2000}', bundle=null, changeLog=null}
2021-08-25 13:18:29.370 12589-12630/com.example.cocoa_giftcards W/System.err:     at com.clover.sdk.v1.ServiceConnector.throwOnFailure(ServiceConnector.java:333)
2021-08-25 13:18:29.370 12589-12630/com.example.cocoa_giftcards W/System.err:     at com.clover.sdk.v1.ServiceConnector.execute(ServiceConnector.java:266)
2021-08-25 13:18:29.370 12589-12630/com.example.cocoa_giftcards W/System.err:     at com.clover.sdk.v3.order.OrderV31Connector.addCustomLineItem(OrderV31Connector.java:343)
2021-08-25 13:18:29.371 12589-12630/com.example.cocoa_giftcards W/System.err:     at com.example.cocoa_giftcards.MainActivity$AddCardToOrder.run(MainActivity.java:837)
2021-08-25 13:18:29.371 12589-12630/com.example.cocoa_giftcards W/System.err:     at java.lang.Thread.run(Thread.java:764)

Any help would be appreciated!

OrdersLineItemsDiscounts
10 |2000

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

1 Answer

lisatak avatar image
lisatak answered

I ended up using orderConnector.addLineItemDiscount2() and I had to make sure I updated the order after adding the custom line item and before adding the discount.

10 |2000

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

Welcome to the
Clover Developer Community