0

I have an issue where we need to be able to replace a list of items, with an new list where one item given as parameter is removed. (As far as I understand is this the only way to remove a item from an subscription with the product catalog 2.0.

public void removeAddon(Subscription subscription, String itemPriceId){
        try{
            List<Subscription.SubscriptionItem> items = subscription.subscriptionItems();

            items.removeIf(item -> item.itemPriceId().equals(itemPriceId));

            Subscription.UpdateForItemsRequest request = Subscription.updateForItems(subscription.id())
                    .replaceItemsList(true)
                    .endOfTerm(false);
            int index = 0;
            for(Subscription.SubscriptionItem updatedItem : items){
                request = request
                        .subscriptionItemItemPriceId(index, updatedItem.itemPriceId())
                        .subscriptionItemQuantity(index, updatedItem.quantity());
                index++;
            }
            request.request();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

There should somehow be a way to replace the old list of items with the new one given the replaceItemsList method that they provide. But so far I have not been able to find any. The documentation simply says:

"replaceItemsList(val) optional, boolean
If true then the existing subscription_items list for the subscription is replaced by the one provided. If false then the provided subscription_items list gets added to the existing list." But nothing about how and where the list should be provided.

EDIT: Issue Resolved

torva
  • 13
  • 3

2 Answers2

0

The replaceItemsList(true) method will indeed replace the existing list of items in the subscription, but you're missing a step to add the new list of items to the subscription update request:

public void removeAddon(Subscription subscription, String itemPriceId) {
    try {
        List<Subscription.SubscriptionItem> items = subscription.subscriptionItems();

        Subscription.SubscriptionItem item = subscription.subscriptionItems()
                        .stream()
                        .filter(current -> current.itemPriceId().equals(itemPriceId))
                        .toList().get(0);

        items.remove(item);

        Subscription.UpdateForItemsRequest updateRequest = Subscription.updateForItems(subscription.id())
                .replaceItemsList(true)
                .endOfTerm(false);
        
        for (Subscription.SubscriptionItem updatedItem : items) {
            updateRequest = updateRequest.subscriptionItemPriceId(updatedItem.itemPriceId());
        }

        updateRequest.request();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Yahor Barkouski
  • 1,361
  • 1
  • 5
  • 21
0

Thank you for your response! This almost solved it. However a few mistakes I did. The "item" is a copy of one of the items in the list, so it does not have the same referance, so it does not get removed. I used items.removeIf(item -> item.itemPriceId().equals(itemPriceId)); instead.

In your for loop you forgot a few steps as well, the subscriptionItemItemPriceId expects a index, and you need to provide the quantity. This was not hard to solve tho.

torva
  • 13
  • 3