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
Iftrue
then the existingsubscription_items
list for the subscription is replaced by the one provided. Iffalse
then the providedsubscription_items
list gets added to the existing list." But nothing about how and where the list should be provided.
EDIT: Issue Resolved
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();
}
}