From 584ae2d086d3a2e39439d7e8e76b62742eac5dce Mon Sep 17 00:00:00 2001 From: Ian Atha Date: Tue, 25 Apr 2017 13:47:04 +0300 Subject: [PATCH] Fix: merging Subs with common Plans Fixes #1 --- main.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 042f0f0..a8db450 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,15 @@ func log(f string, args ...interface{}) { fmt.Printf(" " + fmt.Sprintf(f, args...) + "\n") } +func SubContainsPlanID(sub *stripe.Sub, needle *stripe.Plan) *stripe.SubItem { + for _, item := range sub.Items.Values { + if (item.Plan.ID == needle.ID) { + return item + } + } + return nil +} + func combine(ids []string, key string, run bool) error { if len(ids) < 2 { return fmt.Errorf("At least two subscription IDs are neeed, got %d", len(ids)) @@ -152,11 +161,22 @@ func combine(ids []string, key string, run bool) error { for _, s := range rest { items := []*stripe.SubItemsParams{} for _, item := range s.Items.Values { - log("Added plan %s with quantity %d to %s", item.Plan.ID, item.Quantity, primary.ID) - items = append(items, &stripe.SubItemsParams{ - Plan: item.Plan.ID, - Quantity: item.Quantity, - }) + existing_sub := SubContainsPlanID(primary, item.Plan) + if (existing_sub != nil) { + items = append(items, &stripe.SubItemsParams{ + ID: existing_sub.ID, + Plan: item.Plan.ID, + Quantity: existing_sub.Quantity + item.Quantity, + }) + existing_sub.Quantity = existing_sub.Quantity + item.Quantity + log("Updating existing subscription item %s to quantity %d because it already subscribes %s to %s", existing_sub.ID, existing_sub.Quantity, primary.ID, item.Plan.ID) + } else { + log("Added plan %s with quantity %d to %s", item.Plan.ID, item.Quantity, primary.ID) + items = append(items, &stripe.SubItemsParams{ + Plan: item.Plan.ID, + Quantity: item.Quantity, + }) + } } if run { _, err := api.Subs.Update(primary.ID, &stripe.SubParams{